Completed
Push — 8.x ( 824af6 )
by Tim
09:11
created

SubjectFactory::createSubject()   C

Complexity

Conditions 9
Paths 51

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 15.0484

Importance

Changes 0
Metric Value
dl 0
loc 78
c 0
b 0
f 0
ccs 22
cts 38
cp 0.5789
rs 6.9244
cc 9
nc 51
nop 1
crap 15.0484

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\SubjectFactory
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\Subjects;
22
23
use Doctrine\Common\Collections\Collection;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use TechDivision\Import\SystemLoggerTrait;
26
use TechDivision\Import\Adapter\ImportAdapterInterface;
27
use TechDivision\Import\Adapter\ExportAdapterInterface;
28
use TechDivision\Import\Adapter\ImportAdapterFactoryInterface;
29
use TechDivision\Import\Adapter\ExportAdapterFactoryInterface;
30
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
31
use TechDivision\Import\Subjects\I18n\DateConverterFactoryInterface;
32
use TechDivision\Import\Subjects\I18n\NumberConverterFactoryInterface;
33
34
/**
35
 * A generic subject factory implementation.
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2016 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/techdivision/import
41
 * @link      http://www.techdivision.com
42
 */
43
class SubjectFactory implements SubjectFactoryInterface
44
{
45
46
    /**
47
     * The trait that provides basic filesystem handling functionality.
48
     *
49
     * @var TechDivision\Import\SystemLoggerTrait
50
     */
51
    use SystemLoggerTrait;
52
53
    /**
54
     * The DI container instance.
55
     *
56
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
57
     */
58
    protected $container;
59
60
    /**
61
     * The number converter factory instance.
62
     *
63
     * @var \TechDivision\Import\Subjects\I18n\NumberConverterFactoryInterface
64
     */
65
    protected $numberConverterFactory;
66
67
    /**
68
     * The date converter factory instance.
69
     *
70
     * @var \TechDivision\Import\Subjects\I18n\DateConverterFactoryInterface
71
     */
72
    protected $dateConverterFactory;
73
74
    /**
75
     * Initialize the factory with the DI container instance.
76
     *
77
     * @param \Symfony\Component\DependencyInjection\ContainerInterface          $container              The DI container instance
78
     * @param \Doctrine\Common\Collections\Collection                            $systemLoggers          The collection with the system loggers instances
79
     * @param \TechDivision\Import\Subjects\I18n\NumberConverterFactoryInterface $numberConverterFactory The number converter factory instance
80
     * @param \TechDivision\Import\Subjects\I18n\DateConverterFactoryInterface   $dateConverterFactory   The date converter factory instance
81
     */
82 1
    public function __construct(
83
        ContainerInterface $container,
84
        Collection $systemLoggers,
85
        NumberConverterFactoryInterface $numberConverterFactory,
86
        DateConverterFactoryInterface $dateConverterFactory
87
    ) {
88 1
        $this->container = $container;
89 1
        $this->systemLoggers = $systemLoggers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $systemLoggers of type object<Doctrine\Common\Collections\Collection> is incompatible with the declared type array of property $systemLoggers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90 1
        $this->numberConverterFactory = $numberConverterFactory;
91 1
        $this->dateConverterFactory = $dateConverterFactory;
92 1
    }
93
94
    /**
95
     * Factory method to create new subject instance.
96
     *
97
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration
98
     *
99
     * @return \TechDivision\Import\Subjects\SubjectInterface The subject instance
100
     */
101 1
    public function createSubject(SubjectConfigurationInterface $subjectConfiguration)
102
    {
103
104
        // load the subject instance from the DI container and set the subject configuration
105 1
        $subjectInstance = $this->container->get($subjectConfiguration->getId());
106 1
        $subjectInstance->setConfiguration($subjectConfiguration);
107
108
        // load the import adapter instance from the DI container and set it on the subject instance
109 1
        $importAdapter = $this->container->get($subjectConfiguration->getImportAdapter()->getId());
110
111
        // query whether or not we've found a factory or the instance itself
112 1
        if ($importAdapter instanceof ImportAdapterInterface) {
113 1
            $subjectInstance->setImportAdapter($importAdapter);
114
            // log a warning, that this is deprecated
115 1
            $this->getSystemLogger()->warning(
116 1
                sprintf(
117 1
                    'Direct injection of import adapter with DI ID "%s" is deprecated since version 3.0.0, please use factory instead',
118 1
                    $subjectConfiguration->getImportAdapter()->getId()
119
                )
120
            );
121
        } elseif ($importAdapter instanceof ImportAdapterFactoryInterface) {
122
            $subjectInstance->setImportAdapter($importAdapter->createImportAdapter($subjectConfiguration));
123
        } else {
124
            throw new \Exception(
125
                sprintf(
126
                    'Expected either an instance of ImportAdapterInterface or ImportAdapterFactoryInterface for DI ID "%s"',
127
                    $subjectConfiguration->getImportAdapter()->getId()
128
                )
129
            );
130
        }
131
132
        // query whether or not we've a subject instance that implements the exportable subject interface
133 1
        if ($subjectInstance instanceof ExportableSubjectInterface) {
134
            // load the export adapter instance from the DI container and set it on the subject instance
135 1
            $exportAdapter = $this->container->get($subjectConfiguration->getExportAdapter()->getId());
136
137
            // query whether or not we've found a factory or the instance itself
138 1
            if ($exportAdapter instanceof ExportAdapterInterface) {
139
                // inject the export adapter into the subject
140 1
                $subjectInstance->setExportAdapter($exportAdapter);
141
                // log a warning, that this is deprecated
142 1
                $this->getSystemLogger()->warning(
143 1
                    sprintf(
144 1
                        'Direct injection of export adapter with DI ID "%s" is deprecated since version 3.0.0, please use factory instead',
145 1
                        $subjectConfiguration->getExportAdapter()->getId()
146
                    )
147
                );
148
            } elseif ($exportAdapter instanceof ExportAdapterFactoryInterface) {
149
                $subjectInstance->setExportAdapter($exportAdapter->createExportAdapter($subjectConfiguration));
150
            } else {
151
                throw new \Exception(
152
                    sprintf(
153
                        'Expected either an instance of ExportAdapterInterface or ExportAdapterFactoryInterface for DI ID "%s"',
154
                        $subjectConfiguration->getExportAdapter()->getId()
155
                    )
156
                );
157
            }
158
        }
159
160
        // load te number converter instance from the DI container and set it on the subject instance
161 1
        if ($subjectInstance instanceof NumberConverterSubjectInterface) {
162
            $subjectInstance->setNumberConverter($this->numberConverterFactory->createNumberConverter($subjectConfiguration));
163
        }
164
165
        // load te date converter instance from the DI container and set it on the subject instance
166 1
        if ($subjectInstance instanceof DateConverterSubjectInterface) {
167
            $subjectInstance->setDateConverter($this->dateConverterFactory->createDateConverter($subjectConfiguration));
168
        }
169
170
        // load the filesystem adapter instance from the DI container and set it on the subject instance
171 1
        if ($subjectInstance instanceof FilesystemSubjectInterface) {
172
            $filesystemAdapterFactory = $this->container->get($subjectConfiguration->getFilesystemAdapter()->getId());
173
            $subjectInstance->setFilesystemAdapter($filesystemAdapterFactory->createFilesystemAdapter($subjectConfiguration));
174
        }
175
176
        // return the initialized subject instance
177 1
        return $subjectInstance;
178
    }
179
}
180