Test Failed
Branch develop (dd818d)
by Nuno
03:56
created

ReImportCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 88
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemporaryIndexName() 0 3 1
A handle() 0 56 4
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;
19
use Algolia\ScoutExtended\Algolia;
20
use Algolia\ScoutExtended\Helpers\SearchableFinder;
21
use Algolia\ScoutExtended\Searchable\RecordsCounter;
22
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
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 1
    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 1
        $searchables = $searchableModelsFinder->fromCommand($this);
50
51 1
        $config = config();
52
53 1
        $scoutPrefix = $config->get('scout.prefix');
54 1
        $scoutSynchronous = $config->get('scout.synchronous', false);
55
56 1
        $this->output->text('🔎 Importing: <info>['.implode(',', $searchables).']</info>');
57 1
        $this->output->newLine();
58 1
        $this->output->progressStart(count($searchables) * 3);
59
60 1
        foreach ($searchables as $searchable) {
61 1
            $index = $algolia->index($searchable);
62 1
            $temporaryName = $this->getTemporaryIndexName($index);
63
64 1
            tap($this->output)->progressAdvance()->text("Creating temporary index <info>{$temporaryName}</info>");
65 1
            $shouldCopy = true;
66
67
            try {
68 1
                $searchable::search()->get();
69
            } catch (NotFoundException $e) {
70
                $shouldCopy = false;
71
            }
72
73 1
            if ($shouldCopy) {
74 1
                $algolia->client()->copyIndex($index->getIndexName(), $temporaryName, [
75 1
                    'scope' => [
76
                        'settings',
77
                        'synonyms',
78
                        'rules',
79
                    ],
80 1
                ])->wait();
81
            }
82
83 1
            tap($this->output)->progressAdvance()->text("Importing records to index <info>{$temporaryName}</info>");
84
85
            try {
86 1
                $config->set('scout.prefix', self::$prefix.'_'.$scoutPrefix);
87 1
                $config->set('scout.synchronous', true);
88 1
                $searchable::makeAllSearchable();
89 1
            } finally {
90 1
                $config->set('scout.prefix', $scoutPrefix);
91 1
                $config->set('scout.synchronous', $scoutSynchronous);
92
            }
93
94 1
            tap($this->output)->progressAdvance()->text("Replacing index <info>{$index->getIndexName()}</info> by index <info>{$temporaryName}</info>");
95
96 1
            $algolia->client()->moveIndex($temporaryName, $index->getIndexName())->wait();
97
        }
98
99 1
        tap($this->output)->success('All ['.implode(',', $searchables).'] records have been imported')->newLine();
100 1
    }
101
102
    /**
103
     * Get a temporary index name.
104
     *
105
     * @param \Algolia\AlgoliaSearch\Index $index
106
     *
107
     * @return string
108
     */
109 1
    private function getTemporaryIndexName(Index $index): string
110
    {
111 1
        return self::$prefix.'_'.$index->getIndexName();
112
    }
113
}
114