IndexController::beVerbose()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-09-02 
5
 */
6
7
namespace NetBazzlineZfLocatorGenerator\Controller\Console;
8
9
use Exception;
10
use InvalidArgumentException;
11
use Net\Bazzline\Component\ProcessPipe\PipeInterface;
12
use Zend\Console\ColorInterface;
13
use ZfConsoleHelper\Controller\Console\AbstractConsoleController;
14
15
class IndexController extends AbstractConsoleController
16
{
17
    /** @var array */
18
    private $configuration;
19
20
    /** @var PipeInterface */
21
    private $processPipe;
22
23
    /**
24
     * @param array $configuration
25
     */
26
    public function setConfiguration(array $configuration)
27
    {
28
        $this->configuration = $configuration;
29
    }
30
31
    /**
32
     * @param PipeInterface $processPipe
33
     */
34
    public function setProcessPipe(PipeInterface $processPipe)
35
    {
36
        $this->processPipe = $processPipe;
37
    }
38
39
40
41
    public function generateAction()
42
    {
43
        $beVerbose      = $this->beVerbose();
44
45
        $configuration  = $this->configuration;
46
        $console        = $this->getConsole();
47
        $processPipe    = $this->processPipe;
48
49
        try {
50
            $this->throwExceptionIfNotCalledInsideAnCliEnvironment();
51
            $configuration = $configuration['name_to_configuration_path'];
52
53
            if ($this->hasParameter('locator_name')) {
54
                $locatorName = $this->getParameter('locator_name');
55
56
                if (!isset($configuration[$locatorName])) {
57
                    throw new InvalidArgumentException(
58
                        'invalid locator name provided'
59
                    );
60
                }
61
62
                $namesToPath = array($locatorName => $configuration[$locatorName]);
63
            } else {
64
                $namesToPath = $configuration;
65
            }
66
67
            //@todo implement usage of $this->processItems();
68
            foreach ($namesToPath as $name => $path) {
69
                if ($beVerbose) {
70
                    $console->writeLine(
71
                        'generating "' . $name . '" by using configuration file "' . $path . '"'
72
                    );
73
                } else {
74
                    $console->write('.');
75
                }
76
77
                $arguments = array(
78
                    __FILE__,
79
                    $path
80
                );
81
82
                try {
83
                    $processPipe->execute($arguments);
84
                } catch (Exception $exception) {
85
                    $console->setColor(ColorInterface::LIGHT_RED);
86
                    $console->writeLine('could not generate locator for "' . $name . '"');
87
                    $console->writeLine('error: ' . $exception->getMessage());
88
                    $console->resetColor();
89
                }
90
            }
91
        } catch (Exception $exception) {
92
            $this->handleException($exception);
93
        }
94
    }
95
96
    public function listAction()
97
    {
98
        $configuration  = $this->configuration;
99
        $console        = $this->getConsole();
100
101
        try {
102
            $this->throwExceptionIfNotCalledInsideAnCliEnvironment();
103
104
            $configuration = $configuration['name_to_configuration_path'];
105
106
            foreach ($configuration as $name => $path) {
107
                $console->writeLine('locator: ' . $name . ' with configuration file "' . $path . '"');
108
            }
109
        } catch (Exception $exception) {
110
            $this->handleException($exception);
111
        }
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    protected function beVerbose()
118
    {
119
        return $this->hasBooleanParameter('v', 'verbose');
120
    }
121
}