Passed
Push — master ( 80db42...d275a8 )
by Anton
03:59 queued 23s
created

Application   C

Complexity

Total Complexity 10

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 25

Test Coverage

Coverage 85.48%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 25
dl 0
loc 142
ccs 53
cts 62
cp 0.8548
rs 5
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A init() 0 10 1
A getDefaultInputDefinition() 0 19 2
B registerCommands() 0 26 1
A getWorkingPath() 0 4 1
A getModulePath() 0 7 1
A getModelPath() 0 7 1
A isModuleExists() 0 4 1
A isModelExists() 0 4 1
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Application;
8
9
use Bluz\Config\Config;
10
use Bluz\Proxy;
11
use Bluzman\Command;
12
use Symfony\Component\Console;
13
use Symfony\Component\Console\Input\InputDefinition;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Input\InputArgument;
16
17
/**
18
 * @package Bluzman\Application
19
 *
20
 * @author Pavel Machekhin
21
 * @created 2013-11-28 12:31
22
 */
23
class Application extends Console\Application
24
{
25
    /**
26
     * @param string $name
27
     * @param string $version
28
     */
29 14
    public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
30
    {
31 14
        parent::__construct($name, $version);
32
33 14
        $this->registerCommands();
34 14
    }
35
36
    /**
37
     * init
38
     *
39
     * @return void
40
     */
41
    public function init()
42
    {
43
        // Init Bluz config
44
        $config = new Config();
45
        $config->setPath(PATH_APPLICATION);
46
        $config->setEnvironment(BLUZ_ENV);
47
        $config->init();
48
49
        Proxy\Config::setInstance($config);
50
    }
51
52
    /**
53
     * Removed some commands from default input definition.
54
     *
55
     * @return InputDefinition An InputDefinition instance
56
     */
57 14
    protected function getDefaultInputDefinition()
58
    {
59 14
        return new InputDefinition(
60
            [
61 14
                new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
62 14
                new InputOption(
63 14
                    '--env',
64 14
                    '-e',
65 14
                    InputOption::VALUE_REQUIRED,
66 14
                    'The environment to be used',
67 14
                    getenv('BLUZ_ENV') ?: 'dev'
68
                ),
69 14
                new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
70 14
                new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
71 14
                new InputOption('--verbose', '-v', InputOption::VALUE_NONE, 'Increase verbosity of messages'),
72 14
                new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version')
73
            ]
74
        );
75
    }
76
77
    /**
78
     * Register Bluzman commands
79
     *
80
     * @todo Find a way to do this automatically
81
     */
82 14
    protected function registerCommands()
83
    {
84 14
        $this->addCommands(
85
            [
86 14
                new Command\MagicCommand,
87 14
                new Command\Db\CreateCommand,
88 14
                new Command\Db\MigrateCommand,
89 14
                new Command\Db\RollbackCommand,
90 14
                new Command\Db\StatusCommand,
91 14
                new Command\Db\SeedCreateCommand,
92 14
                new Command\Db\SeedRunCommand,
93 14
                new Command\Generate\ModuleCommand,
94 14
                new Command\Generate\ControllerCommand,
95 14
                new Command\Generate\ModelCommand,
96 14
                new Command\Generate\CrudCommand,
97 14
                new Command\Generate\GridCommand,
98 14
                new Command\Generate\RestCommand,
99 14
                new Command\Module\InstallCommand,
100 14
                new Command\Module\ListCommand,
101 14
                new Command\Module\RemoveCommand,
102 14
                new Command\Server\StartCommand,
103 14
                new Command\Server\StopCommand,
104 14
                new Command\Server\StatusCommand,
105
            ]
106
        );
107 14
    }
108
109
    /**
110
     * Returns the path to the directory with bluzman application
111
     *
112
     * @return string
113
     */
114
    public function getWorkingPath()
115
    {
116
        return getcwd();
117
    }
118
119
    /**
120
     * Get Module path
121
     *
122
     * @param  string $name
123
     * @return string
124
     */
125 3
    public function getModulePath($name)
126
    {
127 3
        return $this->getWorkingPath() . DIRECTORY_SEPARATOR
128 3
            . 'application' . DIRECTORY_SEPARATOR
129 3
            . 'modules' . DIRECTORY_SEPARATOR
130 3
            . $name;
131
    }
132
133
    /**
134
     * Get Model path
135
     *
136
     * @param  string $name
137
     * @return string
138
     */
139 4
    public function getModelPath($name)
140
    {
141 4
        return $this->getWorkingPath() . DIRECTORY_SEPARATOR
142 4
            . 'application' . DIRECTORY_SEPARATOR
143 4
            . 'models' . DIRECTORY_SEPARATOR
144 4
            . $name;
145
    }
146
147
    /**
148
     * @param  string $name
149
     * @return bool
150
     */
151 2
    public function isModuleExists($name)
152
    {
153 2
        return is_dir($this->getModulePath($name));
154
    }
155
156
    /**
157
     * @param  string $name
158
     * @return bool
159
     */
160 3
    public function isModelExists($name)
161
    {
162 3
        return is_dir($this->getModelPath($name));
163
    }
164
}
165