Completed
Push — master ( 65771d...3cac76 )
by Piotr
02:30
created

OptionsValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
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
        return true;
45
    }
46
47
    private function validateConcurrency(int $value) : void
48
    {
49
        Expect::that($value)->isInt()->isGreaterThan(1);
50
    }
51
52
    private function validateTimeout(float $value) : void
53
    {
54
        Expect::that($value)->isFloat()->isGreaterThan(0);
55
    }
56
57
    private function validateCachePool(?CacheItemPoolInterface $value) : void
58
    {
59
        Expect::that($value)->isNullOrInstanceOf('Psr\Cache\CacheItemPoolInterface');
60
    }
61
62
    private function validateLogger(?LoggerInterface $value) : void
63
    {
64
        Expect::that($value)->isNullOrInstanceOf('sr\Log\LoggerInterface');
65
    }
66
67
    private function validateBaseUri(string $value) : void
68
    {
69
        Expect::that($value)->isString();
70
    }
71
72
    private function validateOnReject($value) : void
73
    {
74
        if (is_string($this->config['on_reject']) && !in_array($this->config['on_reject'], [
75
                EsiTentacles::ON_REJECT_EXCEPTION,
76
                EsiTentacles::ON_REJECT_EMPTY
77
            ])) {
78
            throw new InvalidOptionValueException(
79
                'Invalid on_reject option, valid values: '.
80
                EsiTentacles::ON_REJECT_EXCEPTION.', '.EsiTentacles::ON_REJECT_EMPTY.' or Closure'
81
            );
82
        } else {
83
            Expect::that($value)->isInstanceOf('Closure');
84
        }
85
    }
86
87
    private function validateOnTimeout(string $value) : void
88
    {
89
        Expect::that($value)->isString()->isNotEmpty();
90
        if (!in_array($value, [EsiTentacles::ON_TIMEOUT_EXCEPTION, EsiTentacles::ON_TIMEOUT_H_INCLUDE])) {
91
            throw new InvalidOptionValueException(
92
                'Invalid on_reject option, valid values: '.
93
                EsiTentacles::ON_TIMEOUT_EXCEPTION.', '.EsiTentacles::ON_TIMEOUT_H_INCLUDE
94
            );
95
        }
96
    }
97
}