Completed
Push — 16.x ( bd3602...588b8a )
by Tim
02:11
created

FileResolverFactory::getSourceDir()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 14
loc 14
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 9.7998
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\FileResolver\FileResolverFactory
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\FileResolver;
22
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use TechDivision\Import\Adapter\FilesystemAdapterFactoryInterface;
25
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
26
use TechDivision\Import\Handlers\HandlerFactoryInterface;
27
use TechDivision\Import\Loaders\FilteredLoader;
28
use TechDivision\Import\Loaders\PregMatchFilteredLoader;
29
use TechDivision\Import\Loaders\FilesystemLoader;
30
use TechDivision\Import\Loaders\Filters\OkFileFilter;
31
use TechDivision\Import\Adapter\FilesystemAdapterInterface;
32
use TechDivision\Import\Utils\RegistryKeys;
33
use TechDivision\Import\Services\RegistryProcessorInterface;
34
35
/**
36
 * Factory for file resolver instances.
37
 *
38
 * @author    Tim Wagner <[email protected]>
39
 * @copyright 2016 TechDivision GmbH <[email protected]>
40
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
41
 * @link      https://github.com/techdivision/import
42
 * @link      http://www.techdivision.com
43
 */
44
class FileResolverFactory implements FileResolverFactoryInterface
45
{
46
47
    /**
48
     * The DI container instance.
49
     *
50
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
51
     */
52
    private $container;
53
54
    /**
55
     * The .OK file handler factory instance
56
     *
57
     * @var \TechDivision\Import\Handlers\HandlerFactoryInterface
58
     */
59
    private $handlerFactory;
60
61
    /**
62
     * The registry processor instance.
63
     *
64
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
65
     */
66
    private $registryProcessor;
67
68
    /**
69
     * Initialize the factory with the DI container instance.
70
     *
71
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container         The DI container instance
72
     * @param \TechDivision\Import\Handlers\HandlerFactoryInterface     $handlerFactory    The handler factory instance
73
     * @param \TechDivision\Import\Services\RegistryProcessorInterface  $registryProcessor The registry processor instance
74
     */
75
    public function __construct(
76
        ContainerInterface $container,
77
        HandlerFactoryInterface $handlerFactory,
78
        RegistryProcessorInterface $registryProcessor
79
    ) {
80
        $this->container = $container;
81
        $this->handlerFactory = $handlerFactory;
82
        $this->registryProcessor = $registryProcessor;
83
    }
84
85
    /**
86
     * Return's the container instance.
87
     *
88
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance
89
     */
90
    protected function getContainer() : ContainerInterface
91
    {
92
        return $this->container;
93
    }
94
95
    /**
96
     * Return's the registry processor instance.
97
     *
98
     * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance
99
     */
100
    protected function getRegistryProcessor() : RegistryProcessorInterface
101
    {
102
        return $this->registryProcessor;
103
    }
104
105
    /**
106
     * Return's the .OK file handler factory instance.
107
     *
108
     * @return  \TechDivision\Import\Handlers\HandlerFactoryInterface The .OK file handler factory instance
109
     */
110
    protected function getHandlerFactory() : HandlerFactoryInterface
111
    {
112
        return $this->handlerFactory;
113
    }
114
115
    /**
116
     * Return's the actual source directory.
117
     *
118
     * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter to validate the source directory with
119
     *
120
     * @return string The actual source directory
121
     * @throws \Exception Is thrown, if the actual source directory can not be loaded
122
     */
123 View Code Duplication
    protected function getSourceDir(FilesystemAdapterInterface $filesystemAdapter) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
124
    {
125
126
        // try to load the actual status
127
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
128
129
        // query whether or not the configured source directory is available
130
        if (is_array($status) && $filesystemAdapter->isDir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) {
131
            return $sourceDir;
132
        }
133
134
        // throw an exception if the the actual source directory can not be loaded
135
        throw new \Exception(sprintf('Can\'t load source directory to create file writer instance for'));
136
    }
137
138
    /**
139
     * Creates and returns the file resolver instance for the subject with the passed configuration.
140
     *
141
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject to create the file resolver for
142
     *
143
     * @return \TechDivision\Import\Subjects\FileResolver\FileResolverInterface The file resolver instance
144
     */
145
    public function createFileResolver(SubjectConfigurationInterface $subject) : FileResolverInterface
146
    {
147
148
        // load the DI container instance
149
        $container = $this->getContainer();
150
151
        // load the proposed .OK file loader
152
        $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...
153
154
        // load the filesystem adapter instance
155
        $filesystemAdapter = $container->get($subject->getFilesystemAdapter()->getId());
156
157
        // query whether or not we've a factory instance
158
        /** \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter */
159
        if ($filesystemAdapter instanceof FilesystemAdapterFactoryInterface) {
160
            $filesystemAdapter = $filesystemAdapter->createFilesystemAdapter($subject);
161
        }
162
163
        // initialize the filter for the files that has to be imported
164
        // AND matches the .OK file, if one has been requested
165
        $filter = new OkFileFilter($handler, $subject, $this->getSourceDir($filesystemAdapter));
0 ignored issues
show
Documentation introduced by
$filesystemAdapter is of type object|null, but the function expects a object<TechDivision\Impo...systemAdapterInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Compatibility introduced by
$handler of type object<TechDivision\Impo...dlers\HandlerInterface> is not a sub-type of object<TechDivision\Impo...OkFileHandlerInterface>. It seems like you assume a child interface of the interface TechDivision\Import\Handlers\HandlerInterface 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...
166
167
        // initialize the loader for the files that has to be imported
168
        $filesystemLoader = new FilesystemLoader($filesystemAdapter);
0 ignored issues
show
Documentation introduced by
$filesystemAdapter is of type object|null, but the function expects a object<TechDivision\Impo...systemAdapterInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
        $filteredLoader = new FilteredLoader($filesystemLoader);
170
        $pregMatchFilteredloader = new PregMatchFilteredLoader($filteredLoader);
171
        $pregMatchFilteredloader->addFilter($filter);
172
173
        // create a new file resolver instance for the subject with the passed configuration
174
        /** @var \TechDivision\Import\Subjects\FileResolver\FileResolverInterface $fileResolver */
175
        $fileResolver = $container->get($subject->getFileResolver()->getId());
176
        $fileResolver->setFilesystemAdapter($filesystemAdapter);
0 ignored issues
show
Documentation introduced by
$filesystemAdapter is of type object|null, but the function expects a object<TechDivision\Impo...systemAdapterInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177
        $fileResolver->setSubjectConfiguration($subject);
178
        $fileResolver->setLoader($pregMatchFilteredloader);
179
180
        // return the file resolver instance
181
        return $fileResolver;
182
    }
183
}
184