ReIndexCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
C execute() 0 35 7
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\Search\Command;
8
9
use Bitrix\Main\Loader;
10
use Bitrix\Main\ModuleManager;
11
use Notamedia\ConsoleJedi\Application\Command\BitrixCommand;
12
use Notamedia\ConsoleJedi\Application\Exception\BitrixException;
13
use Symfony\Component\Console\Helper\ProgressBar;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Command for search module reindex.
20
 *
21
 * @author Marat Shamshutdinov <[email protected]>
22
 */
23
class ReIndexCommand extends BitrixCommand
24
{
25
    const UPDATE_TIME = 5;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function configure()
31
    {
32
        parent::configure();
33
34
        $this->setName('search:reindex')
35
            ->setDescription('Rebuild search index')
36
            ->addOption('full', 'f', InputOption::VALUE_NONE,
37
                'Clears existing index (otherwise only changed entries would be indexed)');
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        if (!Loader::includeModule('search')) {
46
            throw new BitrixException('Search module is not installed');
47
        }
48
49
        $searchResult = array();
50
51
        $bar = new ProgressBar($output, 0);
52
        do {
53
            $bar->display();
54
55
            $searchResult = \CSearch::ReIndexAll($input->getOption('full'), static::UPDATE_TIME, $searchResult);
56
57
            $bar->advance();
58
            $bar->clear();
59
60
            if (is_array($searchResult) && $searchResult['MODULE'] == 'main') {
61
                list(, $path) = explode("|", $searchResult["ID"], 2);
62
                $output->writeln("\r       " . $path, OutputInterface::VERBOSITY_VERBOSE);
63
            }
64
        } while (is_array($searchResult));
65
66
        $bar->finish();
67
        $bar->clear();
68
        $output->write("\r");
69
70
        if (ModuleManager::isModuleInstalled('socialnetwork')) {
71
            $output->writeln('<info>The Social Network module needs to be reindexed using the Social Network component in the public section of site.</info>');
72
        }
73
74
        $output->writeln(sprintf('<info>Reindexed</info> %d element%s.', $searchResult, $searchResult > 1 ? 's' : ''));
75
76
        return 0;
77
    }
78
}