Completed
Pull Request — master (#123)
by Alex
04:13 queued 02:21
created

DebrilRssAtomExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
3
namespace Debril\RssAtomBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Definition;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9
use Symfony\Component\DependencyInjection\Loader;
10
11
/**
12
 * This is the class that loads and manages your bundle configuration.
13
 *
14
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
15
 */
16
class DebrilRssAtomExtension extends Extension
17
{
18
19
    /**
20
     * @var array
21
     */
22
    protected $defaultDateFormats = [
23
        \DateTime::RFC3339,
24
        \DateTime::RSS,
25
        \DateTime::W3C,
26
        'Y-m-d\TH:i:s.uP',
27
        'Y-m-d',
28
        'd/m/Y',
29
        'd M Y H:i:s P',
30
        'D, d M Y H:i O',
31
        'D, d M Y H:i:s O',
32
        'D M d Y H:i:s e',
33
    ];
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function load(array $configs, ContainerBuilder $container)
39
    {
40
        $configuration = new Configuration();
41
        $config = $this->processConfiguration($configuration, $configs);
42
43
        $this->setDefinition($container, 'logger', 'Psr\Log\NullLogger');
44
45
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
46
        $loader->load('services.yml');
47
48
        $this->setDateFormats($container, $config);
49
        $container->setParameter('debril_rss_atom.private_feeds', $config['private']);
50
    }
51
52
    /**
53
     * @param ContainerBuilder $container
54
     * @param $serviceName
55
     * @param $className
56
     * @return $this
57
     */
58
    protected function setDefinition(ContainerBuilder $container, $serviceName, $className)
59
    {
60
        if ( ! $container->hasDefinition($serviceName) && ! $container->hasAlias($serviceName)) {
61
            $container->setDefinition($serviceName, new Definition($className));
62
        }
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param ContainerBuilder $container
69
     * @param array $config
70
     * @return $this
71
     */
72
    protected function setDateFormats(ContainerBuilder $container, array $config)
73
    {
74
        $dateFormats = isset($config['date_formats']) ?
75
            array_merge($this->defaultDateFormats, $config['date_formats']):
76
            $this->defaultDateFormats;
77
78
        $container->setParameter(
79
            'debril_rss_atom.date_formats',
80
            $dateFormats
81
        );
82
83
        return $this;
84
    }
85
}
86