OptimizeCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 40
ccs 16
cts 16
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 25 5
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\ScoutExtended\Algolia;
17
use Algolia\ScoutExtended\Exceptions\ModelNotFoundException;
18
use Algolia\ScoutExtended\Helpers\SearchableFinder;
19
use Algolia\ScoutExtended\Repositories\LocalSettingsRepository;
20
use Algolia\ScoutExtended\Settings\Compiler;
21
use Algolia\ScoutExtended\Settings\LocalFactory;
22
use Illuminate\Console\Command;
23
24
final class OptimizeCommand extends Command
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected $signature = 'scout:optimize {searchable? : The name of the searchable}';
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected $description = 'Optimize the given searchable creating a settings file';
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 4
    public function handle(
40
        Algolia $algolia,
41
        LocalFactory $localFactory,
42
        Compiler $compiler,
43
        SearchableFinder $searchableFinder,
44
        LocalSettingsRepository $localRepository
45
    ) {
46 4
        foreach ($searchableFinder->fromCommand($this) as $searchable) {
47 4
            $this->output->text('🔎 Optimizing search experience in: <info>['.$searchable.']</info>');
48 4
            $index = $algolia->index($searchable);
49 4
            if (! $localRepository->exists($index) ||
50 4
                $this->confirm('Local settings already exists, do you wish to overwrite?')) {
51
                try {
52 4
                    $settings = $localFactory->create($index, $searchable);
53 1
                } catch (ModelNotFoundException $e) {
54 1
                    $model = $e->getModel();
55 1
                    $this->output->error("Model not found [$model] resolving [$searchable] settings. Please seed your database with records of this model.");
56
57 1
                    return 1;
58
                }
59 3
                $path = $localRepository->getPath($index);
60 3
                $compiler->compile($settings, $path);
61 3
                $this->output->success('Settings file created at: '.$path);
62 3
                $this->output->note('Please review the settings file and synchronize it with Algolia using '.
63 3
                    'the Artisan command `scout:sync`.');
64
            }
65
        }
66 3
    }
67
}
68