Completed
Push — master ( fb073d...0ff0ab )
by Piotr
03:24
created

OptionsValidator::validateOnReject()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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