Completed
Push — non_purge_indexer ( f865fe )
by André
29:12 queued 11:33
created

ReindexCommand::getPhpProcess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\Command;
10
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Process\Process;
16
use Symfony\Component\Process\PhpExecutableFinder;
17
use eZ\Publish\Core\Search\Common\Indexer;
18
use ezcSystemInfo;
19
use RuntimeException;
20
21
class ReindexCommand extends ContainerAwareCommand
22
{
23
    /**
24
     * @var \eZ\Publish\Core\Search\Common\Indexer
25
     */
26
    private $searchIndexer;
27
28
    /**
29
     * Initialize objects required by {@see execute()}.
30
     *
31
     * @param InputInterface $input
32
     * @param OutputInterface $output
33
     */
34
    public function initialize(InputInterface $input, OutputInterface $output)
35
    {
36
        parent::initialize($input, $output);
37
        $this->searchIndexer = $this->getContainer()->get('ezpublish.spi.search.indexer');
38
        if (!$this->searchIndexer instanceof Indexer) {
39
            throw new RuntimeException(
40
                sprintf('Expected to find Search Engine Indexer but found "%s" instead', get_parent_class($this->searchIndexer))
41
            );
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function configure()
49
    {
50
        $this
51
            ->setName('ezplatform:reindex')
52
            ->setDescription('Recreate or Refresh search engine index')
53
            ->addOption('iteration-count', 'c', InputOption::VALUE_OPTIONAL, 'Number of objects to be indexed in a single iteration, for avoiding using to much memory', 20)
54
            ->addOption('no-commit', null, InputOption::VALUE_NONE, 'Do not commit after each iteration')
55
            ->addOption('no-purge', null, InputOption::VALUE_NONE, 'Do not purge before indexing. BC NOTE: Should this be default as of 2.0?')
56
            ->addOption('since', null, InputOption::VALUE_NONE, 'Index changes since a given time, any format understood by DateTime. Implies "no-purge", can not be combined with "content-ids".')
57
            ->addOption('content-ids', null, InputOption::VALUE_NONE, 'Comma separated list of content id\'s to reindex (deleted or updated/added). Implies "no-purge", can not be combined with "since".')
58
            ->addOption('processes', null, InputOption::VALUE_NONE, 'Number of sub processes to spawn in parallel, by default: (number of cpus)-1, disable by setting to "0"')
59
            ->setHelp(
60
                <<<EOT
61
The command <info>%command.name%</info> indexes current configured database in configured search engine index.
62
63
64
TODO: ADD EXAMPLES OF ADVANCE USAGE!
65
66
EOT
67
            );
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $iterationCount = $input->getOption('iteration-count');
76
        $noCommit = $input->getOption('no-commit');
77
        $noPurge = $input->getOption('no-purge');
0 ignored issues
show
Unused Code introduced by
$noPurge is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
79
        if (!is_numeric($iterationCount) || (int)$iterationCount < 1) {
80
            throw new RuntimeException("'--iteration-count' option should be > 0, got '{$iterationCount}'");
81
        }
82
83
        $this->searchIndexer->createSearchIndex($output, intval($iterationCount), empty($noCommit));
84
    }
85
86
87
    private static function getPhpProcess($consoleDir = 'app')
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
88
    {
89
        $phpFinder = new PhpExecutableFinder();
90
        if (!$phpPath = $phpFinder->find()) {
91
            throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again');
92
        }
93
94
        $cmd = 'ezplatform:reindex';
95
        $php = escapeshellarg($phpFinder);
96
        $console = escapeshellarg($consoleDir.'/console');
97
98
        return new Process($php.' '.$console.' '.$cmd, null, null, null, null);
99
    }
100
101
    private function getNumberOfCPUs()
102
    {
103
        $sysInfo = ezcSystemInfo::getInstance();
104
105
        return $sysInfo->cpuCount;
106
    }
107
}
108