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

SchedulerValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 14
eloc 25
dl 0
loc 42
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C validateOptions() 0 37 14
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