<?php

    namespace MNM\Spitzner\Cron;
    
    use Psr\Log\LoggerInterface;
    use Magento\Framework\App\Cache\TypeListInterface;
    use Magento\Framework\App\Cache\Frontend\Pool;
    
    class ClearCaches {
        protected $cacheTypeList;
        protected $cacheFrontendPool;
        protected $logger;
         
        public function __construct(TypeListInterface $cacheTypeList, Pool $cacheFrontendPool, LoggerInterface $logger)
        {
            $this->cacheTypeList = $cacheTypeList;
            $this->cacheFrontendPool = $cacheFrontendPool;
            $this->logger = $logger;
        }
         
        public function execute()
        {    
            /* get all types of cache in system */
            $allTypes = array_keys($this->cacheTypeList->getTypes());
            
            /* clean */
            foreach ($allTypes as $type) 
            {
                $this->cacheTypeList->cleanType($type);
            }
            
            /* flush */
            foreach ($this->cacheFrontendPool as $cacheFrontend) 
            {
                $cacheFrontend->getBackend()->clean();
            }
            
            $this->logger->info('MNM\Spitzner - ClearCaches - done');
        }
    }