Test Failed
Pull Request — master (#10)
by Yo
02:35
created

ConfigurationLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B fromPath() 0 29 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
    public function __construct(Finder $finder, SerializerInterface $serializer)
21
    {
22
        $this->finder = $finder;
23
        $this->serializer = $serializer;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function fromPath($path)
30
    {
31
        /** @var SplFileInfo|null $file */
32
        $file = null;
33
        $finder = $this->finder
34
            ->in($path)
35
            ->files()
36
            ->name(ConfigurationWriter::FILENAME)
37
            ->depth(0);
38
39
        foreach ($finder as $match) {
40
            $file = $match;
41
        }
42
43
        if (null === $file) {
44
            throw new FileNotFoundException(
45
                null,
46
                0,
47
                null,
48
                sprintf(
49
                    'File %s not found in %s',
50
                    ConfigurationWriter::FILENAME,
51
                    $path
52
                )
53
            );
54
        }
55
56
        return $this->serializer->deserialize($file->getContents(), Configuration::class, ComposerEncoder::FORMAT);
57
    }
58
}
59