Completed
Pull Request — master (#94)
by Tim
05:44
created

createFilesystemAdapter()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
ccs 0
cts 12
cp 0
rs 8.9713
cc 2
eloc 9
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Adapter\LeagueFilesystemAdapterFactory
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\Adapter;
22
23
use League\Flysystem\Filesystem;
24
use TechDivision\Import\Utils\ConfigurationKeys;
25
use TechDivision\Import\Utils\ConfigurationUtil;
26
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
27
28
/**
29
 * A generic filesystem adapter factory implementation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 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 LeagueFilesystemAdapterFactory implements FilesystemAdapterFactoryInterface
38
{
39
40
    /**
41
     * Factory method to create new filesystem adapter instance.
42
     *
43
     * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration
44
     *
45
     * @return \TechDivision\Import\Adapter\FilesystemAdapterInterface The filesystem adapter instance
46
     */
47
    public function createFilesystemAdapter(SubjectConfigurationInterface $subjectConfiguration)
48
    {
49
50
        // the filesystem adapter configuration
51
        $filesystemAdapterConfiguration = $subjectConfiguration->getFilesystemAdapter();
52
53
        // load the filesystem adapter's adapter configuration (FS specific)
54
        $adapterConfiguration = $filesystemAdapterConfiguration->getAdapter();
55
56
        // load the adapter parameters
57
        $adapterParams = $adapterConfiguration->getParams();
58
59
        // initialize the root directory, if not specified in the adapter parameters
60
        if (!isset($adapterParams[ConfigurationKeys::ROOT])) {
61
            $adapterParams[ConfigurationKeys::ROOT] = getcwd();
62
        }
63
64
        // load the adapter to use
65
        $reflectionClass = new \ReflectionClass($adapterConfiguration->getType());
66
        $adapter =  $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $adapterParams));
67
68
        // create a new filesystem instance
69
        return new LeagueFilesystemAdapter(new Filesystem($adapter));
70
    }
71
}
72