CommandCollection::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 9
cp 0.6667
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.1481
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Console;
4
5
use ArrayIterator;
6
use InvalidArgumentException;
7
use Symfony\Component\Console\Command\Command;
8
use Venta\Contracts\Console\CommandCollection as CommandCollectionContract;
9
10
/**
11
 * Class CommandCollection
12
 *
13
 * @package Venta\Console
14
 */
15
final class CommandCollection implements CommandCollectionContract
16
{
17
    /**
18
     * @var string[]
19
     */
20
    private $commands = [];
21
22
    /**
23
     * @inheritDoc
24
     */
25 2
    public function add(string $commandClass)
26
    {
27 2
        if (!is_subclass_of($commandClass, Command::class)) {
28 1
            throw new InvalidArgumentException(
29 1
                sprintf('Provided command "%s" is not subclass of "%s" class.', $commandClass, Command::class)
30
            );
31
        }
32 1
        $this->commands[] = $commandClass;
33 1
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38 1
    public function all(): array
39
    {
40 1
        return array_unique($this->commands);
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function getIterator()
47
    {
48
        return new ArrayIterator($this->all());
49
    }
50
}