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

OkFileHandlerFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 80
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getContainer() 0 4 1
A getLoaderFactory() 0 4 1
A createHandler() 0 23 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Handlers\OkFileHandlerFactory
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\Handlers;
22
23
use TechDivision\Import\Loaders\LoaderFactoryInterface;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use TechDivision\Import\Adapter\FilesystemAdapterFactoryInterface;
26
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
27
28
/**
29
 * A .OK file handler factory implementation.
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 OkFileHandlerFactory implements HandlerFactoryInterface
38
{
39
40
    /**
41
     * The DI container instance.
42
     *
43
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
44
     */
45
    private $container;
46
47
    /**
48
     * The loader factory instance used to create the loader for the proposed .OK filenames.
49
     *
50
     * @var \TechDivision\Import\Loaders\LoaderFactoryInterface
51
     */
52
    private $loaderFactory;
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       $loaderFactory The loader factory instance used to create the loader for the proposed .OK filenames
59
     */
60
    public function __construct(
61
        ContainerInterface $container,
62
        LoaderFactoryInterface $loaderFactory
63
    ) {
64
        $this->container = $container;
65
        $this->loaderFactory = $loaderFactory;
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 loader factory instance.
80
     *
81
     * @return  \TechDivision\Import\Loaders\LoaderFactoryInterface The loader factory instance
82
     */
83
    protected function getLoaderFactory() : LoaderFactoryInterface
84
    {
85
        return $this->loaderFactory;
86
    }
87
88
    /**
89
     * Create's and return's a new handler instance.
90
     *
91
     * @return \TechDivision\Import\Handlers\HandlerInterface|null The new handler instance
92
     */
93
    public function createHandler(SubjectConfigurationInterface $subject = null) : HandlerInterface
94
    {
95
96
        // load the proposed .OK file loader
97
        $proposedOkFileLoader = $this->getLoaderFactory()->createLoader($subject);
0 ignored issues
show
Unused Code introduced by
The call to LoaderFactoryInterface::createLoader() 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...
98
99
        // load the filesystem adapter instance
100
        $filesystemAdapter = $this->getContainer()->get($subject->getFilesystemAdapter()->getId());
0 ignored issues
show
Bug introduced by
It seems like $subject is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
101
102
        // query whether or not we've a factory instance
103
        if ($filesystemAdapter instanceof FilesystemAdapterFactoryInterface) {
104
            $filesystemAdapter = $filesystemAdapter->createFilesystemAdapter($subject);
0 ignored issues
show
Bug introduced by
It seems like $subject defined by parameter $subject on line 93 can be null; however, TechDivision\Import\Adap...eateFilesystemAdapter() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
105
        }
106
107
        // create a new .OK file handler instance
108
        $handler = new OkFileHandler();
109
        $handler->setLoader($proposedOkFileLoader);
0 ignored issues
show
Compatibility introduced by
$proposedOkFileLoader of type object<TechDivision\Impo...oaders\LoaderInterface> is not a sub-type of object<TechDivision\Impo...ilteredLoaderInterface>. It seems like you assume a child interface of the interface TechDivision\Import\Loaders\LoaderInterface 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...
110
        $handler->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...
111
        $handler->setFileResolverConfiguration($subject->getFileResolver());
112
113
        // return the new .OK file handler instance
114
        return $handler;
115
    }
116
}
117