ClearCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 12
nop 2
1
<?php
2
/**
3
 * For the full copyright and license information, please view the LICENSE.md
4
 * file that was distributed with this source code.
5
 */
6
7
namespace Notamedia\ConsoleJedi\Cache\Command;
8
9
use Bitrix\Main\Application;
10
use Bitrix\Main\Data\Cache;
11
use Bitrix\Main\Data\StaticHtmlCache;
12
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Command for clear Bitrix cache.
19
 *
20
 * @author Nik Samokhvalov <[email protected]>
21
 */
22
class ClearCommand extends BitrixCommand
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        $this->setName('cache:clear')
30
            ->setDescription('Clear all cache')
31
            ->addOption('dir', 'd', InputOption::VALUE_REQUIRED, 'Clear cache only for directory (relative from /bitrix/cache/)')
32
            ->addOption('tag', 't', InputOption::VALUE_REQUIRED, 'Clear cache by tag');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $dir = $input->getOption('dir');
41
        $tag = $input->getOption('tag');
42
        $cache = Cache::createInstance();
43
44
        if (empty($dir) && empty($tag)) {
45
            Application::getInstance()->getManagedCache()->cleanAll();
46
            $cache->cleanDir();
47
            $cache->cleanDir(false, 'stack_cache');
48
            StaticHtmlCache::getInstance()->deleteAll();
49
50
            if (Cache::clearCache(true)) {
51
                $output->writeln('<info>All Bitrix cache was deleted</info>');
52
            } else {
53
                $output->writeln('<error>Error deleting Bitrix cache</error>');
54
            }
55
        }
56
57
        if ($dir) {
58
            $cache->cleanDir($dir);
59
            $output->writeln('<info>Bitrix cache by "/' . BX_ROOT . '/cache/' . $dir . '" dir was deleted</info>');
60
        }
61
62
        if ($tag) {
63
            Application::getInstance()->getTaggedCache()->clearByTag($tag);
64
            $output->writeln('<info>Bitrix cache by tag "' . $tag . '" was deleted</info>');
65
        }
66
    }
67
}