Completed
Push — master ( 77675a...fe304c )
by Yo
02:42
created

ConfigurationFileLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 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\ConfigurationFileWriter;
12
13
class ConfigurationFileLoader implements ConfigurationFileLoaderInterface
14
{
15
    /** @var Finder */
16
    private $finder;
17
    /** @var SerializerInterface */
18
    private $serializer;
19
20 3
    public function __construct(Finder $finder, SerializerInterface $serializer)
21
    {
22 3
        $this->finder = $finder;
23 3
        $this->serializer = $serializer;
24 3
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function fromPath($path)
30
    {
31 2
        $finder = $this->finder
32 2
            ->in($path)
33 2
            ->files()
34 2
            ->name(ConfigurationFileWriter::FILENAME)
35 2
            ->depth(0);
36
37
        /** @var SplFileInfo|null $file */
38 2
        $file = null;
39 2
        foreach ($finder as $result) {
40 1
            $file = $result;
41 1
            break;
42 2
        }
43
44 2
        if (null === $file) {
45 1
            throw new FileNotFoundException(
46 1
                null,
47 1
                0,
48 1
                null,
49 1
                sprintf(
50 1
                    '%s/%s',
51 1
                    trim($path, '/'),
52
                    ConfigurationFileWriter::FILENAME
53 1
                )
54 1
            );
55
        }
56
57 1
        return $this->fromString($file->getContents());
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function fromString($serializedConfiguration)
64
    {
65 2
        return $this->serializer->deserialize(
66 2
            $serializedConfiguration,
67 2
            ConfigurationFile::class,
68
            ComposerEncoder::FORMAT
69 2
        );
70
    }
71
}
72