Completed
Push — main ( 4991e0...ffdea1 )
by
unknown
16s queued 14s
created

ReindexCommand::execute()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 8
eloc 23
c 3
b 1
f 0
nc 10
nop 2
dl 0
loc 31
rs 8.4444
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita Brevia plugin
6
 *
7
 * Copyright 2024 Atlas Srl
8
 */
9
namespace Brevia\BEdita\Command;
10
11
use Brevia\BEdita\Client\BreviaClient;
12
use Brevia\BEdita\Index\CollectionHandler;
13
use Brevia\BEdita\Utility\ReadCSVTrait;
14
use Cake\Command\Command;
15
use Cake\Console\Arguments;
16
use Cake\Console\ConsoleIo;
17
use Cake\Console\ConsoleOptionParser;
18
use Cake\Log\LogTrait;
19
use Cake\Utility\Hash;
20
use Exception;
21
22
/**
23
 * Reindex collection contents
24
 *
25
 * @property \BEdita\Core\Model\Table\ObjectsTable $Collections
26
 */
27
class ReindexCommand extends Command
28
{
29
    use LogTrait;
30
    use ReadCSVTrait;
31
32
    /**
33
     * Brevia API client
34
     *
35
     * @var \Brevia\BEdita\Client\BreviaClient
36
     */
37
    protected BreviaClient $client;
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public $defaultTable = 'Collections';
43
44
    /**
45
     * @inheritDoc
46
     */
47
    protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
48
    {
49
        return $parser->addOption('collection', [
50
                'help' => 'Collection to reindex (use the unique collection name)',
51
                'short' => 'c',
52
                'required' => true,
53
            ])->addOption('document', [
54
                'help' => 'Document to reindex (use the BEdita document ID)',
55
                'short' => 'd',
56
                'required' => false,
57
            ])->addOption('type', [
58
                'help' => 'Specific type to reindex',
59
                'short' => 't',
60
                'required' => false,
61
            ]);
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function initialize(): void
68
    {
69
        $this->client = new BreviaClient();
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function execute(Arguments $args, ConsoleIo $io)
76
    {
77
        $name = $args->getOption('collection');
78
        $response = $this->client->get('/collections', compact('name'));
79
        $collectionId = Hash::get($response->getJson(), '0.cmetadata.id');
80
        if (empty($collectionId)) {
81
            $io->abort(sprintf('Collection not found: %s', $name));
82
        }
83
        $collection = $this->Collections->get($collectionId, ['contain' => ['HasDocuments']]);
84
        $io->out('Start reindexing collection contents...');
85
        $documents = (array)$collection->get('has_documents');
86
        $handler = new CollectionHandler();
87
        $docId = (int)$args->getOption('document');
88
        $type = $args->getOption('type');
89
        foreach ($documents as $doc) {
90
            if (($docId && $doc->id !== $docId) || ($type && $doc->get('type') !== $type)) {
91
                continue;
92
            }
93
            $this->log(sprintf('Reindexing [%s] "%s" - id: %s', $doc->get('type'), $doc->get('title'), $doc->id), 'info');
94
            try {
95
                $doc = $doc->getTable()->get($doc->id);
96
                $handler->addDocument($collection, $doc, false);
97
            } catch (Exception $e) {
98
                $this->log(sprintf('Error reindexing [%s] "%s" - id: %s', $doc->get('type'), $doc->get('title'), $doc->id), 'error');
99
                $this->log($e->getMessage(), 'error');
100
            }
101
        }
102
103
        $io->out('Done');
104
105
        return null;
106
    }
107
}
108