HTML5CacheExtension::filterCdn()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 1
1
<?php
2
3
namespace Evheniy\HTML5CacheBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
7
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\Config\Definition\Processor;
10
11
/**
12
 * Class HTML5CacheBundle
13
 *
14
 * @package Evheniy\HTML5CacheBundle\DependencyInjection
15
 */
16
class HTML5CacheExtension extends Extension {
17
18
    /**
19
     * @param array            $configs
20
     * @param ContainerBuilder $container
21
     */
22
    public function load(array $configs, ContainerBuilder $container) {
23
        $processor = new Processor();
24
        $configuration = new Configuration();
25
        $config = $processor->processConfiguration($configuration, $configs);
26
        $config['cdn'] = $this->filterCdn($config['cdn']);
27
        $container->setParameter('html5_cache', $config);
28
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
29
        $loader->load('services.yml');
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getAlias() {
36
        return 'html5_cache';
37
    }
38
39
    /**
40
     * @param string $cdn
41
     *
42
     * @return string
43
     */
44
    public function filterCdn($cdn = '') {
45
        if (!empty($cdn) && $cdn != '/') {
46
            $url = parse_url($cdn);
47
            if (!empty($url['host'])) {
48
                $cdn = $url['host'];
49
            } else {
50
                $cdn = current(
51
                        array_filter(preg_split('/[^a-z0-9\.]+/', $url['path']))
52
                );
53
            }
54
            $cdn = '//' . $cdn;
55
        } else {
56
            $cdn = '';
57
        }
58
        return $cdn;
59
    }
60
61
}
62