Passed
Push — master ( 99cc8c...a83aa0 )
by Alec
02:13
created

Sentinel::assertSettings()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core;
4
5
use AlecRabbit\Spinner\Core\Contracts\OutputInterface;
6
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
7
use AlecRabbit\Spinner\Settings\Settings;
8
use function AlecRabbit\typeOf;
9
10
/**
11
 * Class Sentinel
12
 * Contains data asserts
13
 */
14
class Sentinel
15
{
16
    /**
17
     * @param array $frames
18
     */
19 39
    public static function assertFrames(array $frames): void
20
    {
21 39
        if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) {
22 3
            throw new \InvalidArgumentException(
23 3
                sprintf('MAX_SYMBOLS_COUNT limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count)
24
            );
25
        }
26 36
        foreach ($frames as $frame) {
27 30
            self::assertFrame($frame);
28
        }
29 27
    }
30
31
    /**
32
     * @param mixed $frame
33
     */
34 30
    public static function assertFrame($frame): void
35
    {
36 30
        if (!\is_string($frame)) {
37 7
            throw new \InvalidArgumentException('All frames should be of string type.');
38
        }
39 29
        self::assertFrameLength($frame);
40 29
    }
41
42
    /**
43
     * @param array $styles
44
     * @param array $against
45
     */
46 27
    public static function assertStyles(array $styles, array $against): void
47
    {
48 27
        $keys = array_keys($against);
49 27
        foreach ($keys as $index) {
50 27
            if (!\array_key_exists($index, $styles)) {
51
                // @codeCoverageIgnoreStart
52
                throw new \InvalidArgumentException(
53
                    'Styles array does not have [' . $index . '] key.'
54
                );
55
                // @codeCoverageIgnoreEnd
56
            }
57
        }
58 27
    }
59
60
    /**
61
     * @param mixed $output
62
     */
63 29
    public static function assertOutput($output): void
64
    {
65 29
        if (null !== $output && false !== $output && !$output instanceof OutputInterface) {
66
            $typeOrValue =
67 2
                true === $output ? 'true' : typeOf($output);
68 2
            throw new \InvalidArgumentException(
69
                'Incorrect parameter: ' .
70
                '[null|false|' . OutputInterface::class . '] expected'
71 2
                . ' "' . $typeOrValue . '" given.'
72
            );
73
        }
74 27
    }
75
76
    /**
77
     * @param mixed $settings
78
     */
79 27
    public static function assertSettings($settings): void
80
    {
81 27
        if (null !== $settings && !\is_string($settings) && !$settings instanceof Settings) {
82 1
            throw new \InvalidArgumentException(
83 1
                'Instance of [' . Settings::class . '] or string expected ' . typeOf($settings) . ' given.'
84
            );
85
        }
86 26
    }
87
88
    /**
89
     * @param $frame
90
     */
91 29
    public static function assertFrameLength($frame): void
92
    {
93 29
        if (Defaults::MAX_FRAME_LENGTH < $length = mb_strlen($frame)) {
94 2
            throw new \InvalidArgumentException(
95 2
                sprintf(
96 2
                    'Single frame max length [%s] exceeded [%s]',
97 2
                    Defaults::MAX_FRAME_LENGTH,
98 2
                    $length
99
                )
100
            );
101
        }
102 29
    }
103
}
104