Command::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
namespace Bookdown\Bookdown;
3
4
use Psr\Log\LoggerInterface;
5
use Aura\Cli\Context;
6
use Bookdown\Bookdown\Service\Service;
7
use Exception as AnyException;
8
9
class Command
10
{
11
    protected $context;
12
    protected $logger;
13
    protected $service;
14
15 3
    public function __construct(
16
        Context $context,
17
        LoggerInterface $logger,
18
        Service $service
19
    ) {
20 3
        $this->context = $context;
21 3
        $this->logger = $logger;
22 3
        $this->service = $service;
23 3
    }
24
25 2
    public function __invoke()
26
    {
27
        try {
28 2
            list($rootConfigFile, $rootConfigOverrides) = $this->init();
29 1
            $this->service->__invoke($rootConfigFile, $rootConfigOverrides);
30 1
            return 0;
31 1
        } catch (AnyException $e) {
32 1
            $this->logger->error($e->getMessage());
33 1
            $code = $e->getCode() ? $e->getCode() : 1;
34 1
            return $code;
35
        }
36
    }
37
38 2
    protected function init()
39
    {
40 2
        $getopt = $this->context->getopt(array(
41 2
            'template:',
42 2
            'target:',
43
            'root-href:'
44 2
        ));
45
46 2
        if ($getopt->hasErrors()) {
47
            $errors = $getopt->getErrors();
48
            $error = array_shift($errors);
49
            throw $error;
50
        }
51
52 2
        $rootConfigFile = $getopt->get(1);
53 2
        if (! $rootConfigFile) {
54 1
            throw new Exception(
55
                "Please enter the path to a bookdown.json file as the first argument."
56 1
            );
57
        }
58
59 1
        $rootConfigOverrides = $getopt->get();
60 1
        return array($rootConfigFile, $rootConfigOverrides);
61
    }
62
}
63