EnrichDocuments::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Colligator\Console\Commands;
4
5
use Colligator\Collection;
6
use Colligator\Document;
7
use Colligator\EnrichmentService;
8
use Colligator\Search\DocumentsIndex;
9
use Illuminate\Console\Command;
10
11
class EnrichDocuments extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'colligator:enrich
19
                            {service : Name of the service (e.g. "googlebooks")}
20
                            {--f|force : Enrich documents that haven\'t changed}
21
                            {--collection= : Collection id, for enriching only documents belonging to a single collection}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Enrich documents using some service.';
29
30
    /**
31
     * Sleep time in seconds between requests.
32
     *
33
     * @var int
34
     */
35
    public $sleepTime = 5;
36
37
    /**
38
     * @var DocumentsIndex
39
     */
40
    public $docIndex;
41
42
    protected $services;
43
44
    /**
45
     * Create a new command instance.
46
     */
47
    public function __construct()
48
    {
49
        parent::__construct();
50
51
        $this->services = [
52
            \Colligator\GoogleBooksService::$serviceName => 'Colligator\GoogleBooksService',
53
        ];
54
    }
55
56
    public function getDocumentsToBeChecked($serviceName, $force = false, $collectionId = 0)
57
    {
58
59
        if ($collectionId > 0) {
60
            $docs = Collection::findOrFail($collectionId)->documents();
61
        } else {
62
            $docs = Document::query();
63
        }
64
        if ($force) {
65
            return $docs->get();
66
        }
67
        return $docs->whereDoesntHave('enrichments', function($query) use ($serviceName) {
68
            $query->where('service_name', '=', $serviceName);
69
        })->get();
70
    }
71
72
    public function handleDocuments(EnrichmentService $service, $docs)
73
    {
74
        \Log::info('[EnrichDocuments] Starting job. ' . count($docs) . ' documents to be checked using the "' . $service::$serviceName . '" service.');
75
76
        // $this->output->progressStart(count($docs));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
        foreach ($docs as $doc) {
78
            $service->enrich($doc);
79
80
            // Update ElasticSearch
81
            $this->docIndex->index($doc);
82
83
            // $this->output->progressAdvance();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
            sleep($this->sleepTime);
85
        }
86
        // $this->output->progressFinish();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
87
        \Log::info('[EnrichDocuments] Job complete.');
88
    }
89
90
    /**
91
     * Execute the console command.
92
     *
93
     * @param DocumentsIndex $docIndex
94
     */
95
    public function handle(DocumentsIndex $docIndex)
96
    {
97
        $formatter = new \Monolog\Formatter\LineFormatter(null, null, null, true);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
        $handler = new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG);
99
        $handler->setFormatter($formatter);
100
        $monolog = \Log::getMonolog();
101
        $monolog->pushHandler($handler);
102
103
        $this->docIndex = $docIndex;
104
        $serviceName = $this->argument('service');
105
        $force = $this->option('force');
106
        $verbose = $this->option('verbose');
107
        $collectionId = intval($this->option('collection'));
108
109
        if (!isset($this->services[$serviceName])) {
110
            $this->error('Service "' . $serviceName . '" is not defined. Available servies: "' . implode('", "', array_keys($this->services)) . '".');
111
            return;
112
        }
113
114
        if ($verbose) {
115
            \DB::listen(function ($query, $bindings) {
116
                $this->comment('Query: ' . $query . '. Bindings: ' . implode(', ', $bindings));
117
            });
118
        }
119
120
        $collectionHelp = ($collectionId > 0) ? ' in collection ' . $collectionId : '';
121
122
        $docs = $this->getDocumentsToBeChecked($serviceName, $force, $collectionId);
0 ignored issues
show
Documentation introduced by
$force is of type string|array|null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
124
        if (!count($docs)) {
125
            $this->info('No new documents' . $collectionHelp . '. Exiting.');
126
            \Log::info('[EnrichDocuments] No new documents to be checked.');
127
            return;
128
        }
129
130
        $service = $this->getLaravel()->make($this->services[$serviceName]);
131
        $this->handleDocuments($service, $docs);
132
    }
133
134
}
135