Completed
Pull Request — master (#9)
by Jakub
03:53
created

RunnerFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
eloc 19
wmc 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createRealRunner() 0 7 2
A __construct() 0 3 1
targetDir() 0 14 ?
A hp$0 ➔ targetDir() 0 14 3
A createRunner() 0 9 2
A parameters() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Cli\ServiceContainer;
4
5
use Psr\Container\ContainerExceptionInterface;
6
use Psr\Container\ContainerInterface;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Zalas\Toolbox\Cli\Runner\DryRunner;
10
use Zalas\Toolbox\Runner\ParametrisedRunner;
11
use Zalas\Toolbox\Runner\PassthruRunner;
12
use Zalas\Toolbox\Runner\Runner;
13
14
class RunnerFactory
15
{
16
    private $container;
17
18 10
    public function __construct(ContainerInterface $container)
19
    {
20 10
        $this->container = $container;
21
    }
22
23 7
    public function createRunner(): Runner
24
    {
25 7
        $runner = $this->createRealRunner();
26
27 7
        if ($parameters = $this->parameters()) {
28 4
            return new ParametrisedRunner($runner, $parameters);
29
        }
30
31 2
        return $runner;
32
    }
33
34
    /**
35
     * @return DryRunner|PassthruRunner
36
     */
37 7
    private function createRealRunner()
38
    {
39 7
        if ($this->container->get(InputInterface::class)->getOption('dry-run')) {
40 4
            return new DryRunner($this->container->get(OutputInterface::class));
41
        }
42
43 3
        return new PassthruRunner();
44
    }
45
46 7
    private function parameters(): array
47
    {
48 7
        if ($targetDir = $this->targetDir()) {
49 4
            return ['%target-dir%' => $targetDir];
50
        }
51
52 2
        return [];
53
    }
54
55 7
    private function targetDir(): ?string
56
    {
57 7
        if (!$this->container->get(InputInterface::class)->hasOption('target-dir')) {
58 2
            return null;
59
        }
60
61 5
        $targetDir = $this->container->get(InputInterface::class)->getOption('target-dir');
62
63 5
        if (!\is_dir($targetDir)) {
64
            throw new class(\sprintf('The target dir does not exist: "%s".', $targetDir)) extends \RuntimeException implements ContainerExceptionInterface {
65
            };
66
        }
67
68 4
        return \realpath($targetDir);
69
    }
70
}
71