Completed
Branch dev (93c9bf)
by Raffael
04:08
created

SchedulerValidator::validateOptions()   C

Complexity

Conditions 14
Paths 16

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 14.129

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 37
ccs 21
cts 23
cp 0.913
rs 6.2666
c 0
b 0
f 0
cc 14
nc 16
nop 1
crap 14.129

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
declare(strict_types=1);
4
5
/**
6
 * TaskScheduler
7
 *
8
 * @author      Raffael Sahli <[email protected]>
9
 * @copyright   Copryright (c) 2017-2018 gyselroth GmbH (https://gyselroth.com)
10
 * @license     MIT https://opensource.org/licenses/MIT
11
 */
12
13
namespace TaskScheduler;
14
15
use MongoDB\BSON\ObjectId;
16
use MongoDB\BSON\UTCDateTime;
17
use TaskScheduler\Exception\InvalidArgumentException;
18
19
class SchedulerValidator
20
{
21
    /**
22
     * Validate given job options.
23
     */
24 46
    public static function validateOptions(array $options): array
25
    {
26 46
        foreach ($options as $option => $value) {
27
            switch ($option) {
28 46
                case Scheduler::OPTION_AT:
29 46
                    if (!is_int($value) && !($value instanceof UTCDateTime)) {
30
                        throw new InvalidArgumentException('option '.$option.' must be an integer or an instance of '.UTCDateTime::class);
31
                    }
32
33 46
                break;
34 46
                case Scheduler::OPTION_INTERVAL:
35 46
                case Scheduler::OPTION_RETRY:
36 45
                case Scheduler::OPTION_RETRY_INTERVAL:
37 45
                case Scheduler::OPTION_TIMEOUT:
38 46
                    if (!is_int($value)) {
39 1
                        throw new InvalidArgumentException('option '.$option.' must be an integer');
40
                    }
41
42 46
                break;
43 45
                case Scheduler::OPTION_IGNORE_MAX_CHILDREN:
44 45
                    if (!is_bool($value)) {
45
                        throw new InvalidArgumentException('option '.$option.' must be a boolean');
46
                    }
47
48 45
                break;
49 5
                case Scheduler::OPTION_ID:
50 4
                    if (!$value instanceof ObjectId) {
51 1
                        throw new InvalidArgumentException('option '.$option.' must be a an instance of '.ObjectId::class);
52
                    }
53
54 3
                break;
55
                default:
56 46
                    throw new InvalidArgumentException('invalid option '.$option.' given');
57
            }
58
        }
59
60 43
        return $options;
61
    }
62
}
63