MakeFilter::run()   B
last analyzed

Complexity

Conditions 7
Paths 18

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 44
rs 8.6346
cc 7
nc 18
nop 1
1
<?php namespace Hafiz\Commands;
2
3
use CodeIgniter\CLI\BaseCommand;
4
use CodeIgniter\CLI\CLI;
5
use Hafiz\Libraries\FileHandler;
6
7
/**
8
 * Make command class for a Filter skeleton
9
 * class generation
10
 * skeleton has session and validation instance
11
 * already implemented
12
 *
13
 * @package CodeIgniter\Commands
14
 * @extend BaseCommand
15
 */
16
class MakeFilter extends BaseCommand
17
{
18
19
    /**
20
     * The group command is heading under all
21
     * commands will be listed
22
     * @var string
23
     */
24
    protected $group = 'CI4-Recharge';
25
26
    /**
27
     * The Command's name
28
     * @var string
29
     */
30
    protected $name = 'make:filter';
31
32
    /**
33
     * The Command's short description
34
     * @var string
35
     */
36
    protected $description = 'Creates a skeleton Filter file.';
37
38
    /**
39
     * The Command's usage
40
     * @var string
41
     */
42
    protected $usage = 'make:filter [filter_name] [Options]';
43
44
    /**
45
     * The Command's Arguments
46
     * @var array
47
     */
48
    protected $arguments = [
49
        'filter_name' => 'The Filter file name',
50
    ];
51
52
    /**
53
     * The Command's Option
54
     * @var array
55
     */
56
    protected $options = [
57
        '-n' => 'Set Filter namespace',
58
    ];
59
60
    /**
61
     * Creates a new Filter file with the current timestamp.
62
     * @param array $params
63
     * @return void
64
     */
65
    public function run(array $params = [])
66
    {
67
        /**
68
         *
69
         */
70
        helper(['inflector', 'filesystem']);
71
        $file = new FileHandler();
72
73
        $name = array_shift($params);
74
75
        //if namespace is given
76
        $ns = $params['-n'] ?? CLI::getOption('n');
77
78
        if (empty($name))
79
            $name = CLI::prompt(lang('Recharge.filterName'), null, 'required|string');
80
81
        if (empty($name)) {
82
            CLI::error(lang('Recharge.badName'));
83
            return;
84
        }
85
86
        //namespace locator
87
        $nsinfo = $file->getNamespaceInfo($ns, 'App');
88
        $ns = $nsinfo['ns'];
89
        $targetDir = $nsinfo['path'] . '/Filters/';
90
91
        //class & file name
92
        $name = pascalize($name) . ((stripos($name, 'filter') == false) ? 'Filter' : '');
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing stripos($name, 'filter') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
93
        $filepath = $targetDir . $name . '.php';
94
95
        if ($file->verifyDirectory($filepath)) {
96
97
            $data = ['{namespace}' => $ns, '{name}' => $name, '{created_at}' => date("d F, Y h:i:s A")];
98
99
            //check a directory exist
100
            if ($file->checkFileExist($filepath) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
101
                $template = $file->renderTemplate('filter', $data);
102
103
                if (!write_file($filepath, $template)) {
104
                    CLI::error(lang('Recharge.writeError', [$filepath]));
105
                    return;
106
                }
107
108
                CLI::write('Created file: ' . CLI::color($filepath, 'green'));
109
            }
110
        }
111
    }
112
}
113