Completed
Push — master ( 27d687...150b55 )
by Dan Michael O.
12:08
created

OaiPmhHarvest::validate()   D

Complexity

Conditions 10
Paths 17

Size

Total Lines 38
Code Lines 22

Duplication

Lines 14
Ratio 36.84 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 38
rs 4.8196
cc 10
eloc 22
nc 17
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        foreach (['from', 'until', 'resume', 'daily'] as $key) {
112
            if (!is_null($this->option($key))) {
113
                $this->comment(sprintf(' - %s: %s', ucfirst($key), $this->option($key)));
114
            }
115
        }
116
117
        $from = $this->option('from');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $from is correct as $this->option('from') (which targets Illuminate\Console\Command::option()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
118
        $until = $this->option('until');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $until is correct as $this->option('until') (which targets Illuminate\Console\Command::option()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
119
        if ($this->option('daily')) {
120
            $from = Carbon::now()->subDay()->toDateString();
121
            $until = $from;
122
        }
123
124
        $this->dispatch(
125
            new OaiPmhHarvestJob(
126
                $harvestName,
127
                $harvestConfig,
128
                $from,
129
                $until,
130
                $this->option('resume')
131
            )
132
        );
133
    }
134
}
135