Completed
Push — master ( 1235b0...90fadd )
by Tim
15s
created

LibraryLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 111
ccs 0
cts 55
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getContainer() 0 4 1
A getVendorDir() 0 4 1
B load() 0 53 7
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Configuration\LibraryLoader
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cli\Configuration;
22
23
use Symfony\Component\Config\FileLocator;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
27
use TechDivision\Import\ConfigurationInterface;
28
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys;
29
30
/**
31
 * The library loader implementation.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-cli-simple
37
 * @link      http://www.techdivision.com
38
 */
39
class LibraryLoader
40
{
41
42
    /**
43
     * The container instance.
44
     *
45
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
46
     */
47
    protected $container;
48
49
    /**
50
     * The actual input instance.
51
     *
52
     * @var \Symfony\Component\Console\Input\InputInterface
53
     */
54
    protected $input;
55
56
    /**
57
     * Initializes the configuration loader.
58
     *
59
     * @param \Symfony\Component\Console\Input\InputInterface           $input     The input instance
60
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The container instance
61
     */
62
    public function __construct(InputInterface $input, ContainerInterface $container)
63
    {
64
        $this->input = $input;
65
        $this->container= $container;
66
    }
67
68
    /**
69
     * Return's the DI container instance.
70
     *
71
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance
72
     */
73
    protected function getContainer()
74
    {
75
        return $this->container;
76
    }
77
78
    /**
79
     * Return's the absolute path to the actual vendor directory.
80
     *
81
     * @return string The absolute path to the actual vendor directory
82
     * @throws \Exception Is thrown, if none of the possible vendor directories can be found
83
     */
84
    protected function getVendorDir()
85
    {
86
        return $this->getContainer()->getParameter(DependencyInjectionKeys::CONFIGURATION_VENDOR_DIR);
87
    }
88
89
    /**
90
     * Load's the external libraries registered in the passed configuration.
91
     *
92
     * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance
93
     *
94
     * @return void
95
     */
96
    public function load(ConfigurationInterface $configuration)
97
    {
98
99
        // initialize the default loader and load the DI configuration for the this library
100
        $defaultLoader = new XmlFileLoader($this->getContainer(), new FileLocator($this->getVendorDir()));
0 ignored issues
show
Compatibility introduced by
$this->getContainer() of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ction\ContainerBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
101
102
        // load the DI configuration for all the extension libraries
103
        foreach ($configuration->getExtensionLibraries() as $library) {
104
            if (file_exists($diConfiguration = sprintf('%s/%s/symfony/Resources/config/services.xml', $this->getVendorDir(), $library))) {
105
                $defaultLoader->load($diConfiguration);
106
            } else {
107
                throw new \Exception(
108
                    sprintf(
109
                        'Can\'t load DI configuration for library "%s"',
110
                        $diConfiguration
111
                    )
112
                );
113
            }
114
        }
115
116
        // register autoloaders for additional vendor directories
117
        $customLoader = new XmlFileLoader($this->getContainer(), new FileLocator());
0 ignored issues
show
Compatibility introduced by
$this->getContainer() of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ction\ContainerBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
118
        foreach ($configuration->getAdditionalVendorDirs() as $additionalVendorDir) {
119
            // load the vendor directory's auto loader
120
            if (file_exists($autoLoader = $additionalVendorDir->getVendorDir() . '/autoload.php')) {
121
                require $autoLoader;
122
            } else {
123
                throw new \Exception(
124
                    sprintf(
125
                        'Can\'t find autoloader in configured additional vendor directory "%s"',
126
                        $additionalVendorDir->getVendorDir()
127
                    )
128
                );
129
            }
130
131
            // try to load the DI configuration for the configured extension libraries
132
            foreach ($additionalVendorDir->getLibraries() as $library) {
133
                // prepare the DI configuration filename
134
                $diConfiguration = realpath(sprintf('%s/%s/symfony/Resources/config/services.xml', $additionalVendorDir->getVendorDir(), $library));
135
                // try to load the filename
136
                if (file_exists($diConfiguration)) {
137
                    $customLoader->load($diConfiguration);
138
                } else {
139
                    throw new \Exception(
140
                        sprintf(
141
                            'Can\'t load DI configuration for library "%s"',
142
                            $diConfiguration
143
                        )
144
                    );
145
                }
146
            }
147
        }
148
    }
149
}
150