Completed
Push — main ( 6b3c3f...a8a095 )
by
unknown
17s queued 15s
created

ReindexCommand::buildOptionParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
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
            ]);
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function initialize(): void
60
    {
61
        $this->client = new BreviaClient();
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function execute(Arguments $args, ConsoleIo $io)
68
    {
69
        $name = $args->getOption('collection');
70
        $response = $this->client->get('/collections', compact('name'));
71
        $collectionId = Hash::get($response->getJson(), '0.cmetadata.id');
72
        if (empty($collectionId)) {
73
            $io->abort(sprintf('Collection not found: %s', $name));
74
        }
75
        $collection = $this->Collections->get($collectionId, ['contain' => ['HasDocuments']]);
76
        $io->out('Start reindexing collection contents...');
77
        $documents = (array)$collection->get('has_documents');
78
        $handler = new CollectionHandler();
79
        foreach ($documents as $doc) {
80
            $this->log(sprintf('Reindexing [%s] "%s" - id: %s', $doc->get('type'), $doc->get('title'), $doc->id), 'info');
81
            try {
82
                $handler->addDocument($collection, $doc, false);
83
            } catch (Exception $e) {
84
                $this->log(sprintf('Error reindexing [%s] "%s" - id: %s', $doc->get('type'), $doc->get('title'), $doc->id), 'error');
85
                $this->log($e->getMessage(), 'error');
86
            }
87
        }
88
89
        $io->out('Done');
90
91
        return null;
92
    }
93
}
94