Completed
Pull Request — master (#89)
by Tim
02:19
created

SubjectFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 46
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createSubject() 0 18 2
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 Symfony\Component\DependencyInjection\ContainerInterface;
24
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
25
26
/**
27
 * A generic subject factory implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class SubjectFactory implements SubjectFactoryInterface
36
{
37
38
    /**
39
     * The DI container instance.
40
     *
41
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
42
     */
43
    protected $container;
44
45
    /**
46
     * Initialize the factory with the DI container instance.
47
     *
48
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance
49
     */
50 1
    public function __construct(ContainerInterface $container)
51
    {
52 1
        $this->container = $container;
53 1
    }
54
55
    /**
56
     * Factory method to create new subject instance.
57
     *
58
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration
59
     *
60
     * @return \TechDivision\Import\Subjects\SubjectInterface The subject instance
61
     */
62 1
    public function createSubject(SubjectConfigurationInterface $subjectConfiguration)
63
    {
64
65
        // load the subject instance from the DI container and set the subject configuration
66 1
        $subjectInstance = $this->container->get($subjectConfiguration->getId());
67 1
        $subjectInstance->setConfiguration($subjectConfiguration);
68
69
        // load the import adapter instance from the DI container and set it on the subject instance
70 1
        $subjectInstance->setImportAdapter($this->container->get($subjectConfiguration->getImportAdapter()->getId()));
71
72
        // query whether or not we've a subject instance that implements the exportable subject interface
73 1
        if ($subjectInstance instanceof ExportableSubjectInterface) {
74 1
            $subjectInstance->setExportAdapter($this->container->get($subjectConfiguration->getExportAdapter()->getId()));
75
        }
76
77
        // return the initialized subject instance
78 1
        return $subjectInstance;
79
    }
80
}
81