SWPUpdaterExtension::checkNotDefaultDirType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Updater Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\UpdaterBundle\DependencyInjection;
15
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
use Symfony\Component\DependencyInjection\Loader;
20
21
/**
22
 * This is the class that loads and manages your bundle configuration.
23
 *
24
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
25
 */
26
class SWPUpdaterExtension extends Extension
27
{
28
    private $container;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function load(array $configs, ContainerBuilder $container)
34
    {
35
        $this->container = $container;
36
        $config = $this->processConfiguration(new Configuration(), $configs);
37
        $loader = new Loader\YamlFileLoader($this->container, new FileLocator(__DIR__.'/../Resources/config'));
38
        $loader->load('services.yml');
39
40
        $this->checkAndSetSupportedClass($config);
41
42
        $clientConfig = array();
43
        if (!empty($config['client'])) {
44
            foreach (array('base_uri') as $key) {
45
                $clientConfig[$key] = $config['client'][$key];
46
            }
47
48
            $this->container->setParameter($this->getAlias().'.client', $clientConfig);
49
        }
50
51
        $options = array();
52
        if ($this->container->hasParameter($this->getAlias().'.client.options')) {
53
            $options = $this->container->getParameter($this->getAlias().'.client.options');
54
        }
55
56
        $this->container->setParameter($this->getAlias().'.client.options', $options);
57
58
        if (!empty($config['version_class'])) {
59
            $this->container->setParameter($this->getAlias().'.version_class', $config['version_class']);
60
        }
61
62
        $this->setDirectories($config);
63
64
        if (true === $config['monolog_channel']) {
65
            $this->container->setParameter($this->getAlias().'.monolog_channel', true);
66
        }
67
    }
68
69
    private function checkAndSetSupportedClass(array $config)
70
    {
71
        $supported = array(
72
            'default' => 'SWP\UpdaterBundle\Client\DefaultClient',
73
            'guzzle' => 'SWP\UpdaterBundle\Client\GuzzleClient',
74
        );
75
76
        list($class) = explode(':', $config['client']['type'], 2);
77
        if (!isset($supported[$class])) {
78
            throw new \LogicException(sprintf('Client "%s" is not supported by the UpdaterBundle.', $class));
79
        }
80
81
        if ($config['client']['type'] === 'guzzle' && !class_exists('GuzzleHttp\Client')) {
82
            throw new \LogicException('guzzlehttp/guzzle needs to be installed in order to use guzzle client!');
83
        }
84
85
        $this->container->getDefinition('swp_updater.client')->setClass($supported[$class]);
86
    }
87
88
    private function setDirectories($config)
89
    {
90
        foreach (array('temp_dir', 'target_dir') as $value) {
91
            if ($this->isDefault($config[$value])) {
92
                $this->container->setParameter(
93
                    $this->getAlias().'.'.$value,
94
                    $this->checkDirType($value)
95
                );
96
            } else {
97
                $this->container->setParameter(
98
                    $this->getAlias().'.'.$value,
99
                    $this->checkNotDefaultDirType($value, $config[$value])
100
                );
101
            }
102
        }
103
    }
104
105
    private function checkDirType($dir)
106
    {
107
        if ($dir === 'temp_dir') {
108
            return $this->container->getParameter('kernel.cache_dir');
109
        }
110
111
        return $this->container->getParameter('kernel.root_dir').'/../';
112
    }
113
114
    private function checkNotDefaultDirType($dir, $configDir)
115
    {
116
        if ($dir === 'target_dir') {
117
            return $configDir;
118
        }
119
120
        return $this->container->getParameter('kernel.root_dir').'/'.$configDir;
121
    }
122
123
    private function isDefault($dir)
124
    {
125
        if ($dir === 'default') {
126
            return true;
127
        }
128
129
        return false;
130
    }
131
}
132