Completed
Push — develop ( f11aab...a4697b )
by Alejandro
09:31
created

ConfigMigrationController::parseConfigAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
ccs 23
cts 23
cp 1
rs 8.8571
cc 3
eloc 23
nc 3
nop 0
crap 3
1
<?php
2
namespace AcMailer\Controller;
3
4
use AcMailer\Exception\InvalidArgumentException;
5
use AcMailer\Service\ConfigMigrationServiceInterface;
6
use Zend\Config\Writer\WriterInterface;
7
use Zend\Console\ColorInterface;
8
use Zend\Console\Request;
9
use Zend\Mvc\Console\Controller\AbstractConsoleController;
10
11
/**
12
 * Class ConfigMigrationController
13
 * @author Alejandro Celaya Alastrué
14
 * @link http://www.alejandrocelaya.com
15
 */
16
class ConfigMigrationController extends AbstractConsoleController
17
{
18
    const WRITER_NAMESPACE = '\Zend\Config\Writer';
19
20
    private $formats = [
21
        'php' => 'PhpArray',
22
        'ini' => 'Ini',
23
        'json' => 'Json',
24
        'xml' => 'Xml'
25
    ];
26
27
    /**
28
     * @var ConfigMigrationServiceInterface
29
     */
30
    protected $configMigrationService;
31
    /**
32
     * @var array
33
     */
34
    protected $globalConfig;
35
36 5
    public function __construct(ConfigMigrationServiceInterface $configMigrationService, array $globalConfig)
37
    {
38 5
        $this->configMigrationService = $configMigrationService;
39 5
        $this->globalConfig = $globalConfig;
40 5
    }
41
42 4
    public function parseConfigAction()
43
    {
44
        /** @var Request $request */
45 4
        $request = $this->getRequest();
46 4
        $format = $request->getParam('format', 'php');
47 4
        $writer = $this->createWriter($format);
48
49 3
        $configKey = $request->getParam('configKey', 'mail_options'); // Default to the old config key
50 3
        $outputFile = $request->getParam('outputFile');
51
52
        // Global configuration not found
53 3
        if (! isset($this->globalConfig[$configKey])) {
54 1
            $this->getConsole()->writeLine(
55 1
                sprintf('It wasn\'t possible to find the configuration key "%s"', $configKey),
56
                ColorInterface::RED
57 1
            );
58 1
            return PHP_EOL;
59
        }
60
61
        // Write new configuration to CLI
62 2
        $newConfig = $this->configMigrationService->parseConfig($this->globalConfig[$configKey]);
63 2
        if (! isset($outputFile)) {
64 1
            $this->getConsole()->writeLine(
65 1
                'This is your new configuration for the AcMailer module:',
66
                ColorInterface::GREEN
67 1
            );
68 1
            $this->getConsole()->write($writer->toString($newConfig), ColorInterface::LIGHT_BLUE);
69 1
            return PHP_EOL;
70
        }
71
72
        // Write new configuration to file
73 1
        $writer->toFile($outputFile, $newConfig);
74 1
        $this->getConsole()->writeLine(
75 1
            sprintf('The new configuration for the AcMailer module has been written to "%s":', $outputFile),
76
            ColorInterface::GREEN
77 1
        );
78 1
        return PHP_EOL;
79
    }
80
81
    /**
82
     * @param $format
83
     * @return WriterInterface
84
     */
85 4
    protected function createWriter($format)
86
    {
87 4
        if (! array_key_exists($format, $this->formats) && ! in_array($format, $this->formats)) {
88 1
            throw new InvalidArgumentException(sprintf(
89 1
                'Provided format "%s" is not valid. Expected one of ["%s"]',
90 1
                $format,
91 1
                implode('", "', array_keys($this->formats))
92 1
            ));
93
        }
94
95 3
        $writerClass = array_key_exists($format, $this->formats) ? $this->formats[$format] : $format;
96 3
        $writerClass = sprintf('%s\%s', self::WRITER_NAMESPACE, $writerClass);
97 3
        return new $writerClass;
98
    }
99
}
100