Test Failed
Pull Request — master (#33)
by Serhii
07:11 queued 03:16
created

ImportCommand::searchableList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 10
c 1
b 0
f 1
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\ImportSource;
11
use Matchish\ScoutElasticSearch\Database\Scopes\ImportScope;
12
use Matchish\ScoutElasticSearch\Searchable\ImportSourceFactory;
13
use Matchish\ScoutElasticSearch\Searchable\SearchableListFactory;
14
15
final class ImportCommand extends Command
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected $signature = 'scout:import {searchable?* : The name of the searchable}';
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $description = 'Create new index and import all searchable into the one';
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function handle(): void
30
    {
31 2
        $this->searchableList($this->argument('searchable'))
32
        ->each(function ($searchable) {
33 2
            $this->import($searchable);
34 2
        });
35 2
    }
36
37 2
    private function searchableList($argument)
38
    {
39
        return collect($argument)->whenEmpty(function () {
40
            $factory = new SearchableListFactory(app()->getNamespace(), app()->path());
0 ignored issues
show
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

40
            $factory = new SearchableListFactory(app()->/** @scrutinizer ignore-call */ getNamespace(), app()->path());
Loading history...
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

40
            $factory = new SearchableListFactory(app()->getNamespace(), app()->/** @scrutinizer ignore-call */ path());
Loading history...
41
42
            return $factory->make();
43 2
        });
44
    }
45
46 2
    private function import($searchable)
47
    {
48 2
        $sourceFactory = app(ImportSourceFactory::class);
49 2
        $source = $sourceFactory::from($searchable);
50 2
        $job = new Import($source);
51
52 2
        if (config('scout.queue')) {
53
            $job = (new QueueableJob())->chain([$job]);
54
        }
55
56 2
        $bar = (new ProgressBarFactory($this->output))->create();
57 2
        $job->withProgressReport($bar);
58
59 2
        $startMessage = trans('scout::import.start', ['searchable' => "<comment>$searchable</comment>"]);
60 2
        $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

60
        $this->line(/** @scrutinizer ignore-type */ $startMessage);
Loading history...
61
62
        /* @var ImportSource $source */
63 2
        dispatch($job)->allOnQueue($source->syncWithSearchUsingQueue())
64 2
            ->allOnConnection($source->syncWithSearchUsing());
65
66 2
        $doneMessage = trans(config('scout.queue') ? 'scout::import.done.queue' : 'scout::import.done', [
67 2
            'searchable' => $searchable,
68
        ]);
69 2
        $this->output->success($doneMessage);
70 2
    }
71
}
72