ImageEntityClassLocator::getClassName()   C
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 2
nop 0
1
<?php
2
/**
3
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source
8
 * code.
9
 */
10
11
namespace IrishDan\ResponsiveImageBundle;
12
13
use Symfony\Component\HttpKernel\Config\FileLocator;
14
15
/**
16
 * Class ImageEntityClassLocator
17
 *
18
 * The aim is to find entity class which implements ResponsiveImageInterface
19
 *
20
 * @property array bundles
21
 * @package IrishDan\ResponsiveImageBundle
22
 */
23
class ImageEntityClassLocator
24
{
25
    const RESPONSIVE_IMAGE_INTERFACE = 'IrishDan\ResponsiveImageBundle\ResponsiveImageInterface';
26
    /**
27
     * @var null|string
28
     */
29
    protected $className = null;
30
    /**
31
     * @var FileLocator
32
     */
33
    protected $fileLocator;
34
    /**
35
     * @var string
36
     */
37
    protected $entityDirectory = 'Entity';
38
39
    /**
40
     * ImageEntityClassLocator constructor.
41
     *
42
     * @param             $bundles
43
     * @param FileLocator $fileLocator
44
     */
45
    public function __construct($bundles = [], FileLocator $fileLocator)
46
    {
47
        $this->bundles     = $bundles;
48
        $this->fileLocator = $fileLocator;
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    public function getClassName()
55
    {
56
        // Scan Bundle directories for entity directory
57
        if (empty($this->className)) {
58
            foreach ($this->bundles as $key => $namespace) {
59
60
                $path = $this->fileLocator->locate('@' . $key);
61
                $path .= $this->entityDirectory;
62
63
                if (file_exists($path)) {
64
                    // Remove the final part of the namespace
65
                    $namespaceParts = explode('\\', $namespace);
66
                    array_pop($namespaceParts);
67
                    $namespaceParts[] = $this->entityDirectory;
68
                    $namespace        = implode('\\', $namespaceParts);
69
70
                    // switch any /'s into \'s for namespaces
71
                    $namespace = str_replace('/', '\\', $namespace);
72
73
                    $allFiles = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
74
                    $phpFiles = new \RegexIterator($allFiles, '/\.php$/');
75
                    foreach ($phpFiles as $phpFile) {
76
                        $fileName  = $phpFile->getFileName();
77
                        $className = substr($fileName, 0, -4);
78
                        $FQCN      = $namespace . '\\' . $className;
79
80
                        // Try to load the file as a class and determine if it implements the interface
81
                        try {
82
                            $reflect = new \ReflectionClass($FQCN);
83
                            if ($reflect->implementsInterface(self::RESPONSIVE_IMAGE_INTERFACE)) {
84
                                $this->className = $FQCN;
85
                                break;
86
                            }
87
                        } catch (\ReflectionException $e) {
88
                            // No action required
89
                        }
90
                    }
91
                }
92
            }
93
        }
94
95
        return $this->className;
96
    }
97
98
    /**
99
     * @param string $entityDirectory
100
     */
101
    public function setEntityDirectory($entityDirectory)
102
    {
103
        $this->entityDirectory = $entityDirectory;
104
    }
105
}
106