Test Failed
Pull Request — master (#73)
by
unknown
04:25
created

ReImportCommand::getTemporaryIndexName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\AlgoliaSearch\SearchIndex;
20
use Algolia\ScoutExtended\Helpers\SearchableFinder;
21
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
22
23
final class ReImportCommand extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $signature = 'scout:reimport {searchable? : The name of the searchable}';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected $description = 'Reimport the given searchable into the search index';
34
35
    /**
36
     * @var string
37
     */
38
    private static $prefix = 'temp';
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function handle(
44
        Algolia $algolia,
45
        SearchableFinder $searchableModelsFinder
46
    ): void {
47
        $searchables = $searchableModelsFinder->fromCommand($this);
48
49
        $config = config();
50
51
        $scoutPrefix = $config->get('scout.prefix');
52
        $scoutSynchronous = $config->get('scout.synchronous', false);
53
54
        $this->output->text('🔎 Importing: <info>['.implode(',', $searchables).']</info>');
55
        $this->output->newLine();
56
        $this->output->progressStart(count($searchables) * 3);
57
58
        foreach ($searchables as $searchable) {
59
            $index = $algolia->index($searchable);
60
            $temporaryName = $this->getTemporaryIndexName($index);
61
62
            tap($this->output)->progressAdvance()->text("Creating temporary index <info>{$temporaryName}</info>");
63
64
            try {
65
                $searchable::search()->get();
66
67
                $index->copyTo($temporaryName, [
68
                    'scope' => [
69
                        'settings',
70
                        'synonyms',
71
                        'rules',
72
                    ],
73
                ])->wait();
74
            } catch (NotFoundException $e) {
75
                // ..
76
            }
77
78
            tap($this->output)->progressAdvance()->text("Importing records to index <info>{$temporaryName}</info>");
79
80
            try {
81
                $config->set('scout.prefix', self::$prefix.'_'.$scoutPrefix);
82
                $searchable::makeAllSearchable();
83
            } finally {
84
                $config->set('scout.prefix', $scoutPrefix);
85
            }
86
87
            tap($this->output)->progressAdvance()->text("Replacing index <info>{$index->getIndexName()}</info> by index <info>{$temporaryName}</info>");
88
89
            $temporaryIndex = $algolia->client()
90
                ->initIndex($temporaryName);
91
92
            try {
93
                $temporaryIndex->getSettings();
94
95
                $response = $temporaryIndex->moveTo($index->getIndexName());
0 ignored issues
show
Bug introduced by
The method moveTo() does not exist on Algolia\AlgoliaSearch\SearchIndex. ( Ignorable by Annotation )

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

95
                /** @scrutinizer ignore-call */ 
96
                $response = $temporaryIndex->moveTo($index->getIndexName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
97
                if ($scoutSynchronous) {
98
                    $response->wait();
99
                }
100
            } catch (NotFoundException $e) {
101
                $index->setSettings(['attributesForFaceting' => null])->wait();
102
            }
103
        }
104
105
        tap($this->output)->success('All ['.implode(',', $searchables).'] records have been imported')->newLine();
106
    }
107
108
    /**
109
     * Get a temporary index name.
110
     *
111
     * @param \Algolia\AlgoliaSearch\SearchIndex $index
112
     *
113
     * @return string
114
     */
115
    private function getTemporaryIndexName(SearchIndex $index): string
116
    {
117
        return self::$prefix.'_'.$index->getIndexName();
118
    }
119
}
120