Passed
Branch dev (7112df)
by Raffael
03:08
created

SchedulerValidator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
eloc 23
dl 0
loc 38
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C validateOptions() 0 33 13
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 TaskScheduler\Exception\InvalidArgumentException;
17
18
class SchedulerValidator
19
{
20
    /**
21
     * Validate given job options.
22
     */
23 50
    public static function validateOptions(array $options): array
24
    {
25 50
        foreach ($options as $option => $value) {
26
            switch ($option) {
27 50
                case Scheduler::OPTION_AT:
28 50
                case Scheduler::OPTION_INTERVAL:
29 50
                case Scheduler::OPTION_RETRY:
30 49
                case Scheduler::OPTION_RETRY_INTERVAL:
31 49
                case Scheduler::OPTION_TIMEOUT:
32 50
                    if (!is_int($value)) {
33 1
                        throw new InvalidArgumentException('option '.$option.' must be an integer');
34
                    }
35
36 50
                break;
37 49
                case Scheduler::OPTION_IGNORE_DATA:
38 49
                case Scheduler::OPTION_IGNORE_MAX_CHILDREN:
39 49
                    if (!is_bool($value)) {
40 1
                        throw new InvalidArgumentException('option '.$option.' must be a boolean');
41
                    }
42
43 49
                break;
44 5
                case Scheduler::OPTION_ID:
45 4
                    if (!$value instanceof ObjectId) {
46 1
                        throw new InvalidArgumentException('option '.$option.' must be a an instance of '.ObjectId::class);
47
                    }
48
49 3
                break;
50
                default:
51 50
                    throw new InvalidArgumentException('invalid option '.$option.' given');
52
            }
53
        }
54
55 46
        return $options;
56
    }
57
}
58