Bundle::setValues()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelLangBundler\Bundle;
4
5
use Illuminate\Support\Collection;
6
use LaravelLangBundler\BundleItems\BundleItem;
7
use LaravelLangBundler\BundleItems\ItemFactory;
8
9
class Bundle
10
{
11
    /**
12
     * Bundle id provided by client.
13
     *
14
     * @var string
15
     */
16
    protected $id;
17
18
    /**
19
     * BundleMap instance.
20
     *
21
     * @var BundleMap
22
     */
23
    protected $bundleMap;
24
25
    /**
26
     * Array of path keys.
27
     *
28
     * @var array
29
     */
30
    protected $pathKeys = [];
31
32
    /**
33
     * Path resolved from id.
34
     *
35
     * @var string
36
     */
37
    protected $path = '';
38
39
    /**
40
     * Path namespace.
41
     *
42
     * @var string
43
     */
44
    protected $namespace = '';
45
46
    /**
47
     * Collection of lang values.
48
     *
49
     * @var Collection
50
     */
51
    protected $values;
52
53
    /**
54
     * Construct.
55
     *
56
     * @param string $id
57
     */
58
    public function __construct($id, BundleMap $bundleMap)
59
    {
60
        $this->id = $id;
61
        $this->path = $id;
62
63
        $this->bundleMap = $bundleMap;
64
65
        $this->getValuesFromMap();
66
    }
67
68
    /**
69
     * Get bundle namespace.
70
     *
71
     * @return string
72
     */
73
    public function getNamespace()
74
    {
75
        return $this->namespace;
76
    }
77
78
    /**
79
     * Get pathKeys array.
80
     *
81
     * @return array
82
     */
83
    public function getPathKeys()
84
    {
85
        return $this->pathKeys;
86
    }
87
88
    /**
89
     * Get path values collection.
90
     *
91
     * @return Collection
92
     */
93
    public function getValues()
94
    {
95
        return $this->values;
96
    }
97
98
    /**
99
     * Return array of values.
100
     *
101
     * @return array
102
     */
103
    public function getValuesArray()
104
    {
105
        return $this->values->all();
106
    }
107
108
    /**
109
     * Return true if bundle contains no trans values.
110
     *
111
     * @return bool
112
     */
113
    public function hasNoValues()
114
    {
115
        return $this->values->isEmpty();
116
    }
117
118
    /**
119
     * Return true if namespace is valid bundle namespace.
120
     *
121
     * @return bool
122
     */
123
    public function hasValidNamespace()
124
    {
125
        return $this->getNamespace() === 'bundles';
126
    }
127
128
    /**
129
     * Set the namespace on the object.
130
     *
131
     * @param string $namespace
132
     */
133
    protected function setNamespace($namespace)
134
    {
135
        $this->namespace = $namespace;
136
    }
137
138
    /**
139
     * Set pathKeys on object.
140
     *
141
     * @param array $pathKeys
142
     */
143
    protected function setPathKeys(array $pathKeys)
144
    {
145
        $this->pathKeys = $pathKeys;
146
    }
147
148
    /**
149
     * Set values collection on object.
150
     *
151
     * @param Collection $values
152
     *
153
     * @return Collection
154
     */
155
    protected function setValues(Collection $values)
156
    {
157
        return $this->values = $values->map(function ($value) {
158
            if (!$value instanceof BundleItem) {
159
                return ItemFactory::build($value);
160
            }
161
162
            return $value;
163
        });
164
    }
165
166
    /**
167
     * Get bundle values from bundle map.
168
     *
169
     * @return Collection
170
     */
171
    protected function getValuesFromMap()
172
    {
173
        if ($this->bundleMap->bundleMapIsEmpty()) {
174
            $this->bundleMap->mapBundles();
175
        }
176
177
        $this->buildKeys();
178
179
        if (!$this->hasValidNamespace()) {
180
            $this->setValues(collect([]));
181
        } else {
182
            $values = $this->bundleMap->getBundleValues($this->getPathKeys());
183
184
            $this->setValues($values);
185
        }
186
    }
187
188
    /**
189
     * Build pathKeys array and set namespace.
190
     */
191
    protected function buildKeys()
192
    {
193
        $pathKeys = $this->getKeysFromId();
194
195
        $this->setNamespace(array_shift($pathKeys));
196
197
        $this->setPathKeys($pathKeys);
198
    }
199
200
    /**
201
     * Get keys from id.
202
     *
203
     * @return array
204
     */
205
    protected function getKeysFromId()
206
    {
207
        $aliases = config('lang-bundler.aliases');
208
209
        $autoAliases = collect($this->bundleMap->getAutoAliases())
210
            ->filter(function ($value) {
211
                return $value === $this->id;
212
            });
213
214
        if (in_array($this->id, array_keys($aliases))) {
215
            $this->path = $aliases[$this->id];
216
        } elseif ($autoAliases->count() === 1) {
217
            $this->path = 'bundles.'.$autoAliases->keys()[0];
218
        }
219
220
        return explode('.', $this->path);
221
    }
222
}
223