Completed
Push — master ( a06a95...a1d542 )
by dan
15s
created

ImageEntityClassLocator::getClassName()   C

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
 * @package IrishDan\ResponsiveImageBundle
21
 */
22
class ImageEntityClassLocator
23
{
24
    const RESPONSIVE_IMAGE_INTERFACE = 'IrishDan\ResponsiveImageBundle\ResponsiveImageInterface';
25
    /**
26
     * @var null|string
27
     */
28
    protected $className = null;
29
    /**
30
     * @var FileLocator
31
     */
32
    protected $fileLocator;
33
    /**
34
     * @var string
35
     */
36
    protected $entityDirectory = 'Entity';
37
38
    /**
39
     * ImageEntityClassLocator constructor.
40
     *
41
     * @param             $bundles
42
     * @param FileLocator $fileLocator
43
     */
44
    public function __construct($bundles = [], FileLocator $fileLocator)
45
    {
46
        $this->bundles     = $bundles;
0 ignored issues
show
Bug introduced by
The property bundles does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
        $this->fileLocator = $fileLocator;
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    public function getClassName()
54
    {
55
        // Scan Bundle directories for entity directory
56
        if (empty($this->className)) {
57
            foreach ($this->bundles as $key => $namespace) {
58
59
                $path = $this->fileLocator->locate('@' . $key);
60
                $path .= $this->entityDirectory;
61
62
                if (file_exists($path)) {
63
                    // Remove the final part of the namespace
64
                    $namespaceParts = explode('\\', $namespace);
65
                    array_pop($namespaceParts);
66
                    $namespaceParts[] = $this->entityDirectory;
67
                    $namespace        = implode('\\', $namespaceParts);
68
69
                    // switch any /'s into \'s for namespaces
70
                    $namespace = str_replace('/', '\\', $namespace);
71
72
                    $allFiles = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
73
                    $phpFiles = new \RegexIterator($allFiles, '/\.php$/');
74
                    foreach ($phpFiles as $phpFile) {
75
                        $fileName  = $phpFile->getFileName();
76
                        $className = substr($fileName, 0, -4);
77
                        $FQCN      = $namespace . '\\' . $className;
78
79
                        // Try to load the file as a class and determine if it implements the interface
80
                        try {
81
                            $reflect = new \ReflectionClass($FQCN);
82
                            if ($reflect->implementsInterface(self::RESPONSIVE_IMAGE_INTERFACE)) {
83
                                $this->className = $FQCN;
84
                                break;
85
                            }
86
                        } catch (\ReflectionException $e) {
87
                            // No action required
88
                        }
89
                    }
90
                }
91
            }
92
        }
93
94
        return $this->className;
95
    }
96
97
    /**
98
     * @param string $entityDirectory
99
     */
100
    public function setEntityDirectory($entityDirectory)
101
    {
102
        $this->entityDirectory = $entityDirectory;
103
    }
104
}
105