Passed
Push — master ( e2bdc6...71496d )
by Alec
03:05
created

SpinnerCore::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 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6111
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\Adapters\EchoOutputAdapter;
6
use AlecRabbit\Spinner\Core\Contracts\Frames;
7
use AlecRabbit\Spinner\Core\Contracts\SpinnerInterface;
8
use AlecRabbit\Spinner\Core\Contracts\SpinnerOutputInterface;
9
use AlecRabbit\Spinner\Core\Contracts\StylesInterface;
10
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
11
use function AlecRabbit\typeOf;
12
13
abstract class SpinnerCore implements SpinnerInterface
14
{
15
    protected const EMPTY_STRING = Defaults::EMPTY_STRING;
16
17
    protected const INTERVAL = Defaults::DEFAULT_INTERVAL;
18
    protected const FRAMES = Frames::BASE;
19
    protected const STYLES = StylesInterface::STYLING_DISABLED;
20
21
    /** @var null|SpinnerOutputInterface */
22
    protected $output;
23
24
    /**
25
     * @param null|false|SpinnerOutputInterface $output
26
     * @return null|SpinnerOutputInterface
27
     */
28 29
    protected function refineOutput($output): ?SpinnerOutputInterface
29
    {
30 29
        $this->assertOutput($output);
31 27
        if (false === $output) {
32 22
            return null;
33
        }
34 5
        return $output ?? new EchoOutputAdapter();
35
    }
36
37
    /**
38
     * @param mixed $output
39
     */
40 29
    protected function assertOutput($output): void
41
    {
42 29
        if (null !== $output && false !== $output && !$output instanceof SpinnerOutputInterface) {
43
            $typeOrValue =
44 2
                true === $output ? 'true' : typeOf($output);
45 2
            throw new \InvalidArgumentException(
46
                'Incorrect parameter: ' .
47
                '[null|false|' . SpinnerOutputInterface::class . '] expected'
48 2
                . ' "' . $typeOrValue . '" given.'
49
            );
50
        }
51 27
    }
52
}
53