Completed
Push — master ( 57b854...1f9c82 )
by Anton
15:12 queued 13:21
created

CliBootstrap   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 158
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setInput() 0 4 1
A getInput() 0 4 1
A setOutput() 0 4 1
A getOutput() 0 4 1
A initRequest() 0 15 3
A preProcess() 0 5 1
A preDispatch() 0 9 2
A render() 0 18 4
A end() 0 12 2
A sendErrors() 0 6 2
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Application;
12
13
use Application\Users\Table;
14
use Bluz\Application\Application;
15
use Bluz\Controller\Controller;
16
use Bluz\Proxy\Auth;
17
use Bluz\Proxy\Logger;
18
use Bluz\Proxy\Request;
19
use Bluz\Proxy\Response;
20
use Bluz\Proxy\Router;
21
use Bluz\Request\RequestFactory;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Style\SymfonyStyle;
25
26
/**
27
 * Bootstrap for CLI
28
 *
29
 * @category Application
30
 * @package  Bootstrap
31
 *
32
 * @author   Anton Shevchuk
33
 * @created  17.12.12 15:24
34
 */
35
class CliBootstrap extends Application
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
36
{
37
    /**
38
     * Layout flag
39
     *
40
     * @var boolean
41
     */
42
    protected $layoutFlag = false;
43
44
    /**
45
     * @var InputInterface
46
     */
47
    protected $input;
48
49
    /**
50
     * @var OutputInterface
51
     */
52
    protected $output;
53
54
    /**
55
     * @param InputInterface $input
56
     */
57
    public function setInput(InputInterface $input): void
58
    {
59
        $this->input = $input;
60
    }
61
62
    /**
63
     * @return InputInterface
64
     */
65
    public function getInput()
66
    {
67
        return $this->input;
68
    }
69
70
    /**
71
     * @param OutputInterface $output
72
     */
73
    public function setOutput(OutputInterface $output): void
74
    {
75
        $this->output = $output;
76
    }
77
78
    /**
79
     * @return OutputInterface
80
     */
81
    public function getOutput()
82
    {
83
        return $this->output;
84
    }
85
86
    /**
87
     * get CLI Request
88
     *
89
     * @return void
90
     * @throws \InvalidArgumentException
91
     */
92
    public function initRequest(): void
93
    {
94
        $uri = $this->getInput()->getArgument('uri');
95
96
        $parsedQuery = parse_url($uri, PHP_URL_QUERY);
97
        if (false !== $parsedQuery && null !== $parsedQuery) {
98
            parse_str($parsedQuery, $query);
99
        } else {
100
            $query = [];
101
        }
102
103
        $request = RequestFactory::fromGlobals(['REQUEST_URI' => $uri, 'REQUEST_METHOD' => 'CLI'], $query);
104
105
        Request::setInstance($request);
106
    }
107
108
    /**
109
     * Pre process
110
     *
111
     * @return void
112
     */
113
    protected function preProcess(): void
114
    {
115
        Router::process();
116
        Response::setType('CLI');
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     *
122
     * @param Controller $controller
123
     *
124
     * @return void
125
     * @throws \Bluz\Db\Exception\DbException
126
     */
127
    protected function preDispatch($controller): void
128
    {
129
        // auth as CLI user
130
        if ($cliUser = Table::findRowWhere(['login' => 'system'])) {
131
            Auth::setIdentity($cliUser);
132
        }
133
134
        parent::preDispatch($controller);
135
    }
136
137
    /**
138
     * Render, is send Response
139
     *
140
     * @return void
141
     */
142
    public function render(): void
143
    {
144
        $io = new SymfonyStyle($this->getInput(), $this->getOutput());
145
        $io->title('Bluz CLI');
146
147
        if ($params = Request::getParams()) {
148
            foreach ($params as $key => $value) {
149
                $key = \is_int($key) ? "<comment>$key</comment>" : "<info>$key</info>";
150
                $io->writeln("$key: $value");
151
            }
152
        }
153
154
        $io->writeln('');
155
        $io->writeln('========');
156
        $io->writeln('');
157
        $io->writeln(json_encode(Response::getBody()));
158
        $io->writeln('');
159
    }
160
161
    /**
162
     * Finish it
163
     *
164
     * @return void
165
     */
166
    public function end(): void
167
    {
168
        if ($errors = Logger::get('error')) {
169
            $this->sendErrors($errors);
170
        }
171
        // return code 1 for invalid behaviour of application
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
172
//        if ($exception = $this->getException()) {
173
//            echo $exception->getMessage();
174
//            exit(1);
175
//        }
176
        exit;
177
    }
178
179
    /**
180
     * Send errors to logger
181
     *
182
     * @param  array $errors
183
     *
184
     * @return void
185
     */
186
    protected function sendErrors($errors): void
187
    {
188
        foreach ($errors as $message) {
189
            errorLog(new \ErrorException($message, 0, E_USER_ERROR));
190
        }
191
    }
192
}
193