Completed
Push — 16.x ( 0056bb...bd3602 )
by Tim
02:11 queued 10s
created

FileWriterFactory::createFileWriter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
rs 9.504
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\FileWriter\FileWriterFactory
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 2020 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\FileWriter;
22
23
use TechDivision\Import\Handlers\HandlerFactoryInterface;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use TechDivision\Import\Adapter\FilesystemAdapterFactoryInterface;
26
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
27
28
/**
29
 * Generic factory implementation for file writer instances.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2020 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import
35
 * @link      http://www.techdivision.com
36
 */
37
class FileWriterFactory implements FileWriterFactoryInterface
38
{
39
40
    /**
41
     * The DI container instance.
42
     *
43
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
44
     */
45
    private $container;
46
47
    /**
48
     * The .OK file handler factory instance
49
     *
50
     * @var \TechDivision\Import\Handlers\HandlerFactoryInterface
51
     */
52
    private $handlerFactory;
53
54
    /**
55
     * Initialize the factory with the DI container instance.
56
     *
57
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container      The DI container instance
58
     * @param \TechDivision\Import\Loaders\LoaderFactoryInterface       $handlerFactory The .OK file handler factory instance
59
     */
60
    public function __construct(
61
        ContainerInterface $container,
62
        HandlerFactoryInterface $handlerFactory
63
    ) {
64
        $this->container = $container;
65
        $this->handlerFactory = $handlerFactory;
66
    }
67
68
    /**
69
     * Return's the container instance.
70
     *
71
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance
72
     */
73
    protected function getContainer() : ContainerInterface
74
    {
75
        return $this->container;
76
    }
77
78
    /**
79
     * Return's the .OK file handler factory instance.
80
     *
81
     * @return  \TechDivision\Import\Handlers\HandlerFactoryInterface The .OK file handler factory instance
82
     */
83
    protected function getHandlerFactory() : HandlerFactoryInterface
84
    {
85
        return $this->handlerFactory;
86
    }
87
88
    /**
89
     * Creates and returns the file resolver instance for the subject with the passed configuration.
90
     *
91
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject to create the file resolver for
92
     *
93
     * @return \TechDivision\Import\Subjects\FileWriter\FileWriterInterface The file resolver instance
94
     */
95
    public function createFileWriter(SubjectConfigurationInterface $subject) : FileWriterInterface
96
    {
97
98
        // load the DI container instance
99
        $container = $this->getContainer();
100
101
        // load the proposed .OK file loader
102
        $handler = $this->getHandlerFactory()->createHandler($subject);
0 ignored issues
show
Unused Code introduced by
The call to HandlerFactoryInterface::createHandler() has too many arguments starting with $subject.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103
104
        // load the filesystem adapter instance
105
        $filesystemAdapter = $container->get($subject->getFilesystemAdapter()->getId());
106
107
        // query whether or not we've a factory instance
108
        if ($filesystemAdapter instanceof FilesystemAdapterFactoryInterface) {
109
            $filesystemAdapter = $filesystemAdapter->createFilesystemAdapter($subject);
110
        }
111
112
        // create a new file writer instance for the subject with the passed configuration
113
        $fileWriter = $container->get($subject->getFileWriter()->getId());
114
        $fileWriter->setHandler($handler);
115
        $fileWriter->setFilesystemAdapter($filesystemAdapter);
116
        $fileWriter->setFileResolverConfiguration($subject->getFileResolver());
117
118
        // return the file writer instance
119
        return $fileWriter;
120
    }
121
}
122