IndexController::cliAction()   A
last analyzed

Complexity

Conditions 3
Paths 13

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 13
nop 0
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-07-08
5
 */
6
7
namespace NetBazzlineZfCliGenerator\Controller\Console;
8
9
use Exception;
10
use Net\Bazzline\Component\ProcessPipe\PipeInterface;
11
use NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer\DumpCliContent;
12
use ZfConsoleHelper\Controller\Console\AbstractConsoleController;
13
14
class IndexController extends AbstractConsoleController
15
{
16
    /** @var PipeInterface */
17
    private $generateCli;
18
19
    /** @var PipeInterface */
20
    private $generateConfiguration;
21
22
    /** @var string */
23
    private $pathToApplication;
24
25
    /** @var string */
26
    private $pathToAutoload;
27
28
    /** @var string */
29
    private $pathToCli;
30
31
    /** @var string */
32
    private $pathToConfiguration;
33
34
    /** @var string */
35
    private $prefix;
36
37
    /**
38
     * @param PipeInterface $processPipe
39
     */
40
    public function injectGenerateCliProcessPipe(PipeInterface $processPipe)
41
    {
42
        $this->generateCli = $processPipe;
43
    }
44
45
    /**
46
     * @param PipeInterface $processPipe
47
     */
48
    public function injectGenerateConfigurationProcessPipe(PipeInterface $processPipe)
49
    {
50
        $this->generateConfiguration = $processPipe;
51
    }
52
53
    /**
54
     * @param string $path
55
     */
56
    public function injectPathToApplication($path)
57
    {
58
        $this->pathToApplication = $path;
59
    }
60
61
    /**
62
     * @param string $path
63
     */
64
    public function injectPathToAutoload($path)
65
    {
66
        $this->pathToAutoload = $path;
67
    }
68
69
    /**
70
     * @param string $path
71
     */
72
    public function injectPathToCli($path)
73
    {
74
        $this->pathToCli = $path;
75
    }
76
77
    /**
78
     * @param string $path
79
     */
80
    public function injectPathToConfiguration($path)
81
    {
82
        $this->pathToConfiguration = $path;
83
    }
84
85
    /**
86
     * @param string $prefix
87
     */
88
    public function injectPrefix($prefix)
89
    {
90
        $this->prefix = $prefix;
91
    }
92
93
    public function configurationAction()
94
    {
95
        //begin of dependencies
96
        $console                = $this->getConsole();
97
        $pathToApplication      = $this->pathToApplication;
98
        $pathToConfiguration    = $this->pathToConfiguration;
99
        $processPipe            = $this->generateConfiguration;
100
        //end of dependencies
101
102
        try {
103
            $this->throwExceptionIfNotCalledInsideAnCliEnvironment();
104
105
            //@todo replace by something form zend
106
            $content = $processPipe->execute($pathToApplication);
107
            $this->tryToWriteContent($pathToConfiguration, $content);
108
            $console->writeLine('generated configuration in path: "' . $pathToConfiguration . '"');
109
        } catch (Exception $exception) {
110
            $this->handleException($exception);
111
        }
112
    }
113
114
    public function cliAction()
115
    {
116
        //begin of dependencies
117
        $console                = $this->getConsole();
118
        $pathToApplication      = $this->pathToApplication;
119
        $pathToAutoload         = $this->pathToAutoload;
120
        $pathToConfiguration    = $this->pathToConfiguration;
121
        $pathToCli              = $this->pathToCli;
122
        $prefix                 = $this->prefix;
123
        $processPipe            = $this->generateCli;
124
        //end of dependencies
125
126
        try {
127
            $this->throwExceptionIfNotCalledInsideAnCliEnvironment();
128
129
            //workflow
130
            //  generate cli file using the template
131
            //  this file also contains the closure used in the configuration to execute the code
132
            $input                  = array(
133
                DumpCliContent::INPUT_KEY_PATH_TO_APPLICATION   => $pathToApplication,
134
                DumpCliContent::INPUT_KEY_PATH_TO_AUTOLOAD      => $pathToAutoload,
135
                DumpCliContent::INPUT_KEY_PATH_TO_CLI           => $pathToCli,
136
                DumpCliContent::INPUT_KEY_PATH_TO_CONFIGURATION => $pathToConfiguration,
137
                DumpCliContent::INPUT_KEY_PREFIX_CLI            => $prefix
138
            );
139
            //we need to make sure the file exist
140
            if (!file_exists($pathToCli)) {
141
                $this->tryToWriteContent($pathToCli, '');
142
            }
143
            $content = $processPipe->execute($input);
144
            $this->tryToWriteContent($pathToCli, $content);
145
            chmod($pathToCli, 0755);
146
            $console->writeLine('generated cli in path: "' . $pathToCli . '"');
147
        } catch (Exception $exception) {
148
            $this->handleException($exception);
149
        }
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    protected function beVerbose()
156
    {
157
        return $this->hasBooleanParameter('v', 'verbose');
158
    }
159
160
    /**
161
     * @param string $path
162
     * @param mixed $content
163
     * @throws Exception
164
     */
165
    private function tryToWriteContent($path, $content)
166
    {
167
        $contentCouldBeWritten  = (file_put_contents($path, $content) !== false);
168
169
        if (!$contentCouldBeWritten) {
170
            throw new Exception(
171
                'could not write to path "' . $path . '"'
172
            );
173
        }
174
    }
175
}