Completed
Push — master ( 77e005...3b85e8 )
by Anton
15s
created

CliBootstrap::sendErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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\Config;
18
use Bluz\Proxy\Logger;
19
use Bluz\Proxy\Request;
20
use Bluz\Proxy\Response;
21
use Bluz\Proxy\Router;
22
use Bluz\Request\RequestFactory;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
27
/**
28
 * Bootstrap for CLI
29
 *
30
 * @category Application
31
 * @package  Bootstrap
32
 *
33
 * @author   Anton Shevchuk
34
 * @created  17.12.12 15:24
35
 */
36
class CliBootstrap extends Application
37
{
38
    /**
39
     * Layout flag
40
     *
41
     * @var boolean
42
     */
43
    protected $layoutFlag = false;
44
45
    /**
46
     * @var InputInterface
47
     */
48
    protected $input;
49
50
    /**
51
     * @var OutputInterface
52
     */
53
    protected $output;
54
55
    /**
56
     * @param InputInterface $input
57
     */
58
    public function setInput(InputInterface $input)
59
    {
60
        $this->input = $input;
61
    }
62
63
    /**
64
     * @return InputInterface
65
     */
66
    public function getInput()
67
    {
68
        return $this->input;
69
    }
70
71
    /**
72
     * @param OutputInterface $output
73
     */
74
    public function setOutput(OutputInterface $output)
75
    {
76
        $this->output = $output;
77
    }
78
79
    /**
80
     * @return OutputInterface
81
     */
82
    public function getOutput()
83
    {
84
        return $this->output;
85
    }
86
87
    /**
88
     * get CLI Request
89
     *
90
     * @return void
91
     * @throws \Application\Exception
92
     * @throws \InvalidArgumentException
93
     */
94
    public function initRequest()
95
    {
96
        $uri = $this->getInput()->getArgument('uri');
97
98
        $parsedQuery = parse_url($uri, PHP_URL_QUERY);
99
        if (false !== $parsedQuery) {
100
            parse_str($parsedQuery, $query);
101
        } else {
102
            $query = [];
103
        }
104
105
        $request = RequestFactory::fromGlobals(['REQUEST_URI' => $uri, 'REQUEST_METHOD' => 'CLI'], $query);
106
107
        Request::setInstance($request);
108
    }
109
110
    /**
111
     * initConfig
112
     *
113
     * @return void
114
     * @throws \Bluz\Config\ConfigException
115
     */
116
    public function initConfig()
117
    {
118
        $config = new \Bluz\Config\Config();
119
        $config->setPath(self::getInstance()->getPath());
120
        $config->setEnvironment(self::getInstance()->getEnvironment());
121
        $config->init();
122
123
        Config::setInstance($config);
124
125
        parent::initConfig();
126
    }
127
128
    /**
129
     * Pre process
130
     *
131
     * @return void
132
     */
133
    protected function preProcess()
134
    {
135
        Router::process();
136
        Response::setType('CLI');
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     *
142
     * @param Controller $controller
143
     *
144
     * @return void
145
     */
146
    protected function preDispatch($controller)
147
    {
148
        // auth as CLI user
149
        $cliUser = Table::findRowWhere(['login' => 'system']);
150
        Auth::setIdentity($cliUser);
151
152
        parent::preDispatch($controller);
153
    }
154
155
    /**
156
     * Render, is send Response
157
     *
158
     * @return void
159
     */
160
    public function render()
161
    {
162
        $io = new SymfonyStyle($this->getInput(), $this->getOutput());
163
        $io->title('Bluz CLI');
164
165
        if ($params = Request::getParams()) {
166
            foreach ($params as $key => $value) {
167
                $key = is_int($key) ? "<comment>$key</comment>" : "<info>$key</info>";
168
                $io->writeln("$key: $value");
169
            }
170
        }
171
172
        $io->writeln('');
173
        $io->writeln('========');
174
        $io->writeln('');
175
176
        $data = Response::getBody()->getData()->toArray();
177
178
        foreach ($data as $key => $value) {
179
            $io->writeln("<info>$key</info>: $value");
180
        }
181
182
        $io->writeln('');
183
    }
184
185
    /**
186
     * Finish it
187
     *
188
     * @return void
189
     */
190
    public function end()
191
    {
192
        if ($errors = Logger::get('error')) {
193
            $this->sendErrors($errors);
194
        }
195
        // 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...
196
//        if ($exception = $this->getException()) {
197
//            echo $exception->getMessage();
198
//            exit(1);
199
//        }
200
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method end() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
201
    }
202
203
    /**
204
     * sendErrorBody
205
     *
206
     * @param  array $errors
207
     *
208
     * @return void
209
     */
210
    protected function sendErrors($errors)
211
    {
212
        foreach ($errors as $message) {
213
            errorLog(new \ErrorException($message, 0, E_USER_ERROR));
214
        }
215
    }
216
}
217