Test Failed
Push — develop ( cf0157...996c4f )
by Nuno
05:01
created

OptimizeCommand::handle()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.7629

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 4
nop 5
dl 0
loc 24
ccs 11
cts 16
cp 0.6875
crap 5.7629
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 2
    public function handle(
40
        Algolia $algolia,
41
        LocalFactory $localFactory,
42
        Compiler $compiler,
43
        SearchableFinder $searchableFinder,
44
        LocalSettingsRepository $localRepository
45
    ) {
46 2
        foreach ($searchableFinder->fromCommand($this) as $searchable) {
47 2
            $this->output->text('🔎 Optimizing search experience in: <info>['.$searchable.']</info>');
48 2
            $index = $algolia->index($searchable);
49 2
            if (! $localRepository->exists($index) ||
50 2
                $this->confirm('Local settings already exists, do you wish to overwrite?')) {
51
                try {
52 2
                    $settings = $localFactory->create($index, $searchable);
53 2
                } catch (ModelNotFoundException $e) {
54 2
                    $model = $e->getModel();
55 2
                    $this->output->error("Model not found [$model] resolving [$searchable] settings. Please seed your database with records of this model.");
56 2
                    return 1;
57
                }
58
                $path = $localRepository->getPath($index);
59
                $compiler->compile($settings, $path);
60
                $this->output->success('Settings file created at: '.$path);
61
                $this->output->note('Please review the settings file and synchronize it with Algolia using: "'.
62
                    ARTISAN_BINARY.' scout:sync"');
63
            }
64
        }
65
    }
66
}
67