Passed
Pull Request — master (#15)
by Serhii
06:06
created

ImportCommand::import()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Matchish\ScoutElasticSearch\Console\Commands;
6
7
use Illuminate\Console\Command;
8
use Matchish\ScoutElasticSearch\Jobs\Import;
9
use Matchish\ScoutElasticSearch\Jobs\QueueableJob;
10
use Matchish\ScoutElasticSearch\Searchable\SearchableListFactory;
11
12
final class ImportCommand extends Command
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected $signature = 'scout:import {searchable?* : The name of the searchable}';
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $description = 'Create new index and import all searchable into the one';
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 9
    public function handle(): void
27
    {
28 9
        $this->searchableList($this->argument('searchable'))
29
        ->each(function ($searchable) {
30 9
            $this->import($searchable);
31 9
        });
32 9
    }
33
34 9
    private function searchableList($argument)
35
    {
36
        return collect($argument)->whenEmpty(function () {
37 8
            $factory = new SearchableListFactory(app()->getNamespace(), app()->path());
0 ignored issues
show
introduced by
The method path() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $factory = new SearchableListFactory(app()->getNamespace(), app()->/** @scrutinizer ignore-call */ path());
Loading history...
introduced by
The method getNamespace() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $factory = new SearchableListFactory(app()->/** @scrutinizer ignore-call */ getNamespace(), app()->path());
Loading history...
38
39 8
            return $factory->make();
40 9
        });
41
    }
42
43 9
    private function import($searchable)
44
    {
45 9
        $job = new Import($searchable);
46
47 9
        if (config('scout.queue')) {
48 2
            $job = (new QueueableJob())->chain([$job]);
49
        }
50
51 9
        $bar = (new ProgressBarFactory($this->output))->create();
52 9
        $job->withProgressReport($bar);
53
54 9
        $startMessage = trans('scout::import.start', ['searchable' => "<comment>$searchable</comment>"]);
55 9
        $this->line($startMessage);
0 ignored issues
show
Bug introduced by
It seems like $startMessage can also be of type array; however, parameter $string of Illuminate\Console\Command::line() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        $this->line(/** @scrutinizer ignore-type */ $startMessage);
Loading history...
56
57 9
        dispatch($job)->allOnQueue((new $searchable)->syncWithSearchUsingQueue())
58 9
            ->allOnConnection(config((new $searchable)->syncWithSearchUsing()));
59
60 9
        $doneMessage = trans(config('scout.queue') ? 'scout::import.done.queue' : 'scout::import.done', [
61 9
            'searchable' => $searchable,
62
        ]);
63 9
        $this->output->success($doneMessage);
64 9
    }
65
}
66