ReImportCommand::handle()   A
last analyzed

Complexity

Conditions 5
Paths 55

Size

Total Lines 62
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 5.0067

Importance

Changes 7
Bugs 1 Features 0
Metric Value
cc 5
eloc 36
c 7
b 1
f 0
nc 55
nop 2
dl 0
loc 62
ccs 29
cts 31
cp 0.9355
crap 5.0067
rs 9.0328

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Algolia\AlgoliaSearch\Exceptions\NotFoundException;
17
use Algolia\AlgoliaSearch\SearchClient;
18
use Algolia\AlgoliaSearch\SearchIndex;
19
use Algolia\ScoutExtended\Helpers\SearchableFinder;
20
use function count;
21
use Illuminate\Console\Command;
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 1
    public function handle(
44
        SearchClient $client,
45
        SearchableFinder $searchableModelsFinder
46
    ): void {
47 1
        $searchables = $searchableModelsFinder->fromCommand($this);
48
49 1
        $config = config();
50
51 1
        $scoutPrefix = $config->get('scout.prefix');
52
53 1
        $this->output->text('🔎 Importing: <info>['.implode(',', $searchables).']</info>');
54 1
        $this->output->newLine();
55 1
        $this->output->progressStart(count($searchables) * 3);
56
57 1
        foreach ($searchables as $searchable) {
58 1
            $index = $client->initIndex((new $searchable)->searchableAs());
59 1
            $temporaryName = $this->getTemporaryIndexName($index);
60
61 1
            tap($this->output)->progressAdvance()->text("Creating temporary index <info>{$temporaryName}</info>");
62
63
            try {
64 1
                $index->getSettings();
65
66 1
                $client->copyIndex($index->getIndexName(), $temporaryName, [
67 1
                    'scope' => [
68
                        'settings',
69
                        'synonyms',
70
                        'rules',
71
                    ],
72 1
                ])->wait();
73
            } catch (NotFoundException $e) {
74
                // ..
75
            }
76
77 1
            tap($this->output)->progressAdvance()->text("Importing records to index <info>{$temporaryName}</info>");
78
79
            try {
80 1
                $config->set('scout.prefix', self::$prefix.'_'.$scoutPrefix);
81 1
                $searchable::makeAllSearchable();
82 1
            } finally {
83 1
                $config->set('scout.prefix', $scoutPrefix);
84
            }
85
86 1
            tap($this->output)->progressAdvance()
87 1
                ->text("Replacing index <info>{$index->getIndexName()}</info> by index <info>{$temporaryName}</info>");
88
89 1
            $temporaryIndex = $client->initIndex($temporaryName);
90
91
            try {
92 1
                $temporaryIndex->getSettings();
93
94 1
                $response = $client->moveIndex($temporaryName, $index->getIndexName());
95
96 1
                if ($config->get('scout.synchronous', false)) {
97 1
                    $response->wait();
98
                }
99
            } catch (NotFoundException $e) {
100 1
                $index->setSettings(['attributesForFaceting' => null])->wait();
101
            }
102
        }
103
104 1
        tap($this->output)->success('All ['.implode(',', $searchables).'] records have been imported')->newLine();
105 1
    }
106
107
    /**
108
     * Get a temporary index name.
109
     *
110
     * @param \Algolia\AlgoliaSearch\SearchIndex $index
111
     *
112
     * @return string
113
     */
114 1
    private function getTemporaryIndexName(SearchIndex $index): string
115
    {
116 1
        return self::$prefix.'_'.$index->getIndexName();
117
    }
118
}
119