FormatDiscoverLocalFileStorageFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 78.95%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 51
ccs 15
cts 19
cp 0.7895
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B factor() 0 27 7
1
<?php
2
3
namespace Mathielen\ImportEngine\Storage\Factory;
4
5
use Mathielen\ImportEngine\Storage\LocalFileStorage;
6
use Mathielen\ImportEngine\Storage\Format\Discovery\FormatDiscoverStrategyInterface;
7
use Mathielen\ImportEngine\ValueObject\StorageSelection;
8
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
9
use Psr\Log\LoggerInterface;
10
11
class FormatDiscoverLocalFileStorageFactory implements StorageFactoryInterface
12
{
13
    /**
14
     * @var FormatDiscoverStrategyInterface
15
     */
16
    private $formatDiscoverStrategyInterface;
17
18
    /**
19
     * @var LoggerInterface
20
     */
21
    private $logger;
22
23 15
    public function __construct(FormatDiscoverStrategyInterface $formatDiscoverStrategyInterface, LoggerInterface $logger = null)
24
    {
25 15
        $this->formatDiscoverStrategyInterface = $formatDiscoverStrategyInterface;
26 15
        $this->logger = $logger;
27 15
    }
28
29
    /**
30
     * (non-PHPdoc).
31
     *
32
     * @see \Mathielen\ImportEngine\Storage\Factory\StorageFactoryInterface::factor()
33
     */
34 2
    public function factor(StorageSelection $selection)
35
    {
36 2
        $file = $selection->getImpl();
37
38 2
        if (!($file instanceof \SplFileInfo)) {
39
            throw new InvalidConfigurationException('StorageSelection does not contain a SplFileInfo as impl property but this is mandatory for a LocalFileStorage.');
40
        }
41 2
        if (!$file->isFile() || !$file->isReadable()) {
42
            throw new InvalidConfigurationException('StorageSelection references a File that does not exists or is not readable.');
43
        }
44
45 2
        $format = $selection->getMetadata('format');
46 2
        if (!$format) {
47 2
            $format = $this->formatDiscoverStrategyInterface->getFormat($selection);
48 2
            if (!$format) {
49
                throw new InvalidConfigurationException('Could not discover format!');
50
            }
51
52 2
            if ($this->logger) {
53
                $this->logger->info("File $file was discovered as format '$format'", ['selection' => $selection->toArray()]);
54
            }
55
        }
56
57 2
        $localFile = new LocalFileStorage($file, $format);
58
59 2
        return $localFile;
60
    }
61
}
62