OaiPmhHarvest::handle()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.4266
c 0
b 0
f 0
cc 7
nc 6
nop 0
1
<?php
2
3
namespace Colligator\Console\Commands;
4
5
use Carbon\Carbon;
6
use Colligator\Jobs\OaiPmhHarvest as OaiPmhHarvestJob;
7
use Illuminate\Console\Command;
8
use Illuminate\Foundation\Bus\DispatchesJobs;
9
10
class OaiPmhHarvest extends Command
11
{
12
    use DispatchesJobs;
13
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'colligator:harvest-oaipmh
20
                            {name?       : Name of the harvest config as defined in configs/oaipmh.php}
21
                            {--from=     : Start date on ISO format YYYY-MM-DD}
22
                            {--until=    : End date on ISO format YYYY-MM-DD}
23
                            {--resume=   : Resumption token}
24
                            {--daily     : Harvest records modified yesterday. Cannot be combined with --from / --until}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Harvest records from OAI-PMH service and store as XML files.';
32
33
    /**
34
     * Create a new command instance.
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Output a list of the configurations.
43
     */
44
    public function listConfigurations()
45
    {
46
        $this->comment('');
47
        $this->comment('Available configurations:');
48
        $config = \Config::get('oaipmh.harvests', null);
49
        foreach (array_keys($config) as $key) {
50
            $this->comment(' - ' . $key);
51
        }
52
    }
53
54
    public function validate()
55
    {
56
        if (empty($this->argument('name'))) {
57
            $this->listConfigurations();
58
59
            return false;
60
        }
61
        $harvestConfig = \Config::get('oaipmh.harvests.' . $this->argument('name'), null);
62
        if (is_null($harvestConfig)) {
63
            $this->error('Unknown configuration specified.');
64
            $this->listConfigurations();
65
66
            return false;
67
        }
68
        if ($this->option('daily')) {
69
            if ($this->option('from') || $this->option('until')) {
70
                $this->error('--daily cannot be combined with --from / --until.');
71
72
                return false;
73
            }
74
        }
75 View Code Duplication
        if ($this->option('from')) {
76
            if (!preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $this->option('from'))) {
77
                $this->error('--from must be on ISO-format YYYY-MM-DD.');
78
79
                return false;
80
            }
81
        }
82 View Code Duplication
        if ($this->option('until')) {
83
            if (!preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $this->option('until'))) {
84
                $this->error('--until must be on ISO-format YYYY-MM-DD.');
85
86
                return false;
87
            }
88
        }
89
90
        return true;
91
    }
92
93
    /**
94
     * Execute the console command.
95
     */
96
    public function handle()
97
    {
98
        if (!$this->validate()) {
99
            return;
100
        }
101
102
        $harvestName = $this->argument('name');
103
        $harvestConfig = \Config::get('oaipmh.harvests.' . $harvestName, null);
104
105
        $this->comment(sprintf('Dispatching new harvest job'));
106
107
        $this->comment(' - Repo: ' . $harvestConfig['url']);
108
        $this->comment(' - Schema: ' . $harvestConfig['schema']);
109
        $this->comment(' - Set: ' . $harvestConfig['set']);
110
111
        if ($this->option('daily')) {
112
            $from = Carbon::now()->setTime(0,0,0)->subDay();
113
            $until = Carbon::now()->setTime(0,0,0);
114
        } else {
115
            $from = $this->option('from') ? Carbon::parse($this->option('from')) : null;
116
            $until = $this->option('until') ? Carbon::parse($this->option('until')) : null;
117
        }
118
        $this->comment(' - From: ' . ($from ? $from->toDateTimeString() : '(unspecified)'));
119
        $this->comment(' - Until: ' . ($until ? $until->toDateTimeString() : '(unspecified)'));
120
121
        $this->dispatch(
122
            new OaiPmhHarvestJob(
123
                $harvestName,
0 ignored issues
show
Bug introduced by
It seems like $harvestName defined by $this->argument('name') on line 102 can also be of type array or null; however, Colligator\Jobs\OaiPmhHarvest::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
124
                $harvestConfig,
125
                $from,
126
                $until,
127
                $this->option('resume')
128
            )
129
        );
130
    }
131
}
132