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

ConfigurationLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B fromPath() 0 27 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->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