Passed
Push — master ( 2aad05...4fe473 )
by Yo
02:45
created

ConfigurationLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 47
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B fromPath() 0 30 3
1
<?php
2
namespace Yoanm\ComposerConfigManager\Infrastructure\Loader;
3
4
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Finder\SplFileInfo;
7
use Symfony\Component\Serializer\SerializerInterface;
8
use Yoanm\ComposerConfigManager\Application\Loader\ConfigurationLoaderInterface;
9
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
10
use Yoanm\ComposerConfigManager\Infrastructure\Serializer\Encoder\ComposerEncoder;
11
use Yoanm\ComposerConfigManager\Infrastructure\Writer\ConfigurationWriter;
12
13
class ConfigurationLoader implements ConfigurationLoaderInterface
14
{
15
    /** @var Finder */
16
    private $finder;
17
    /** @var SerializerInterface */
18
    private $serializer;
19
20 2
    public function __construct(Finder $finder, SerializerInterface $serializer)
21
    {
22 2
        $this->finder = $finder;
23 2
        $this->serializer = $serializer;
24 2
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function fromPath($path)
30
    {
31 2
        $finder = $this->finder
32 2
            ->in($path)
33 2
            ->files()
34 2
            ->name(ConfigurationWriter::FILENAME)
35 2
            ->depth(0);
36
37
        /** @var SplFileInfo|null $file */
38 2
        $file = null;
39 2
        foreach ($finder as $result) {
40 1
            $file = $result;
41 1
            break;
42 2
        }
43
44 2
        if (null === $file) {
45 1
            throw new FileNotFoundException(
46 1
                null,
47 1
                0,
48 1
                null,
49 1
                sprintf(
50 1
                    '%s/%s',
51 1
                    $path,
52
                    ConfigurationWriter::FILENAME
53 1
                )
54 1
            );
55
        }
56
57 1
        return $this->serializer->deserialize($file->getContents(), Configuration::class, ComposerEncoder::FORMAT);
58
    }
59
}
60