Test Failed
Pull Request — master (#128)
by Lucas
02:55
created

StatusCommand::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 39
ccs 0
cts 24
cp 0
rs 9.536
c 0
b 0
f 0
cc 3
nc 3
nop 4
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Console\Commands;
15
16
use function count;
17
use Illuminate\Console\Command;
18
use Algolia\ScoutExtended\Algolia;
19
use Algolia\ScoutExtended\Settings\Synchronizer;
20
use Algolia\ScoutExtended\Helpers\SearchableFinder;
21
use Algolia\ScoutExtended\Searchable\RecordsCounter;
22
23
final class StatusCommand extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $signature = 'scout:status {searchable? : The name of the searchable}';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected $description = 'Show the status of the index of the the given searchable';
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function handle(
39
        Algolia $algolia,
40
        SearchableFinder $searchableFinder,
41
        Synchronizer $synchronizer,
42
        RecordsCounter $recordsCounter
43
    ): void {
44
        $searchables = $searchableFinder->fromCommand($this);
45
46
        $rows = [];
47
48
        $this->output->text('🔎 Analysing information from: <info>['.implode(',', $searchables).']</info>');
49
        $this->output->newLine();
50
        $this->output->progressStart(count($searchables));
51
52
        foreach ($searchables as $searchable) {
53
            $row = [];
54
            $instance = $this->laravel->make($searchable);
55
            $index = $algolia->index($instance);
56
            $row[] = $searchable;
57
            $row[] = $instance->searchableAs();
58
59
            $status = $synchronizer->analyse($index);
60
            $description = $status->toHumanString();
61
            if (! $status->bothAreEqual()) {
62
                $description = "<fg=red>$description</>";
63
            } else {
64
                $description = '<fg=green>Synchronized</>';
65
            }
66
67
            $row[] = $description;
68
            $row[] = $recordsCounter->local($searchable);
69
            $row[] = $recordsCounter->remote($searchable);
70
71
            $rows[] = $row;
72
            $this->output->progressAdvance();
73
        }
74
75
        $this->output->progressFinish();
76
        $this->output->table(['Searchable', 'Index', 'Settings', 'Local records', 'Remote records'], $rows);
77
    }
78
}
79