NovawayOpenGraphExtension   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 8
dl 0
loc 63
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C load() 0 57 11
1
<?php
2
3
namespace Novaway\Bundle\OpenGraphBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8
use Symfony\Component\DependencyInjection\Alias;
9
use Symfony\Component\DependencyInjection\Loader;
10
11
class NovawayOpenGraphExtension extends Extension
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function load(array $configs, ContainerBuilder $container)
17
    {
18
        $configuration = new Configuration();
19
        $config = $this->processConfiguration($configuration, $configs);
20
21
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
22
        $loader->load('services.yml');
23
24
        if ('none' === $config['metadata']['cache']) {
25
            $container->removeAlias('novaway.open_graph.metadata.cache');
26
        } elseif ('file' === $config['metadata']['cache']) {
27
            $container
28
                ->getDefinition('novaway.open_graph.metadata.file_cache')
29
                ->replaceArgument(0, $config['metadata']['file_cache']['dir'])
30
            ;
31
            $dir = $container->getParameterBag()->resolveValue($config['metadata']['file_cache']['dir']);
32
            if (!file_exists($dir)) {
33
                if (!$rs = @mkdir($dir, 0777, true)) {
34
                    throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $dir));
35
                }
36
            }
37
        } else {
38
            $container->setAlias('novaway.open_graph.metadata.cache', new Alias($config['metadata']['cache'], false));
39
        }
40
41
        $debug = ($config['metadata']['debug'] || $container->getParameter('kernel.debug'));
42
        $container
43
            ->getDefinition('novaway.open_graph.metadata_factory')
44
            ->replaceArgument(2, $debug)
45
        ;
46
47
        $bundles = $container->getParameter('kernel.bundles');
48
49
        $directories = [];
50
        if ($config['metadata']['auto_detection']) {
51
            foreach ($bundles as $name => $class) {
52
                $ref = new \ReflectionClass($class);
53
                $directories[$ref->getNamespaceName()] = dirname($ref->getFileName()).'/Resources/config/open-graph';
54
            }
55
        }
56
        foreach ($config['metadata']['directories'] as $directory) {
57
            $directory['path'] = rtrim(str_replace('\\', '/', $directory['path']), '/');
58
            if ('@' === $directory['path'][0]) {
59
                $bundleName = substr($directory['path'], 1, strpos($directory['path'], '/') - 1);
60
                if (!isset($bundles[$bundleName])) {
61
                    throw new \RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, implode(', ', array_keys($bundles))));
62
                }
63
                $ref = new \ReflectionClass($bundles[$bundleName]);
64
                $directory['path'] = dirname($ref->getFileName()).substr($directory['path'], strlen('@'.$bundleName));
65
            }
66
            $directories[rtrim($directory['namespace_prefix'], '\\')] = rtrim($directory['path'], '\\/');
67
        }
68
        $container
69
            ->getDefinition('novaway.open_graph.metadata.file_locator')
70
            ->replaceArgument(0, $directories)
71
        ;
72
    }
73
}
74