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

getRegistryProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Loaders\ProposedOkFilenameLoaderFactory
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\Loaders;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Adapter\FilesystemAdapterInterface;
25
use TechDivision\Import\Services\RegistryProcessorInterface;
26
use TechDivision\Import\Loaders\Filters\DefaultOkFileFilter;
27
use TechDivision\Import\Loaders\Sorters\DefaultOkFileSorter;
28
use Symfony\Component\DependencyInjection\ContainerInterface;
29
use TechDivision\Import\Adapter\FilesystemAdapterFactoryInterface;
30
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
31
32
/**
33
 * Factory implementation for a loader for the proposed .OK filenames.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2020 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import
39
 * @link      http://www.techdivision.com
40
 */
41
class ProposedOkFilenameLoaderFactory implements LoaderFactoryInterface
42
{
43
44
    /**
45
     * The DI container instance.
46
     *
47
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
48
     */
49
    private $container;
50
51
    /**
52
     * The registry processor instance.
53
     *
54
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
55
     */
56
    private $registryProcessor;
57
58
    /**
59
     * Initialize the factory with the DI container instance.
60
     *
61
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container         The DI container instance
62
     * @param \TechDivision\Import\Services\RegistryProcessorInterface  $registryProcessor The registry processor instance
63
     */
64
    public function __construct(
65
        ContainerInterface $container,
66
        RegistryProcessorInterface $registryProcessor
67
    ) {
68
        $this->container = $container;
69
        $this->registryProcessor = $registryProcessor;
70
    }
71
72
    /**
73
     * Return's the container instance.
74
     *
75
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance
76
     */
77
    protected function getContainer() : ContainerInterface
78
    {
79
        return $this->container;
80
    }
81
82
    /**
83
     * Return's the registry processor instance.
84
     *
85
     * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance
86
     */
87
    protected function getRegistryProcessor() : RegistryProcessorInterface
88
    {
89
        return $this->registryProcessor;
90
    }
91
92
    /**
93
     * Return's the actual source directory.
94
     *
95
     * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter to validate the source directory with
96
     *
97
     * @return string The actual source directory
98
     * @throws \Exception Is thrown, if the actual source directory can not be loaded
99
     */
100
    protected function getSourceDir(FilesystemAdapterInterface $filesystemAdapter) : string
101
    {
102
103
        // try to load the actual status
104
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
105
106
        // query whether or not the configured source directory is available
107
        if (is_array($status) && $filesystemAdapter->isDir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) {
108
            return $sourceDir;
109
        }
110
111
        // throw an exception if the the actual source directory can not be loaded
112
        throw new \Exception(sprintf('Can\'t load source directory to create file writer instance for'));
113
    }
114
115
    /**
116
     * Create's and return's the apropriate loader instance.
117
     *
118
     * @return \TechDivision\Import\Loaders\LoaderInterface|null The loader instance
119
     */
120
    public function createLoader(SubjectConfigurationInterface $subject = null) : LoaderInterface
121
    {
122
123
        // load the filesystem adapter instance
124
        $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...
125
126
        // query whether or not we've a factory instance
127
        if ($filesystemAdapter instanceof FilesystemAdapterFactoryInterface) {
128
            $filesystemAdapter = $filesystemAdapter->createFilesystemAdapter($subject);
0 ignored issues
show
Bug introduced by
It seems like $subject defined by parameter $subject on line 120 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...
129
        }
130
131
        // initialize the chain with the loader instances
132
        $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...
133
        $filteredLoader = new FilteredLoader($filesystemLoader);
134
        $pregMatchFilteredLoader = new PregMatchFilteredLoader($filteredLoader);
135
        $proposedOkFileLoader = new ProposedOkFilenameLoader($pregMatchFilteredLoader);
136
        $proposedOkFileLoader->addFilter(new DefaultOkFileFilter());
137
        $proposedOkFileLoader->addSorter(new DefaultOkFileSorter());
138
        $proposedOkFileLoader->setSourceDir($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...
139
        $proposedOkFileLoader->setFileResolverConfiguration($subject->getFileResolver());
140
141
        // return the new loader instance
142
        return $proposedOkFileLoader;
143
    }
144
}
145