Completed
Push — master ( 4586d8...9f2196 )
by Neomerx
02:14
created

LimoncelloCommand::execute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 5
nop 2
crap 4
1
<?php namespace Limoncello\Commands;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Composer\Command\BaseCommand;
20
use Exception;
21
use Limoncello\Commands\Traits\CommandSerializationTrait;
22
use Limoncello\Commands\Traits\CommandTrait;
23
use Limoncello\Commands\Wrappers\DataArgumentWrapper;
24
use Limoncello\Commands\Wrappers\DataOptionWrapper;
25
use Limoncello\Contracts\Exceptions\ThrowableHandlerInterface;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Output\OutputInterface;
28
29
/**
30
 * @package Limoncello\Commands
31
 */
32
class LimoncelloCommand extends BaseCommand
33
{
34
    use CommandTrait, CommandSerializationTrait;
35
36
    /**
37
     * @var string
38
     */
39
    private $description;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
40
41
    /**
42
     * @var string
43
     */
44
    private $help;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
45
46
    /**
47
     * @var array
48
     */
49
    private $arguments;
50
51
    /**
52
     * @var array
53
     */
54
    private $options;
55
56
    /**
57
     * @var callable|array
58
     */
59
    private $callable;
60
61
    /**
62
     * @param string $name
63
     * @param string $description
64
     * @param string $help
65
     * @param array  $arguments
66
     * @param array  $options
67
     * @param array  $callable
68
     */
69 4
    public function __construct(
70
        string $name,
71
        string $description,
72
        string $help,
73
        array $arguments,
74
        array $options,
75
        array $callable
76
    ) {
77 4
        $this->description = $description;
78 4
        $this->help        = $help;
79 4
        $this->arguments   = $arguments;
80 4
        $this->options     = $options;
81 4
        $this->callable    = $callable;
82
83
        // it is important to call the parent constructor after
84
        // data init as it calls `configure` method.
85 4
        parent::__construct($name);
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 4
    public function configure()
92
    {
93 4
        parent::configure();
94
95
        $this
96 4
            ->setDescription($this->description)
97 4
            ->setHelp($this->help);
98
99 4
        foreach ($this->arguments as $data) {
100 3
            $arg = new DataArgumentWrapper($data);
101 3
            $this->addArgument($arg->getName(), $arg->getMode(), $arg->getDescription(), $arg->getDefault());
102
        }
103
104 4
        foreach ($this->options as $data) {
105 3
            $opt = new DataOptionWrapper($data);
106 3
            $this->addOption(
107 3
                $opt->getName(),
108 3
                $opt->getShortcut(),
109 3
                $opt->getMode(),
110 3
                $opt->getDescription(),
111 3
                $opt->getDefault()
112
            );
113
        }
114
    }
115
116
    /** @noinspection PhpMissingParentCallCommonInspection
117
     * @inheritdoc
118
     */
119 3
    public function execute(InputInterface $input, OutputInterface $output)
120
    {
121 3
        $container =  null;
122
123
        try {
124 3
            $container = $this->createContainer($this->getComposer(), $this->getName());
0 ignored issues
show
Bug introduced by
It seems like $this->getComposer() can be null; however, createContainer() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
125 2
            call_user_func($this->callable, $container, $this->wrapIo($input, $output));
126 2
        } catch (Exception $exception) {
127 2
            if ($container !== null && $container->has(ThrowableHandlerInterface::class) === true) {
128
                /** @var ThrowableHandlerInterface $handler */
129 1
                $handler  = $container->get(ThrowableHandlerInterface::class);
130 1
                $response = $handler->createResponse($exception, $container);
131
132 1
                $output->writeln((string)$response->getBody());
133
            } else {
134 1
                $message = $exception->getMessage();
135 1
                $file    = $exception->getFile();
136 1
                $line    = $exception->getLine();
137 1
                $trace   = $exception->getTraceAsString();
138
139 1
                $output->writeln("$message at $file#$line" . PHP_EOL . $trace);
140
            }
141
142 2
            throw $exception;
143
        }
144
    }
145
}
146