JbFileUploaderExtension::loadConfiguration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright 2014 Jonathan Bouzekri. All rights reserved.
5
 *
6
 * @copyright Copyright 2014 Jonathan Bouzekri <[email protected]>
7
 * @license https://github.com/jbouzekri/FileUploaderBundle/blob/master/LICENSE
8
 * @link https://github.com/jbouzekri/FileUploaderBundle
9
 */
10
11
/**
12
 * @namespace
13
 */
14
namespace Jb\Bundle\FileUploaderBundle\DependencyInjection;
15
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
use Symfony\Component\DependencyInjection\Loader;
19
use Symfony\Component\Config\FileLocator;
20
use Symfony\Component\Config\Definition\Processor;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * Class JbFileUploaderExtension
25
 * @package Jb\Bundle\FileUploaderBundle\DependencyInjection
26
 */
27
class JbFileUploaderExtension extends Extension
28
{
29
    /**
30
     * @var array
31
     */
32
    protected $factories = null;
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function load(array $configs, ContainerBuilder $container)
38
    {
39
        $processor = new Processor();
40
41
        // first assemble the resolver factories
42
        $factoryConfig = new ResolverFactoryConfiguration();
43
        $config        = $processor->processConfiguration($factoryConfig, $configs);
44
        $factories     = $this->createResolverFactories($config, $container);
45
46
        $config = $this->processConfiguration(new MainConfiguration($factories), $configs);
0 ignored issues
show
Bug introduced by
It seems like $factories defined by $this->createResolverFac...es($config, $container) on line 44 can also be of type null; however, Jb\Bundle\FileUploaderBu...guration::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
        $this->loadConfiguration($container, $config);
48
49
        $resolvers = array();
50
        foreach ($config['resolvers'] as $name => $resolver) {
51
            $resolvers[$name] = $this->createResolver($name, $resolver, $container, $factories);
0 ignored issues
show
Bug introduced by
It seems like $factories defined by $this->createResolverFac...es($config, $container) on line 44 can also be of type null; however, Jb\Bundle\FileUploaderBu...nsion::createResolver() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
52
        }
53
54
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
55
        $loader->load('form_types.yml');
56
        $loader->load('resolvers.yml');
57
        $loader->load('services.yml');
58
        $loader->load('validators.yml');
59
60
        $resolverChain = $container->findDefinition('jb_fileuploader.resolver_chain');
61
62
        foreach ($resolvers as $name => $resolver) {
63
            $resolverChain->addMethodCall('addResolver', array(new Reference($resolver), $name));
64
        }
65
    }
66
67
    /**
68
     * Load configuration
69
     *
70
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
71
     * @param array $config
72
     */
73
    protected function loadConfiguration(ContainerBuilder $container, array $config)
74
    {
75
        $container->setParameter('jb_fileuploader.endpoints', $config);
76
        $container->setParameter('jb_fileuploader.crop_route', $config['crop_route']);
77
    }
78
79
    /**
80
     * Creates the resolver factories
81
     *
82
     * @param  array            $configs
83
     * @param  ContainerBuilder $container
84
     *
85
     * @return null|array
86
     */
87
    protected function createResolverFactories(array $configs, ContainerBuilder $container)
88
    {
89
        if (null !== $this->factories) {
90
            return $this->factories;
91
        }
92
93
        // load bundled resolver factories
94
        $tempContainer = new ContainerBuilder();
95
        $parameterBag  = $container->getParameterBag();
96
        $loader        = new Loader\YamlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
97
        $loader->load('resolver_factories.yml');
98
99
        // load user-created resolver factories
100
        foreach ($configs['resolver_factories'] as $factory) {
101
            $loader->load($parameterBag->resolveValue($factory));
102
        }
103
104
        $services  = $tempContainer->findTaggedServiceIds('jb_fileuploader.resolver.factory');
105
106
        $factories = array();
107
        foreach (array_keys($services) as $id) {
108
            $factory = $tempContainer->get($id);
109
            $factories[str_replace('-', '_', $factory->getKey())] = $factory;
110
        }
111
112
        return $this->factories = $factories;
113
    }
114
115
    /**
116
     * Create resolver
117
     *
118
     * @param string $name
119
     * @param array $config
120
     * @param ContainerBuilder $container
121
     * @param array $factories
122
     *
123
     * @return string
124
     *
125
     * @throws \LogicException
126
     */
127
    protected function createResolver($name, array $config, ContainerBuilder $container, array $factories)
128
    {
129
        foreach ($config as $key => $resolver) {
130
            if (array_key_exists($key, $factories)) {
131
                $id = sprintf('jb_fileuploader.%s_resolver', $name);
132
                $factories[$key]->create($container, $id, $resolver);
133
134
                return $id;
135
            }
136
        }
137
138
        throw new \LogicException(sprintf('The resolver \'%s\' is not configured.', $name));
139
    }
140
}
141