Completed
Push — master ( 00cf4f...de116d )
by Nik
02:50
created

ReIndexCommand::execute()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 39
rs 6.7272
cc 7
eloc 21
nc 5
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\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
        {
47
            throw new BitrixException('Search module is not installed');
48
        }
49
50
        $searchResult = array();
51
52
        $bar = new ProgressBar($output, 0);
53
        do
54
        {
55
            $bar->display();
56
57
            $searchResult = \CSearch::ReIndexAll($input->getOption('full'), static::UPDATE_TIME, $searchResult);
58
59
            $bar->advance();
60
            $bar->clear();
61
62
            if (is_array($searchResult) && $searchResult['MODULE'] == 'main')
63
            {
64
                list(, $path) = explode("|", $searchResult["ID"], 2);
65
                $output->writeln("\r       " . $path, OutputInterface::VERBOSITY_VERBOSE);
66
            }
67
        } while (is_array($searchResult));
68
69
        $bar->finish();
70
        $bar->clear();
71
        $output->write("\r");
72
73
        if (ModuleManager::isModuleInstalled('socialnetwork'))
74
        {
75
            $output->writeln('<info>The Social Network module needs to be reindexed using the Social Network component in the public section of site.</info>');
76
        }
77
78
        $output->writeln(sprintf('<info>Reindexed</info> %d element%s.', $searchResult, $searchResult > 1 ? 's' : ''));
79
80
        return 0;
81
    }
82
}