BfwCli::getModule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BfwCli;
4
5
use \Exception;
6
7
class BfwCli implements \SplObserver
8
{
9
    /**
10
     * @const ERR_NO_FILE_SPECIFIED_IN_ARG Exception code if the cli file to
11
     * run is not specified with the "-f" argument.
12
     */
13
    const ERR_NO_FILE_SPECIFIED_IN_ARG = 1201001;
14
    
15
    /**
16
     * @const ERR_CLI_FILE_NOT_FOUND Exception code if the cli file to run is
17
     * not found.
18
     */
19
    const ERR_FILE_NOT_FOUND = 1201002;
20
    
21
    /**
22
     * @var \BFW\Module $module The bfw module instance for this module
23
     */
24
    protected $module;
25
    
26
    /**
27
     * @var string The name of the executed cli file
28
     */
29
    protected $executedFile = '';
30
    
31
    /**
32
     * Constructor
33
     * 
34
     * @param \BFW\Module $module
35
     */
36
    public function __construct(\BFW\Module $module)
37
    {
38
        $this->module = $module;
39
    }
40
    
41
    /**
42
     * Getter accessor for module property
43
     * 
44
     * @return \BFW\Module
45
     */
46
    public function getModule(): \BFW\Module
47
    {
48
        return $this->module;
49
    }
50
    
51
    /**
52
     * Getter to the property executedFile
53
     * 
54
     * @return string
55
     */
56
    public function getExecutedFile(): string
57
    {
58
        return $this->executedFile;
59
    }
60
    
61
    /**
62
     * Observer update method
63
     * 
64
     * @param \SplSubject $subject
65
     * 
66
     * @return void
67
     */
68
    public function update(\SplSubject $subject)
69
    {
70
        if ($subject->getAction() === 'BfwApp_done_ctrlRouterLink') {
71
            $this->run();
72
        }
73
    }
74
    
75
    /**
76
     * Check if the system is run in cli mode, and call methods to find cli
77
     * file to execute and execute it.
78
     * 
79
     * @return void
80
     */
81
    protected function run()
82
    {
83
        if (PHP_SAPI !== 'cli') {
84
            return;
85
        }
86
        
87
        \BFW\Application::getInstance()
88
            ->getSubjectList()
89
            ->getSubjectByName('ApplicationTasks')
90
            ->sendNotify('run_cli_file');
0 ignored issues
show
Bug introduced by
The method sendNotify() does not exist on SplSubject. It seems like you code against a sub-type of SplSubject such as BFW\RunTasks. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
            ->/** @scrutinizer ignore-call */ sendNotify('run_cli_file');
Loading history...
91
        
92
        $this->executedFile = $this->obtainFileFromArg();
93
        
94
        if ($this->checkFile() === true) {
0 ignored issues
show
introduced by
The condition $this->checkFile() === true is always true.
Loading history...
95
            $this->execFile();
96
        }
97
    }
98
    
99
    /**
100
     * Obtain the file to execute from the cli arg.
101
     * Search the arg "f" to get the value.
102
     * 
103
     * @return string The file path
104
     * 
105
     * @throws \Exception If no file is declared to be executed
106
     */
107
    public function obtainFileFromArg(): string
108
    {
109
        $cliArgs = getopt('f:');
110
        if (!isset($cliArgs['f'])) {
111
            throw new Exception(
112
                'Error: No file specified.',
113
                $this::ERR_NO_FILE_SPECIFIED_IN_ARG
114
            );
115
        }
116
117
        return CLI_DIR.$cliArgs['f'].'.php';
0 ignored issues
show
Bug introduced by
The constant BfwCli\CLI_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
118
    }
119
    
120
    /**
121
     * Check the file to execute.
122
     * 
123
     * @return boolean
124
     * 
125
     * @throws \Exception
126
     */
127
    protected function checkFile(): bool
128
    {
129
        if (!file_exists($this->executedFile)) {
130
            throw new Exception(
131
                'File to execute not found.',
132
                $this::ERR_FILE_NOT_FOUND
133
            );
134
        }
135
        
136
        return true;
137
    }
138
    
139
    /**
140
     * Execute the cli file into a different scope.
141
     * The new scope have access to $this of this class.
142
     * 
143
     * @return void
144
     */
145
    protected function execFile()
146
    {
147
        $this->module
148
            ->monolog
0 ignored issues
show
Bug introduced by
The property monolog does not seem to exist on BFW\Module.
Loading history...
149
            ->getLogger()
150
            ->debug(
151
                'execute cli file.',
152
                ['file' => $this->executedFile]
153
            );
154
        
155
        $fctRunCliFile = function() {
156
            require($this->executedFile);
157
        };
158
        
159
        $fctRunCliFile();
160
    }
161
}
162