ConfigurationFinder::find()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dandelion\Configuration;
6
7
use Dandelion\Exception\ConfigurationFileNotFoundException;
8
use Dandelion\Exception\RuntimeException;
9
use Dandelion\Filesystem\FilesystemInterface;
10
use SplFileInfo;
11
use Symfony\Component\Finder\Finder as SymfonyFinder;
12
13
class ConfigurationFinder implements ConfigurationFinderInterface
14
{
15
    /**
16
     * @var \Symfony\Component\Finder\Finder
17
     */
18
    protected $symfonyFinder;
19
20
    /**
21
     * @var \Dandelion\Filesystem\FilesystemInterface
22
     */
23
    protected $filesystem;
24
25
    /**
26
     * @param \Symfony\Component\Finder\Finder $symfonyFinder
27
     * @param \Dandelion\Filesystem\FilesystemInterface $filesystem
28
     */
29
    public function __construct(
30
        SymfonyFinder $symfonyFinder,
31
        FilesystemInterface $filesystem
32
    ) {
33
        $this->symfonyFinder = $symfonyFinder;
34
        $this->filesystem = $filesystem;
35
    }
36
37
    /**
38
     * @return \SplFileInfo
39
     *
40
     * @throws \Dandelion\Exception\ConfigurationFileNotFoundException
41
     * @throws \Exception
42
     */
43
    public function find(): SplFileInfo
44
    {
45
        $this->symfonyFinder->files()
46
            ->in($this->filesystem->getCurrentWorkingDirectory())
47
            ->name('dandelion.json')
48
            ->depth('== 0');
49
50
        if ($this->symfonyFinder->count() !== 1) {
51
            throw new ConfigurationFileNotFoundException('Configuration "dandelion.json" not found.');
52
        }
53
54
        $iterator = $this->symfonyFinder->getIterator();
55
        $iterator->rewind();
56
57
        if (!$iterator->valid()) {
58
            throw new RuntimeException('Current position is not valid.');
59
        }
60
61
        return $iterator->current();
62
    }
63
}
64