Completed
Push — master ( 12962f...9eb849 )
by Piotr
01:41
created

OptionsValidator::validateRejected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace CrazyGoat\Octophpus\Validator;
4
5
use CrazyGoat\Octophpus\EsiTentacles;
6
use Nunzion\Expect;
7
use Psr\Cache\CacheItemPoolInterface;
8
use Psr\Log\LoggerInterface;
9
10
class OptionsValidator implements ValidatorInterface
11
{
12
    use Camelize;
13
    /**
14
     * @var array
15
     */
16
    private $config;
17
18
    public function __construct(array $config)
19
    {
20
        $this->config = $config;
21
    }
22
23
    /**
24
     * @return bool
25
     * @uses validateConcurrency
26
     * @uses validateTimeout
27
     * @uses validateCachePool
28
     * @uses validateLogger
29
     * @uses validateBaseUri
30
     * @uses validateOnReject
31
     * @uses validateOnTimeout
32
     * @uses validateCachePrefix
33
     * @uses validateRequestOptions
34
     * @uses validateCacheTtl
35
     * @uses validateRecurrenceLevel
36
     * @uses validateFulfilled
37
     * @uses validateRejected
38
     */
39
    public function validate(): bool
40
    {
41
        foreach ($this->config as $key => $value) {
42
            $function = 'validate'.$this->camlize($key);
43
            if (method_exists($this, $function)) {
44
                $this->$function($value);
45
            }
46
        }
47
48
        return true;
49
    }
50
51
    private function validateCacheTtl(int $value)
52
    {
53
        Expect::that($value)->isInt()->isGreaterThan(0);
54
    }
55
56
    private function validateRecurrenceLevel(int $value)
57
    {
58
        Expect::that($value)->isInt()->isGreaterThan(0);
59
    }
60
61
    private function validateFulfilled(\Closure $value): void
62
    {
63
        Expect::that($value)->isInstanceOf(\Closure::class);
64
    }
65
66
    private function validateRejected(\Closure $value): void
67
    {
68
        Expect::that($value)->isInstanceOf(\Closure::class);
69
    }
70
71
    private function validateRequestOptions(array $value)
72
    {
73
        Expect::that($value)->isArray();
74
    }
75
76
    private function validateCachePrefix(string $value) : void
77
    {
78
        Expect::that($value)->isString();
79
    }
80
81
    private function validateConcurrency(int $value) : void
82
    {
83
        Expect::that($value)->isInt()->isGreaterThan(0);
84
    }
85
86
    private function validateTimeout(float $value) : void
87
    {
88
        Expect::that($value)->isFloat()->isGreaterThan(0);
89
    }
90
91
    private function validateCachePool(?CacheItemPoolInterface $value) : void
92
    {
93
        Expect::that($value)->isNullOrInstanceOf('Psr\Cache\CacheItemPoolInterface');
94
    }
95
96
    private function validateLogger(?LoggerInterface $value) : void
97
    {
98
        Expect::that($value)->isNullOrInstanceOf('Psr\Log\LoggerInterface');
99
    }
100
101
    private function validateBaseUri(string $value) : void
102
    {
103
        Expect::that($value)->isString();
104
    }
105
106
    private function validateOnReject($value) : void
107
    {
108
        if ((is_string($this->config['on_reject']) && in_array($this->config['on_reject'], [
109
                EsiTentacles::ON_REJECT_EXCEPTION,
110
                EsiTentacles::ON_REJECT_EMPTY
111
            ])) || $value instanceof \Closure) {
112
            return;
113
        }
114
115
        throw new \InvalidArgumentException(
116
            'Invalid on_reject option, valid values: '.
117
            EsiTentacles::ON_REJECT_EXCEPTION.', '.EsiTentacles::ON_REJECT_EMPTY.' or Closure'
118
        );
119
    }
120
121
    private function validateOnTimeout(string $value) : void
122
    {
123
        Expect::that($value)->isString()->isNotEmpty();
124
        if (!in_array($value, [EsiTentacles::ON_TIMEOUT_EXCEPTION, EsiTentacles::ON_TIMEOUT_H_INCLUDE])) {
125
            throw new \InvalidArgumentException(
126
                'Invalid on_reject option, valid values: '.
127
                EsiTentacles::ON_TIMEOUT_EXCEPTION.', '.EsiTentacles::ON_TIMEOUT_H_INCLUDE
128
            );
129
        }
130
    }
131
}