BundleMetadata::buildInformation()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 9.248
c 0
b 0
f 0
cc 4
nc 4
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;
64
65
    /**
66
     * @var OdmMetadata
67
     */
68
    protected $odmMetadata;
69
70
    /**
71
     * @var PhpcrMetadata
72
     */
73
    protected $phpcrMetadata;
74
75
    /**
76
     * @var string
77
     */
78
    private $application;
79
80
    public function __construct(BundleInterface $bundle, array $configuration = [])
81
    {
82
        $this->bundle = $bundle;
83
        $this->configuration = $configuration;
84
85
        $this->buildInformation();
86
    }
87
88
    public function isExtendable(): bool
89
    {
90
        // does not extends Application bundle ...
91
        return !(
92
            0 === strpos($this->getClass(), $this->configuration['namespace'])
93
            || 0 === strpos($this->getClass(), 'Symfony')
94
        );
95
    }
96
97
    public function getClass(): string
98
    {
99
        return \get_class($this->bundle);
100
    }
101
102
    public function isValid(): bool
103
    {
104
        return $this->valid;
105
    }
106
107
    public function getExtendedDirectory(): string
108
    {
109
        return $this->extendedDirectory;
110
    }
111
112
    public function getVendor(): string
113
    {
114
        return $this->vendor;
115
    }
116
117
    public function getExtendedNamespace(): string
118
    {
119
        return $this->extendedNamespace;
120
    }
121
122
    public function getNamespace(): string
123
    {
124
        return $this->namespace;
125
    }
126
127
    /**
128
     * return the bundle name.
129
     *
130
     * @return string return the bundle name
131
     */
132
    public function getName(): string
133
    {
134
        return $this->name;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getApplication()
141
    {
142
        return $this->application;
143
    }
144
145
    public function getBundle(): BundleInterface
146
    {
147
        return $this->bundle;
148
    }
149
150
    public function getOdmMetadata(): OdmMetadata
151
    {
152
        return $this->odmMetadata;
153
    }
154
155
    public function getOrmMetadata(): OrmMetadata
156
    {
157
        return $this->ormMetadata;
158
    }
159
160
    public function getPhpcrMetadata(): PhpcrMetadata
161
    {
162
        return $this->phpcrMetadata;
163
    }
164
165
    /**
166
     * build basic information and check if the bundle respect the following convention
167
     *   Vendor/BundleNameBundle/VendorBundleNameBundle.
168
     *
169
     * if the bundle does not respect this convention then the easy extends command will ignore
170
     * this bundle
171
     */
172
    protected function buildInformation(): void
173
    {
174
        $information = explode('\\', $this->getClass());
175
176
        if (!$this->isExtendable()) {
177
            $this->valid = false;
178
179
            return;
180
        }
181
182
        if (3 !== \count($information)) {
183
            $this->valid = false;
184
185
            return;
186
        }
187
188
        if ($information[0].$information[1] !== $information[2]) {
189
            $this->valid = false;
190
191
            return;
192
        }
193
194
        $this->name = $information[\count($information) - 1];
195
        $this->vendor = $information[0];
196
        $this->namespace = sprintf('%s\\%s', $this->vendor, $information[1]);
197
        $this->extendedDirectory =
198
            str_replace(':vendor', $this->vendor, $this->configuration['application_dir']).
199
            \DIRECTORY_SEPARATOR.
200
            $information[1];
201
        $this->extendedNamespace = sprintf(
202
            '%s%s\\%s',
203
            $this->configuration['namespace_prefix'],
204
            str_replace(':vendor', $this->vendor, $this->configuration['namespace']),
205
            $information[1]
206
        );
207
        $this->application = explode('\\', $this->configuration['namespace'])[0];
208
        $this->valid = true;
209
210
        $this->ormMetadata = new OrmMetadata($this);
211
        $this->odmMetadata = new OdmMetadata($this);
212
        $this->phpcrMetadata = new PhpcrMetadata($this);
213
    }
214
}
215