Passed
Push — master ( eb7214...99cc8c )
by Alec
02:56 queued 10s
created

Sentinel::assertOutput()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

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