Completed
Push — master ( 41c601...a92ef9 )
by
unknown
03:11
created

BundleMetadata::getApplication()   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 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\EasyExtendsBundle\Bundle;
15
16
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
17
18
class BundleMetadata
19
{
20
    /**
21
     * @var BundleInterface
22
     */
23
    protected $bundle;
24
25
    /**
26
     * @var string
27
     */
28
    protected $vendor;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $valid = false;
34
35
    /**
36
     * @var string
37
     */
38
    protected $namespace;
39
40
    /**
41
     * @var string
42
     */
43
    protected $name;
44
45
    /**
46
     * @var string
47
     */
48
    protected $extendedDirectory;
49
50
    /**
51
     * @var string
52
     */
53
    protected $extendedNamespace;
54
55
    /**
56
     * @var array
57
     */
58
    protected $configuration = [];
59
60
    /**
61
     * @var OrmMetadata
62
     */
63
    protected $ormMetadata = null;
64
65
    /**
66
     * @var OdmMetadata
67
     */
68
    protected $odmMetadata = null;
69
70
    /**
71
     * @var PhpcrMetadata
72
     */
73
    protected $phpcrMetadata = null;
74
75
    /**
76
     * @var string
77
     */
78
    private $application;
79
80
    /**
81
     * @param BundleInterface $bundle
82
     * @param array           $configuration
83
     */
84
    public function __construct(BundleInterface $bundle, array $configuration = [])
85
    {
86
        $this->bundle = $bundle;
87
        $this->configuration = $configuration;
88
89
        $this->buildInformation();
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isExtendable(): bool
96
    {
97
        // does not extends Application bundle ...
98
        return !(
99
            0 === strpos($this->getClass(), $this->configuration['namespace'])
100
            || 0 === strpos($this->getClass(), 'Symfony')
101
        );
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getClass(): string
108
    {
109
        return get_class($this->bundle);
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function isValid(): bool
116
    {
117
        return $this->valid;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getExtendedDirectory(): string
124
    {
125
        return $this->extendedDirectory;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getVendor(): string
132
    {
133
        return $this->vendor;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getExtendedNamespace(): string
140
    {
141
        return $this->extendedNamespace;
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getNamespace(): string
148
    {
149
        return $this->namespace;
150
    }
151
152
    /**
153
     * return the bundle name.
154
     *
155
     * @return string return the bundle name
156
     */
157
    public function getName(): string
158
    {
159
        return $this->name;
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getApplication()
166
    {
167
        return $this->application;
168
    }
169
170
    /**
171
     * @return BundleInterface
172
     */
173
    public function getBundle(): BundleInterface
174
    {
175
        return $this->bundle;
176
    }
177
178
    /**
179
     * @return OdmMetadata
180
     */
181
    public function getOdmMetadata(): OdmMetadata
182
    {
183
        return $this->odmMetadata;
184
    }
185
186
    /**
187
     * @return OrmMetadata
188
     */
189
    public function getOrmMetadata(): OrmMetadata
190
    {
191
        return $this->ormMetadata;
192
    }
193
194
    /**
195
     * @return PhpcrMetadata
196
     */
197
    public function getPhpcrMetadata(): PhpcrMetadata
198
    {
199
        return $this->phpcrMetadata;
200
    }
201
202
    /**
203
     * build basic information and check if the bundle respect the following convention
204
     *   Vendor/BundleNameBundle/VendorBundleNameBundle.
205
     *
206
     * if the bundle does not respect this convention then the easy extends command will ignore
207
     * this bundle
208
     */
209
    protected function buildInformation(): void
210
    {
211
        $information = explode('\\', $this->getClass());
212
213
        if (!$this->isExtendable()) {
214
            $this->valid = false;
215
216
            return;
217
        }
218
219
        if (3 != count($information)) {
220
            $this->valid = false;
221
222
            return;
223
        }
224
225
        if ($information[0].$information[1] != $information[2]) {
226
            $this->valid = false;
227
228
            return;
229
        }
230
231
        $this->name = $information[count($information) - 1];
232
        $this->vendor = $information[0];
233
        $this->namespace = sprintf('%s\\%s', $this->vendor, $information[1]);
234
        $this->extendedDirectory =
235
            str_replace(':vendor', $this->vendor, $this->configuration['application_dir']).
236
            DIRECTORY_SEPARATOR.
237
            $information[1];
238
        $this->extendedNamespace = sprintf(
239
            '%s%s\\%s',
240
            $this->configuration['namespace_prefix'],
241
            str_replace(':vendor', $this->vendor, $this->configuration['namespace']),
242
            $information[1]
243
        );
244
        $this->application = explode('\\', $this->configuration['namespace'])[0];
245
        $this->valid = true;
246
247
        $this->ormMetadata = new OrmMetadata($this);
248
        $this->odmMetadata = new OdmMetadata($this);
249
        $this->phpcrMetadata = new PhpcrMetadata($this);
250
    }
251
}
252