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

ConfigurationLoader::fromPath()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 4
nop 1
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