Completed
Push — master ( b95763...2cb7d7 )
by Nikola
29:49
created

findQuerySourcesInDirectory()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 4
nop 2
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\Twig\CacheWarmer;
11
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\HttpKernel\KernelInterface;
14
15
/**
16
 * Class QuerySourcesIterator
17
 *
18
 * Iterator for all query resources in bundles and in the application Resources/query directory.
19
 *
20
 * @package RunOpenCode\Bundle\QueryResourcesLoader\Twig
21
 */
22
class QuerySourcesIterator implements \IteratorAggregate
23
{
24
    private $kernel;
25
    private $rootDir;
26
    private $queries;
27
    private $paths;
28
29
    /**
30
     * @param KernelInterface $kernel  A KernelInterface instance
31
     * @param string          $rootDir The directory where global query sources can be stored
32
     * @param array           $paths   Additional Twig paths to warm
33
     */
34
    public function __construct(KernelInterface $kernel, $rootDir, array $paths = array())
35
    {
36
        $this->kernel = $kernel;
37
        $this->rootDir = $rootDir;
38
        $this->paths = $paths;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getIterator()
45
    {
46
        if (null !== $this->queries) {
47
            return $this->queries;
48
        }
49
50
        $this->queries = $this->findQuerySourcesInDirectory($this->rootDir.'/Resources/query');
51
        foreach ($this->kernel->getBundles() as $bundle) {
52
            $name = $bundle->getName();
53 View Code Duplication
            if ('Bundle' === substr($name, -6)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
                $name = substr($name, 0, -6);
55
            }
56
57
            $this->queries = array_merge(
58
                $this->queries,
59
                $this->findQuerySourcesInDirectory($bundle->getPath().'/Resources/query', $name),
60
                $this->findQuerySourcesInDirectory($this->rootDir.'/'.$bundle->getName().'/query', $name)
61
            );
62
        }
63
64
        foreach ($this->paths as $dir => $namespace) {
65
            $this->queries = array_merge($this->queries, $this->findQuerySourcesInDirectory($dir, $namespace));
66
        }
67
68
        return $this->queries = new \ArrayIterator(array_unique($this->queries));
69
    }
70
71
    /**
72
     * Find query sources in the given directory.
73
     *
74
     * @param string      $dir       The directory where to look for query sources
75
     * @param string|null $namespace The query source namespace
76
     *
77
     * @return array
78
     */
79
    private function findQuerySourcesInDirectory($dir, $namespace = null)
80
    {
81
        if (!is_dir($dir)) {
82
            return array();
83
        }
84
85
        $templates = array();
86
        foreach (Finder::create()->files()->followLinks()->in($dir) as $file) {
87
            $templates[] = (null !== $namespace ? '@'.$namespace.'/' : '').str_replace('\\', '/', $file->getRelativePathname());
88
        }
89
90
        return $templates;
91
    }
92
}
93