SyncCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 96.36%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 59
c 2
b 0
f 1
dl 0
loc 94
ccs 53
cts 55
cp 0.9636
rs 10
wmc 14

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 77 14
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\Helpers\SearchableFinder;
18
use Algolia\ScoutExtended\Repositories\LocalSettingsRepository;
19
use Algolia\ScoutExtended\Settings\Status;
20
use Algolia\ScoutExtended\Settings\Synchronizer;
21
use Illuminate\Console\Command;
22
23
final class SyncCommand extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $signature = 'scout:sync
29
                            {searchable? : The name of the searchable}
30
                            {--keep=none} : In conflict keep the given option';
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected $description = 'Synchronize the given searchable settings';
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 10
    public function handle(
41
        Algolia $algolia,
42
        Synchronizer $synchronizer,
43
        SearchableFinder $searchableFinder,
44
        LocalSettingsRepository $localRepository
45
    ): void {
46 10
        foreach ($searchableFinder->fromCommand($this) as $searchable) {
47 10
            $this->output->text('🔎 Analysing settings from: <info>['.$searchable.']</info>');
48 10
            $status = $synchronizer->analyse($index = $algolia->index($searchable));
49 9
            $path = $localRepository->getPath($index);
50
51 9
            switch ($status->toString()) {
52 9
                case Status::LOCAL_NOT_FOUND:
53 2
                    if ($status->remoteNotFound()) {
54 2
                        $this->output->note('No settings found.');
55 2
                        if ($this->output->confirm('Wish to optimize the search experience based on information from the searchable class?')) {
56 1
                            $this->call('scout:optimize', ['searchable' => $searchable]);
57
58 2
                            return;
59
                        }
60
                    } else {
61
                        $this->output->note('Remote settings <info>found</info>!');
62
                        $this->output->newLine();
63
                    }
64
65 1
                    $this->output->text('⬇️  Downloading <info>remote</info> settings...');
66 1
                    $synchronizer->download($index);
67 1
                    $this->output->success('Settings file created at: '.$path);
68 1
                    break;
69 7
                case Status::REMOTE_NOT_FOUND:
70 1
                    $this->output->success('Remote settings does not exists. Uploading settings file: '.$path);
71 1
                    $synchronizer->upload($index);
72 1
                    break;
73 6
                case Status::BOTH_ARE_EQUAL:
74 1
                    $this->output->success('Local and remote settings match.');
75 1
                    break;
76 5
                case Status::LOCAL_GOT_UPDATED:
77 1
                    if ($this->output->confirm('Local settings got updated. Wish to upload them?')) {
78 1
                        $this->output->text('Uploading <info>local settings</info>...');
79 1
                        $this->output->newLine();
80 1
                        $synchronizer->upload($index);
81
                    }
82 1
                    break;
83 4
                case Status::REMOTE_GOT_UPDATED:
84 1
                    if ($this->output->confirm('Remote settings got updated. Wish to download them?')) {
85 1
                        $this->output->text('Downloading <info>remote settings</info>...');
86 1
                        $this->output->newLine();
87 1
                        $synchronizer->download($index);
88
                    }
89 1
                    break;
90 3
                case Status::BOTH_GOT_UPDATED:
91 3
                    $options = ['none', 'local', 'remote'];
92
93
                    /** @var string $keep */
94 3
                    $keep = $this->option('keep');
95 3
96
                    $choice =
97 3
                        $this->output->choice('Remote & Local settings got updated. Which one you want to preserve?',
98 3
                            $options, $keep);
99 1
100 1
                    switch ($choice) {
101 1
                        case 'local':
102 1
                            $this->output->text('Uploading <info>local settings</info>...');
103 2
                            $this->output->newLine();
104 1
                            $synchronizer->upload($index);
105 1
                            break;
106 1
                        case 'remote':
107 1
                            $this->output->text('Downloading <info>remote settings</info>...');
108
                            $this->output->newLine();
109 8
                            $synchronizer->download($index);
110
                            break;
111
                    }
112
                    break;
113 8
            }
114 8
        }
115
116
        $this->output->newLine();
117
    }
118
}
119