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

ConfigurationFileLoader::fromPath()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 20
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\ConfigurationFileLoaderInterface;
9
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile;
10
use Yoanm\ComposerConfigManager\Infrastructure\Serializer\Encoder\ComposerEncoder;
11
use Yoanm\ComposerConfigManager\Infrastructure\Writer\ConfigurationWriter;
12
13
class ConfigurationFileLoader implements ConfigurationFileLoaderInterface
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
        $finder = $this->finder
32
            ->in($path)
33
            ->files()
34
            ->name(ConfigurationWriter::FILENAME)
35
            ->depth(0);
36
37
        /** @var SplFileInfo|null $file */
38
        $file = null;
39
        foreach ($finder as $result) {
40
            $file = $result;
41
            break;
42
        }
43
44
        if (null === $file) {
45
            throw new FileNotFoundException(
46
                null,
47
                0,
48
                null,
49
                sprintf(
50
                    '%s/%s',
51
                    trim($path, '/'),
52
                    ConfigurationWriter::FILENAME
53
                )
54
            );
55
        }
56
57
        return $this->fromString($file->getContents());
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function fromString($serializedConfiguration)
64
    {
65
        return $this->serializer->deserialize($serializedConfiguration, ConfigurationFile::class, ComposerEncoder::FORMAT);
66
    }
67
}
68