Test Failed
Push — develop ( 7ffe68...2ad78c )
by Nuno
04:31
created

ReImportCommand::waitingForRecordsImported()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\AlgoliaSearch\Index;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Index 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...
19
use Algolia\ScoutExtended\Algolia;
20
use Algolia\ScoutExtended\Helpers\SearchableFinder;
21
use Algolia\ScoutExtended\Searchable\RecordsCounter;
22
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Exceptions\NotFoundException 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...
23
24
final class ReImportCommand extends Command
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected $signature = 'scout:reimport {searchable? : The name of the searchable}';
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected $description = 'Reimport the given searchable into the search index';
35
36
    /**
37
     * @var string
38
     */
39
    private static $prefix = 'temp';
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function handle(
45
        Algolia $algolia,
46
        SearchableFinder $searchableModelsFinder,
47
        RecordsCounter $recordsCounter
0 ignored issues
show
Unused Code introduced by
The parameter $recordsCounter is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
        /** @scrutinizer ignore-unused */ RecordsCounter $recordsCounter

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    ): void {
49
        $searchables = $searchableModelsFinder->fromCommand($this);
50
51
        $config = config();
52
53
        $scoutPrefix = $config->get('scout.prefix');
54
        $scoutSynchronous = $config->get('scout.synchronous', false);
55
56
        $this->output->text('🔎 Importing: <info>['.implode(',', $searchables).']</info>');
57
        $this->output->newLine();
58
        $this->output->progressStart(count($searchables) * 3);
59
60
        foreach ($searchables as $searchable) {
61
            $index = $algolia->index($searchable);
62
            $temporaryName = $this->getTemporaryIndexName($index);
63
64
            tap($this->output)->progressAdvance()->text("Creating temporary index <info>{$temporaryName}</info>");
65
66
            $algolia->client()->copyIndex($index->getIndexName(), $temporaryName, [
67
                'scope' => [
68
                    'settings',
69
                    'synonyms',
70
                    'rules',
71
                ],
72
            ])->wait();
73
74
            tap($this->output)->progressAdvance()->text("Importing records to index <info>{$temporaryName}</info>");
75
76
            try {
77
                $config->set('scout.prefix', self::$prefix.'_'.$scoutPrefix);
78
                $config->set('scout.synchronous', true);
79
                $searchable::makeAllSearchable();
80
            } finally {
81
                $config->set('scout.prefix', $scoutPrefix);
82
                $config->set('scout.synchronous', $scoutSynchronous);
83
            }
84
85
            tap($this->output)->progressAdvance()->text("Replacing index <info>{$index->getIndexName()}</info> by index <info>{$temporaryName}</info>");
86
87
            $algolia->client()->moveIndex($temporaryName, $index->getIndexName())->wait();
88
        }
89
90
        tap($this->output)->success('All ['.implode(',', $searchables).'] records have been imported')->newLine();
91
    }
92
93
    /**
94
     * Get a temporary index name.
95
     *
96
     * @param \Algolia\AlgoliaSearch\Index $index
97
     *
98
     * @return string
99
     */
100
    private function getTemporaryIndexName(Index $index): string
101
    {
102
        return self::$prefix.'_'.$index->getIndexName();
103
    }
104
}
105