HarvestXisbn   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 80
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDocuments() 0 4 1
A handleDocuments() 0 21 3
A handle() 0 10 2
1
<?php
2
3
namespace Colligator\Console\Commands;
4
5
use Colligator\Document;
6
use Colligator\XisbnClient;
7
use Illuminate\Console\Command;
8
use Log;
9
10
class HarvestXisbn extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'colligator:harvest-xisbn';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Adds additional isbn numbers from the xisbn service.';
25
26
    /**
27
     * Sleep time in seconds between requests.
28
     *
29
     * @var int
30
     */
31
    public $sleepTime = 5;
32
33
    /**
34
     * Create a new command instance.
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    public function getDocuments()
42
    {
43
        return Document::whereNull('xisbn')->get();
44
    }
45
46
    /**
47
     * @param XisbnClient $client
48
     * @param Document[]  $docs
49
     */
50
    public function handleDocuments(XisbnClient $client, $docs)
51
    {
52
        $this->info('Will check ' . count($docs) . ' documents');
53
        Log::info('[HarvestXisbnJob] Starting job. ' . count($docs) . ' documents to be checked.');
54
55
        $this->output->progressStart(count($docs));
56
        foreach ($docs as $doc) {
57
            $response = $client->checkIsbns($doc->bibliographic['isbns']);
58
            if ($response->overLimit()) {
59
                $this->error('Reached daily limit. Aborting.');
60
                break;
61
            }
62
            $doc->xisbn = $response->toArray();
63
            $doc->save();
64
65
            $this->output->progressAdvance();
66
            sleep($this->sleepTime);
67
        }
68
        $this->output->progressFinish();
69
        Log::info('[HarvestXisbnJob] Complete.');
70
    }
71
72
    /**
73
     * Execute the console command.
74
     *
75
     * @param XisbnClient $client
76
     *
77
     * @return void
78
     */
79
    public function handle(XisbnClient $client)
80
    {
81
        $docs = $this->getDocuments();
82
        if (count($docs)) {
83
            $this->handleDocuments($client, $docs);
84
        } else {
85
            $this->info('No new documents. Exiting.');
86
            Log::info('[HarvestXisbnJob] No new documents to be checked.');
87
        }
88
    }
89
}
90