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

ConfigurationLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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->files()
34
            ->in($path)
35
            ->name(ConfigurationWriter::FILENAME);
36
37
        foreach ($finder as $match) {
38
            $file = $match;
39
        }
40
41
        if (null === $file) {
42
            throw new FileNotFoundException(
43
                null,
44
                0,
45
                null,
46
                sprintf(
47
                    'File %s not found in %s',
48
                    ConfigurationWriter::FILENAME,
49
                    $path
50
                )
51
            );
52
        }
53
54
        return $this->serializer->deserialize($file->getContents(), Configuration::class, ComposerEncoder::FORMAT);
55
    }
56
}
57