Passed
Push — master ( 3252a3...420639 )
by Andreas
23:53
created

reindex::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.console
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\console\command;
10
11
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use midcom_services_indexer;
15
use midcom_helper__componentloader;
16
use midcom_error;
17
use midcom_helper_nav;
18
use midcom_services_indexer_client;
19
use Symfony\Component\Console\Input\InputOption;
20
use GuzzleHttp\Exception\RequestException;
21
use Symfony\Component\DomCrawler\Crawler;
22
use midcom\console\loginhelper;
23
24
/**
25
 * Reindex command
26
 *
27
 * Drops the index, then iterates over all existing topics, retrieves the corresponding
28
 * interface class and invokes the reindexing.
29
 *
30
 * This may take some time.
31
 *
32
 * @package midcom.console
33
 */
34
class reindex extends Command
35
{
36
    use loginhelper;
37
38
    private midcom_services_indexer $indexer;
39
40
    private midcom_helper__componentloader $loader;
41
42
    public function __construct(midcom_services_indexer $indexer, midcom_helper__componentloader $loader)
43
    {
44
        $this->indexer = $indexer;
45
        $this->loader = $loader;
46
        parent::__construct();
47
    }
48
49
    protected function configure()
50
    {
51
        $this->setName('midcom:reindex')
52
            ->setAliases(['reindex'])
53
            ->setDescription('Reindex')
54
            ->addOption('id', 'i', InputOption::VALUE_OPTIONAL, 'Start node (root if empty)');
55
    }
56
57
    protected function interact(InputInterface $input, OutputInterface $output)
58
    {
59
        if (!$this->indexer->enabled()) {
60
            throw new midcom_error('No indexer backend has been defined. Aborting.');
61
        }
62
63
        if (empty($input->getParameterOption(['--servername', '-s'], null))) {
64
            throw new midcom_error('Please specify host name (with --servername or -s)');
65
        }
66
67
        $dialog = $this->getHelperSet()->get('question');
68
        $this->require_admin($dialog, $input, $output);
69
    }
70
71
    protected function execute(InputInterface $input, OutputInterface $output) : int
72
    {
73
        $nap = new midcom_helper_nav();
74
        $nodes = [];
75
        $node = $nap->get_node((int) $input->getOption('id') ?: $nap->get_root_node());
76
77
        while ($node !== null) {
78
            // Reindex the node...
79
            $output->write("Processing Node #{$node[MIDCOM_NAV_ID]}, {$node[MIDCOM_NAV_FULLURL]}...");
80
            if (!$this->indexer->delete_all("__TOPIC_GUID:{$node[MIDCOM_NAV_OBJECT]->guid}")) {
81
                $output->writeln("\n<error>Failed to remove documents from index.</error>");
82
            }
83
            $interface = $this->loader->get_interface_class($node[MIDCOM_NAV_COMPONENT]);
84
            $stat = $interface->reindex($node[MIDCOM_NAV_OBJECT]);
85
            if ($stat instanceof midcom_services_indexer_client) {
86
                try {
87
                    $stat->reindex();
88
                } catch (RequestException $e) {
89
                    if ($e->hasResponse()) {
90
                        $crawler = new Crawler($e->getResponse()->getBody()->getContents());
91
                        $body = $crawler->filterXPath('//body')->html();
92
                        $output->writeln("\n<error>" . strip_tags($body) . '</error>');
93
                    } else {
94
                        $output->writeln("\n<error>" . $e->getMessage() . '</error>');
95
                    }
96
                }
97
            } elseif ($stat === false) {
98
                $output->writeln("\n<error>Failed to reindex the node {$node[MIDCOM_NAV_ID]} which is of {$node[MIDCOM_NAV_COMPONENT]}.</error>");
99
            }
100
101
            // Retrieve all child nodes and append them to $nodes:
102
            $nodes = array_merge($nodes, $nap->get_nodes($node[MIDCOM_NAV_ID]));
103
            $node = array_shift($nodes);
104
            $output->writeln("Done");
105
        }
106
107
        $output->writeln('Reindex complete');
108
        return 0;
109
    }
110
}
111