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

SpinnerCore   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
c 1
b 0
f 0
dl 0
loc 36
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assertOutput() 0 9 5
A refineOutput() 0 7 2
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