Completed
Pull Request — master (#3)
by Jacob
02:20
created

MetadataDrivers::load()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 25
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 25
loc 25
rs 8.5806
cc 4
eloc 17
nc 4
nop 2
1
<?php
2
3
namespace As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader;
4
5
use As3\Bundle\ModlrBundle\DependencyInjection\Utility;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Loads metadata driver services.
12
 *
13
 * @author  Jacob Bare <[email protected]>
14
 */
15
class MetadataDrivers implements ServiceLoaderInterface
16
{
17
    /**
18
     * Creates the file locator service definition.
19
     *
20
     * @param   string  $modelDir
21
     * @param   string  $mixinDir
22
     * @return  Definition
23
     */
24
    private function createFileLocator($modelDir, $mixinDir)
25
    {
26
        $definition = new Definition(
27
            Utility::getLibraryClass('Metadata\Driver\FileLocator'),
28
            [ $modelDir, $mixinDir ]
29
        );
30
        $definition->setPublic(false);
31
        return $definition;
32
    }
33
34
    /**
35
     * Creates the YAML metadata driver service definition.
36
     *
37
     * @param   string              $driverName
38
     * @param   array               $driverConfig
39
     * @param   ContainerBuilder    $container
40
     * @return  Definition
41
     */
42
    private function createYmlDriver($driverName, array $driverConfig, ContainerBuilder $container)
43
    {
44
        // Definition directories
45
        list($modelDir, $mixinDir) = $this->getDefinitionDirs($driverName, $driverConfig, $container);
46
47
        // Set the directories to the dirs container parameter.
48
        Utility::appendParameter('dirs', sprintf('%s.model_dir', $driverName), $modelDir, $container);
49
        Utility::appendParameter('dirs', sprintf('%s.mixin_dir', $driverName), $mixinDir, $container);
50
51
        // File locator
52
        $locatorName = sprintf('%s.file_locator', $driverName);
53
        $locatorDef = $this->createFileLocator($modelDir, $mixinDir);
54
        $container->setDefinition($locatorName, $locatorDef);
55
56
        // Driver
57
        return new Definition(
58
            Utility::getLibraryClass('Metadata\Driver\YamlFileDriver'),
59
            [
60
                new Reference($locatorName),
61
                new Reference(Utility::getAliasedName('storage_manager')),
62
            ]
63
        );
64
    }
65
66
    /**
67
     * Gets the definition directories for models and mixins and returns as a tuple.
68
     *
69
     * @param   string              $driverName
70
     * @param   array               $driverConfig
71
     * @param   ContainerBuilder    $container
72
     * @return  array
73
     */
74
    private function getDefinitionDirs($driverName, array $driverConfig, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $driverName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        $defaultDir = sprintf('%s/Resources/As3ModlrBundle', $container->getParameter('kernel.root_dir'));
77
78
        $modelDir = sprintf('%s/models', $defaultDir);
79
        $mixinDir = sprintf('%s/mixins', $defaultDir);
80 View Code Duplication
        if (isset($driverConfig['parameters']['model_dir'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            $modelDir = Utility::locateResource($driverConfig['parameters']['model_dir'], $container);
82
        }
83 View Code Duplication
        if (isset($driverConfig['parameters']['mixin_dir'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
            $mixinDir = Utility::locateResource($driverConfig['parameters']['mixin_dir'], $container);
85
        }
86
87
        return [$modelDir, $mixinDir];
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     * @todo    Add low-level support for multiple drivers in the metadata factory.
93
     */
94 View Code Duplication
    public function load(array $config, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        foreach ($config['metadata']['drivers'] as $name => $driverConfig) {
97
            $driverName = Utility::getAliasedName(sprintf('metadata.driver.%s', $name));
98
            if (isset($driverConfig['service'])) {
99
                // Custom persister.
100
                $container->setAlias($driverName, Utility::cleanServiceName($driverConfig['service']));
101
            } else {
102
                // Built-in driver.
103
                switch ($driverConfig['type']) {
104
                    case 'yml':
105
                        $definition = $this->createYmlDriver($driverName, $driverConfig, $container);
106
                        break;
107
                    default:
108
                        throw new \RuntimeException(sprintf('Unable to create a metadata driver for type "%s"', $driverConfig['type']));
109
                }
110
                $definition->setPublic(false);
111
                $container->setDefinition($driverName, $definition);
112
            }
113
            // The library currently only supports one driver. Must break and set as default alias.
114
            $container->setAlias(Utility::getAliasedName('metadata.default_driver'), $driverName);
115
            break;
116
        }
117
        return $this;
118
    }
119
}
120