Completed
Push — master ( f8fad3...d6dee8 )
by Anton
9s
created

CliBootstrap::finish()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 15
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
/**
10
 * @namespace
11
 */
12
namespace Application;
13
14
use Application\Users\Table;
15
use Bluz\Application\Application;
16
use Bluz\Application\Exception\ApplicationException;
17
use Bluz\Cli;
18
use Bluz\Proxy\Auth;
19
use Bluz\Proxy\Logger;
20
use Bluz\Proxy\Request;
21
use Bluz\Proxy\Response;
22
use Bluz\Proxy\Router;
23
use Bluz\Request\RequestFactory;
24
25
/**
26
 * Bootstrap for CLI
27
 *
28
 * @category Application
29
 * @package  Bootstrap
30
 *
31
 * @author   Anton Shevchuk
32
 * @created  17.12.12 15:24
33
 */
34
class CliBootstrap extends Application
35
{
36
    /**
37
     * Layout flag
38
     * @var boolean
39
     */
40
    protected $layoutFlag = false;
41
42
    /**
43
     * get CLI Request
44
     * @return void
45
     * @throws ApplicationException
46
     */
47
    public function initRequest()
48
    {
49
        $arguments = getopt("u:", ["uri:"]);
50
51 View Code Duplication
        if (!array_key_exists('u', $arguments) && !array_key_exists('uri', $arguments)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            throw new ApplicationException('Attribute `--uri` is required');
53
        }
54
55
        $uri = $arguments['u'] ?? $arguments['uri'];
56
57
        $request = RequestFactory::fromGlobals(['REQUEST_URI' => $uri, 'REQUEST_METHOD' => 'CLI']);
58
59
        Request::setInstance($request);
60
    }
61
62
    /**
63
     * Pre process
64
     * @return void
65
     */
66
    protected function preProcess()
67
    {
68
        Router::process();
69
        Response::switchType('CLI');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     *
75
     * @param string $module
76
     * @param string $controller
77
     * @param array $params
78
     * @return void
79
     */
80
    protected function preDispatch($module, $controller, $params = array())
81
    {
82
        // auth as CLI user
83
        $cliUser = Table::findRowWhere(['login' => 'system']);
84
        Auth::setIdentity($cliUser);
85
86
        parent::preDispatch($module, $controller, $params);
87
    }
88
89
    /**
90
     * @return void
91
     */
92 View Code Duplication
    public function end()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        if ($messages = Logger::get('error')) {
95
            foreach ($messages as $message) {
96
                errorLog(new \ErrorException($message, 0, E_USER_ERROR));
97
            }
98
        }
99
100
        // 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...
101
//        if ($exception = $this->getException()) {
102
//            echo $exception->getMessage();
103
//            exit(1);
104
//        }
105
        exit;
106
    }
107
}
108