Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

RecursiveFileLocator::locateFilesNamed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ThemeBundle\Locator;
13
14
use Sylius\Bundle\ThemeBundle\Factory\FinderFactoryInterface;
15
use Symfony\Component\Finder\Finder;
16
use Symfony\Component\Finder\SplFileInfo;
17
18
/**
19
 * @author Kamil Kokot <[email protected]>
20
 */
21
final class RecursiveFileLocator implements FileLocatorInterface
22
{
23
    /**
24
     * @var FinderFactoryInterface
25
     */
26
    private $finderFactory;
27
28
    /**
29
     * @var array
30
     */
31
    private $paths;
32
33
    /**
34
     * @param FinderFactoryInterface $finderFactory
35
     * @param array $paths An array of paths where to look for resources
36
     */
37
    public function __construct(FinderFactoryInterface $finderFactory, array $paths)
38
    {
39
        $this->finderFactory = $finderFactory;
40
        $this->paths = $paths;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function locateFileNamed($name)
47
    {
48
        return $this->doLocateFilesNamed($name)->current();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function locateFilesNamed($name)
55
    {
56
        return iterator_to_array($this->doLocateFilesNamed($name));
57
    }
58
59
    /**
60
     * @param string $name
61
     *
62
     * @return \Generator
63
     */
64
    private function doLocateFilesNamed($name)
65
    {
66
        $this->assertNameIsNotEmpty($name);
67
68
        $finder = $this->finderFactory->create();
69
        $finder
70
            ->files()
71
            ->name($name)
72
            ->ignoreUnreadableDirs()
73
            ->in($this->paths);
74
75
        $this->assertThatAtLeastOneFileWasFound($finder, $name);
76
77
        /** @var SplFileInfo $file */
78
        foreach ($finder as $file) {
79
            yield $file->getPathname();
80
        }
81
    }
82
83
    /**
84
     * @param string $name
85
     */
86
    private function assertNameIsNotEmpty($name)
87
    {
88
        if (null === $name || '' === $name) {
89
            throw new \InvalidArgumentException(
90
                'An empty file name is not valid to be located.'
91
            );
92
        }
93
    }
94
95
    /**
96
     * @param Finder $finder
97
     * @param string $name
98
     */
99
    private function assertThatAtLeastOneFileWasFound(Finder $finder, $name)
100
    {
101
        if (0 === $finder->count()) {
102
            throw new \InvalidArgumentException(sprintf(
103
                'The file "%s" does not exist (searched in the following directories: %s).',
104
                $name,
105
                implode(', ', $this->paths)
106
            ));
107
        }
108
    }
109
}
110