Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

PluginFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 50 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 7
dl 45
loc 90
rs 10
c 0
b 0
f 0
ccs 0
cts 50
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B createPlugin() 45 62 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * TechDivision\Import\Plugins\PluginFactory
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Plugins;
22
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use TechDivision\Import\Configuration\PluginConfigurationInterface;
25
use TechDivision\Import\Adapter\ImportAdapterInterface;
26
use TechDivision\Import\Adapter\ImportAdapterFactoryInterface;
27
use TechDivision\Import\Adapter\ExportAdapterInterface;
28
use TechDivision\Import\Adapter\ExportAdapterFactoryInterface;
29
30
/**
31
 * A generic plugin factory 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
37
 * @link      http://www.techdivision.com
38
 */
39
class PluginFactory implements PluginFactoryInterface
40
{
41
42
    /**
43
     * The DI container instance.
44
     *
45
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
46
     */
47
    protected $container;
48
49
    /**
50
     * Initialize the factory with the DI container instance.
51
     *
52
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance
53
     */
54
    public function __construct(ContainerInterface $container)
55
    {
56
        $this->container = $container;
57
    }
58
59
    /**
60
     * Factory method to create new plugin instance.
61
     *
62
     * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration
63
     *
64
     * @return \TechDivision\Import\Plugins\PluginInterface The plugin instance
65
     */
66
    public function createPlugin(PluginConfigurationInterface $pluginConfiguration)
67
    {
68
69
        // load the plugin instance from the DI container and set the plugin configuration
70
        $pluginInstance = $this->container->get($pluginConfiguration->getId());
71
        $pluginInstance->setPluginConfiguration($pluginConfiguration);
72
73
        // load the import adapter instance from the DI container and set it on the plugin instance
74
        $importAdapter = $this->container->get($pluginConfiguration->getImportAdapter()->getId());
75
76
        // query whether or not we've found a factory or the instance itself
77 View Code Duplication
        if ($importAdapter instanceof ImportAdapterInterface) {
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...
78
            $pluginInstance->setImportAdapter($importAdapter);
79
            // log a warning, that this is deprecated
80
            $this->getSystemLogger()->warning(
0 ignored issues
show
Bug introduced by
The method getSystemLogger() does not seem to exist on object<TechDivision\Import\Plugins\PluginFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
                sprintf(
82
                    'Direct injection of import adapter with DI ID "%s" is deprecated since version 3.0.0, please use factory instead',
83
                    $pluginConfiguration->getImportAdapter()->getId()
84
                )
85
            );
86
        } elseif ($importAdapter instanceof ImportAdapterFactoryInterface) {
87
            $pluginInstance->setImportAdapter($importAdapter->createImportAdapter($pluginConfiguration));
88
        } else {
89
            throw new \Exception(
90
                sprintf(
91
                    'Expected either an instance of ImportAdapterInterface or ImportAdapterFactoryInterface for DI ID "%s"',
92
                    $pluginConfiguration->getImportAdapter()->getId()
93
                )
94
            );
95
        }
96
97
        // query whether or not we've a plugin instance that implements the exportable plugin interface
98 View Code Duplication
        if ($pluginInstance instanceof ExportablePluginInterface) {
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...
99
            // load the export adapter instance from the DI container and set it on the plugin instance
100
            $exportAdapter = $this->container->get($pluginConfiguration->getExportAdapter()->getId());
101
102
            // query whether or not we've found a factory or the instance itself
103
            if ($exportAdapter instanceof ExportAdapterInterface) {
104
                // inject the export adapter into the subject
105
                $pluginInstance->setExportAdapter($exportAdapter);
106
                // log a warning, that this is deprecated
107
                $this->getSystemLogger()->warning(
0 ignored issues
show
Bug introduced by
The method getSystemLogger() does not seem to exist on object<TechDivision\Import\Plugins\PluginFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
                    sprintf(
109
                        'Direct injection of export adapter with DI ID "%s" is deprecated since version 3.0.0, please use factory instead',
110
                        $pluginConfiguration->getExportAdapter()->getId()
111
                    )
112
                );
113
            } elseif ($exportAdapter instanceof ExportAdapterFactoryInterface) {
114
                $pluginInstance->setExportAdapter($exportAdapter->createExportAdapter($pluginConfiguration));
115
            } else {
116
                throw new \Exception(
117
                    sprintf(
118
                        'Expected either an instance of ExportAdapterInterface or ExportAdapterFactoryInterface for DI ID "%s"',
119
                        $pluginConfiguration->getExportAdapter()->getId()
120
                    )
121
                );
122
            }
123
        }
124
125
        // return the plugin instance
126
        return $pluginInstance;
127
    }
128
}
129