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

OptimizeCommand::handle()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.1647

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 4
nop 5
dl 0
loc 25
ccs 13
cts 16
cp 0.8125
crap 5.1647
rs 9.4222
c 0
b 0
f 0
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 Illuminate\Console\Command;
17
use Algolia\ScoutExtended\Algolia;
18
use Algolia\ScoutExtended\Settings\Compiler;
19
use Algolia\ScoutExtended\Settings\LocalFactory;
20
use Algolia\ScoutExtended\Helpers\SearchableFinder;
21
use Algolia\ScoutExtended\Exceptions\ModelNotFoundException;
22
use Algolia\ScoutExtended\Repositories\LocalSettingsRepository;
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 3
    public function handle(
40
        Algolia $algolia,
41
        LocalFactory $localFactory,
42
        Compiler $compiler,
43
        SearchableFinder $searchableFinder,
44
        LocalSettingsRepository $localRepository
45
    ) {
46 3
        foreach ($searchableFinder->fromCommand($this) as $searchable) {
47 3
            $this->output->text('🔎 Optimizing search experience in: <info>['.$searchable.']</info>');
48 3
            $index = $algolia->index($searchable);
49 3
            if (! $localRepository->exists($index) ||
50 3
                $this->confirm('Local settings already exists, do you wish to overwrite?')) {
51
                try {
52 3
                    $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 2
                $path = $localRepository->getPath($index);
60 2
                $compiler->compile($settings, $path);
61
                $this->output->success('Settings file created at: '.$path);
62
                $this->output->note('Please review the settings file and synchronize it with Algolia using: "'.
63
                    ARTISAN_BINARY.' scout:sync"');
64
            }
65
        }
66
    }
67
}
68