Passed
Push — master ( 2d5bb4...107b8a )
by Alec
02:55
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\Helpers\wcswidth;
9
use function AlecRabbit\typeOf;
10
11
/**
12
 * Class Sentinel.
13
 * Contains data asserts
14
 * @internal
15
 */
16
class Sentinel
17
{
18
    /**
19
     * @param array $frames
20
     */
21 40
    public static function assertFrames(array $frames): void
22
    {
23 40
        if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) {
24 3
            throw new \InvalidArgumentException(
25 3
                sprintf('MAX_SYMBOLS_COUNT limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count)
26
            );
27
        }
28 37
        foreach ($frames as $frame) {
29 31
            self::assertFrame($frame);
30
        }
31 28
    }
32
33
    /**
34
     * @param mixed $frame
35
     */
36 31
    public static function assertFrame($frame): void
37
    {
38 31
        if (!\is_string($frame)) {
39 7
            throw new \InvalidArgumentException('All frames should be of string type.');
40
        }
41 30
        self::assertFrameWidth($frame);
42 30
    }
43
44
    /**
45
     * @param string $frame
46
     */
47 30
    public static function assertFrameWidth(string $frame): void
48
    {
49 30
        if (Defaults::MAX_FRAME_WIDTH < $w = wcswidth($frame)) {
50 2
            throw new \InvalidArgumentException(
51 2
                sprintf(
52 2
                    'Single frame max width [%s] exceeded [%s]',
53 2
                    Defaults::MAX_FRAME_WIDTH,
54 2
                    $w
55
                )
56
            );
57
        }
58 30
    }
59
60
    /**
61
     * @param array $styles
62
     * @param array $against
63
     */
64 28
    public static function assertStyles(array $styles, array $against): void
65
    {
66 28
        $keys = array_keys($against);
67 28
        foreach ($keys as $index) {
68 28
            if (!\array_key_exists($index, $styles)) {
69
                // @codeCoverageIgnoreStart
70
                throw new \InvalidArgumentException(
71
                    'Styles array does not have [' . $index . '] key.'
72
                );
73
                // @codeCoverageIgnoreEnd
74
            }
75
        }
76 28
    }
77
78
    /**
79
     * @param mixed $output
80
     */
81 30
    public static function assertOutput($output): void
82
    {
83 30
        if (null !== $output && false !== $output && !$output instanceof OutputInterface) {
84
            $typeOrValue =
85 2
                true === $output ? 'true' : typeOf($output);
86 2
            throw new \InvalidArgumentException(
87
                'Incorrect parameter: ' .
88
                '[null|false|' . OutputInterface::class . '] expected'
89 2
                . ' "' . $typeOrValue . '" given.'
90
            );
91
        }
92 28
    }
93
94
    /**
95
     * @param mixed $settings
96
     */
97 28
    public static function assertSettings($settings): void
98
    {
99 28
        if (null !== $settings && !\is_string($settings) && !$settings instanceof Settings) {
100 1
            throw new \InvalidArgumentException(
101 1
                'Instance of [' . Settings::class . '] or string expected ' . typeOf($settings) . ' given.'
102
            );
103
        }
104 27
    }
105
106
    /**
107
     * @param array $order
108
     */
109 3
    public static function assertJugglersOrder(array $order): void
110
    {
111 3
        if (Defaults::NUMBER_OF_ORDER_DIRECTIVES !== $count = count($order)) {
112 1
            throw new \InvalidArgumentException(
113 1
                sprintf(
114 1
                    'Incorrect count of order directives [%s] when exactly %s was expected',
115 1
                    $count,
116 1
                    Defaults::NUMBER_OF_ORDER_DIRECTIVES
117
                )
118
            );
119
        }
120 2
        foreach (Defaults::DEFAULT_ORDER_DIRECTIVES as $directive) {
121 2
            if (!in_array($directive, $order, true)) {
122 1
                throw new \InvalidArgumentException(
123 1
                    sprintf(
124 1
                        'Directive for %s position not found',
125 1
                        Defaults::DIRECTIVES_NAMES[$directive]
126
                    )
127
                );
128
            }
129
        }
130 1
    }
131
}
132