Completed
Push — master ( 18b9a5...070941 )
by Simonas
02:16
created

DocumentFinder::setDocumentDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
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 ONGR\ElasticsearchBundle\Mapping;
13
14
/**
15
 * Finds documents in bundles.
16
 */
17
class DocumentFinder
18
{
19
    /**
20
     * @var array
21
     */
22
    private $bundles;
23
24
    /**
25
     * @var string Directory in bundle to load documents from.
26
     */
27
    private $documentDir;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param array $bundles Parameter kernel.bundles from service container.
33
     */
34
    public function __construct(array $bundles)
35
    {
36
        $this->documentDir = 'Document';
37
        $this->bundles = $bundles;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getDocumentDir()
44
    {
45
        return $this->documentDir;
46
    }
47
48
    /**
49
     * @param string $documentDir
50
     */
51
    public function setDocumentDir($documentDir)
52
    {
53
        $this->documentDir = $documentDir;
54
    }
55
56
    /**
57
     * Formats namespace from short syntax.
58
     *
59
     * @param string $namespace
60
     * @param string $documentsDirectory Directory name where documents are stored in the bundle.
61
     *
62
     * @return string
63
     */
64
    public function getNamespace($namespace, $documentsDirectory = null)
65
    {
66
        if (!$documentsDirectory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $documentsDirectory of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
67
            $documentsDirectory = $this->documentDir;
68
        }
69
70
        if (strpos($namespace, ':') !== false) {
71
            list($bundle, $document) = explode(':', $namespace);
72
            $bundle = $this->getBundleClass($bundle);
73
74
            // If bundle has a sub-namespace it needs to be replaced
75
            if (strpos($documentsDirectory, '\\')) {
76
                $bundleSubNamespace = substr(
77
                    $bundle,
78
                    $start = strpos($bundle, '\\') + 1,
79
                    strrpos($bundle, '\\') - $start + 1
80
                );
81
82
                $documentsDirectory = str_replace(
83
                    $bundleSubNamespace,
84
                    '',
85
                    $documentsDirectory
86
                );
87
            }
88
89
            $namespace = substr($bundle, 0, strrpos($bundle, '\\')) . '\\' .
90
                $documentsDirectory . '\\' . $document;
91
        }
92
93
        return $namespace;
94
    }
95
96
    /**
97
     * Returns bundle class namespace else throws an exception.
98
     *
99
     * @param string $name
100
     *
101
     * @return string
102
     *
103
     * @throws \LogicException
104
     */
105
    public function getBundleClass($name)
106
    {
107
        if (array_key_exists($name, $this->bundles)) {
108
            return $this->bundles[$name];
109
        }
110
111
        throw new \LogicException(sprintf('Bundle \'%s\' does not exist.', $name));
112
    }
113
114
    /**
115
     * Returns a list of bundle document classes.
116
     *
117
     * Example output:
118
     *
119
     *     [
120
     *         'Category',
121
     *         'Product',
122
     *         'SubDir\SomeObject'
123
     *     ]
124
     *
125
     * @param string $bundle Bundle name. E.g. AppBundle
126
     * @param string $documentsDirectory Directory name where documents are stored in the bundle.
127
     *
128
     * @return array
129
     */
130
    public function getBundleDocumentClasses($bundle, $documentsDirectory = null)
131
    {
132
        if (!$documentsDirectory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $documentsDirectory of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
133
            $documentsDirectory = $this->documentDir;
134
        }
135
136
        $bundleReflection = new \ReflectionClass($this->getBundleClass($bundle));
137
138
        $documentsDirectory = DIRECTORY_SEPARATOR . $documentsDirectory . DIRECTORY_SEPARATOR;
139
        $directory = dirname($bundleReflection->getFileName()) . $documentsDirectory;
140
141
        if (!is_dir($directory)) {
142
            return [];
143
        }
144
145
        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
146
        $files = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
147
148
        $documents = [];
149
150
        foreach ($files as $file => $v) {
151
            $documents[] = str_replace(
152
                DIRECTORY_SEPARATOR,
153
                '\\',
154
                substr(strstr($file, $documentsDirectory), strlen($documentsDirectory), -4)
155
            );
156
        }
157
158
        return $documents;
159
    }
160
}
161