CommandFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace N1215\CakeCandle\Console;
5
6
use Cake\Console\CommandFactoryInterface;
7
use Cake\Console\CommandInterface;
8
use Cake\Console\Shell;
9
use InvalidArgumentException;
10
use N1215\CakeCandle\ContainerBagLocator;
11
12
/**
13
 * This is a factory for creating Command and Shell instances.
14
 */
15
final class CommandFactory implements CommandFactoryInterface
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20 4
    public function create($className)
21
    {
22 4
        $command = ContainerBagLocator::get()->get($className);
23 4
        if (!($command instanceof CommandInterface) && !($command instanceof Shell)) {
24
            /** @psalm-suppress DeprecatedClass */
25 2
            $valid = implode('` or `', [Shell::class, CommandInterface::class]);
26 2
            $message = sprintf('Class `%s` must be an instance of `%s`.', $className, $valid);
27 2
            throw new InvalidArgumentException($message);
28
        }
29
30 2
        return $command;
31
    }
32
}
33