Completed
Push — master ( c2ab20...6fd91f )
by Alex
14s queued 12s
created

MetadataTrait   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 365
Duplicated Lines 0 %

Test Coverage

Coverage 72.67%

Importance

Changes 13
Bugs 0 Features 0
Metric Value
wmc 44
eloc 136
c 13
b 0
f 0
dl 0
loc 365
ccs 109
cts 150
cp 0.7267
rs 8.8798

12 Methods

Rating   Name   Duplication   Size   Complexity  
A metadataMask() 0 13 3
A getAllAttributes() 0 23 4
A getEndpointName() 0 10 3
C metadata() 0 75 12
A retrieveCasts() 0 3 1
A collectGetters() 0 19 6
A setEagerLoad() 0 3 1
A getTableDoctrineColumns() 0 10 2
B extractGubbins() 0 45 7
A getTableColumns() 0 11 2
A isRunningInArtisan() 0 3 2
A getEagerLoad() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like MetadataTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MetadataTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace AlgoWeb\PODataLaravel\Models;
3
4
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubFactory;
5
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubMonomorphic;
6
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubPolymorphic;
7
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubRelationType;
8
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityField;
9
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType;
10
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldType;
11
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityGubbins;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Database\Eloquent\Relations\BelongsTo;
14
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
15
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
16
use Illuminate\Database\Eloquent\Relations\MorphMany;
17
use Illuminate\Database\Eloquent\Relations\MorphOne;
18
use Illuminate\Database\Eloquent\Relations\MorphToMany;
19
use Illuminate\Database\Eloquent\Relations\Relation;
20
use Illuminate\Support\Facades\App;
21
use Mockery\Mock;
22
use POData\Common\InvalidOperationException;
23
use POData\Providers\Metadata\Type\IType;
24
25
/**
26
 * Trait MetadataTrait
27
 * @package AlgoWeb\PODataLaravel\Models
28 3
 * @mixin Model
29
 */
30 3
trait MetadataTrait
31
{
32
    use MetadataRelationsTrait;
33 2
34 2
    protected $loadEagerRelations = [];
35
    protected static $tableColumns = [];
36 2
    protected static $tableColumnsDoctrine = [];
37
    protected static $tableData = [];
38 2
    protected static $dontCastTypes = ['object', 'array', 'collection', 'int'];
39 1
40
    /**
41
     * Retrieve and assemble this model's metadata for OData packaging.
42 1
     * @throws InvalidOperationException
43 1
     * @throws \Doctrine\DBAL\DBALException
44 1
     * @return array
45
     */
46 1
    public function metadata()
47
    {
48 1
        if (!$this instanceof Model) {
49 1
            throw new InvalidOperationException(get_class($this));
50 1
        }
51
52 1
        if (0 !== count(self::$tableData)) {
53 1
            return self::$tableData;
54 1
        } elseif (isset($this->odata)) {
55
            return self::$tableData = $this->odata;
56 1
        }
57
58 1
        // Break these out separately to enable separate reuse
59 1
        $connect = $this->getConnection();
60 1
        $builder = $connect->getSchemaBuilder();
61 1
62 1
        $table = $this->getTable();
63 1
64 1
        if (!$builder->hasTable($table)) {
65
            return self::$tableData = [];
66 1
        }
67
68
        /** @var array $columns */
69
        $columns = $this->getTableColumns();
70
        /** @var array $mask */
71
        $mask = $this->metadataMask();
72
        $columns = array_intersect($columns, $mask);
73 4
74
        $tableData = [];
75 4
76
        $rawFoo = $this->getTableDoctrineColumns();
77 4
        $foo = [];
78 4
        /** @var array $getters */
79 4
        $getters = $this->collectGetters();
80 2
        $getters = array_intersect($getters, $mask);
81 4
        $casts = $this->retrieveCasts();
82 1
83 1
        foreach ($rawFoo as $key => $val) {
84
            // Work around glitch in Doctrine when reading from MariaDB which added ` characters to root key value
85 4
            $key = trim($key, '`"');
86
            $foo[$key] = $val;
87
        }
88
89
        foreach ($columns as $column) {
90
            // Doctrine schema manager returns columns with lowercased names
91 5
            $rawColumn = $foo[strtolower($column)];
92
            /** @var IType $rawType */
93 5
            $rawType = $rawColumn->getType();
94 5
            $type = $rawType->getName();
95 1
            $default = $this->$column;
96
            $tableData[$column] = ['type' => $type,
97
                'nullable' => !($rawColumn->getNotNull()),
98 4
                'fillable' => in_array($column, $this->getFillable()),
99
                'default' => $default
100 4
            ];
101
        }
102 4
103 4
        foreach ($getters as $get) {
104 4
            if (isset($tableData[$get])) {
105 4
                continue;
106 4
            }
107 4
            $default = $this->$get;
108
            $tableData[$get] = ['type' => 'text', 'nullable' => true, 'fillable' => false, 'default' => $default];
109 4
        }
110 1
111 1
        // now, after everything's gathered up, apply Eloquent model's $cast array
112 1
        foreach ($casts as $key => $type) {
113 1
            $type = strtolower($type);
114
            if (array_key_exists($key, $tableData) && !in_array($type, self::$dontCastTypes)) {
115 4
                $tableData[$key]['type'] = $type;
116 4
            }
117
        }
118 4
119
        self::$tableData = $tableData;
120
        return $tableData;
121
    }
122
123
    /**
124
     * Return the set of fields that are permitted to be in metadata
125
     * - following same visible-trumps-hidden guideline as Laravel.
126
     *
127
     * @return array
128
     */
129
    public function metadataMask()
130
    {
131
        $attribs = array_keys($this->getAllAttributes());
132
133
        $visible = $this->getVisible();
134
        $hidden = $this->getHidden();
135
        if (0 < count($visible)) {
136
            $attribs = array_intersect($visible, $attribs);
137
        } elseif (0 < count($hidden)) {
138
            $attribs = array_diff($attribs, $hidden);
139
        }
140
141
        return $attribs;
142
    }
143
144
    /*
145
     * Get the endpoint name being exposed
146
     *
147
     * @return null|string;
148
     */
149
    public function getEndpointName()
150
    {
151
        $endpoint = isset($this->endpoint) ? $this->endpoint : null;
152
153
        if (!isset($endpoint)) {
154
            $bitter = get_class($this);
155
            $name = substr($bitter, strrpos($bitter, '\\')+1);
156
            return ($name);
157
        }
158
        return ($endpoint);
159
    }
160
161
    protected function getAllAttributes()
162
    {
163
        // Adapted from http://stackoverflow.com/a/33514981
164
        // $columns = $this->getFillable();
165
        // Another option is to get all columns for the table like so:
166
        $columns = $this->getTableColumns();
167
        // but it's safer to just get the fillable fields
168
169
        $attributes = $this->getAttributes();
170
171
        foreach ($columns as $column) {
172
            if (!array_key_exists($column, $attributes)) {
173
                $attributes[$column] = null;
174
            }
175 3
        }
176
177 3
        $methods = $this->collectGetters();
178
179 3
        foreach ($methods as $method) {
180 3
            $attributes[$method] = null;
181 3
        }
182 3
183 3
        return $attributes;
184 3
    }
185 3
186 3
    /**
187 3
     * Get the visible attributes for the model.
188 3
     *
189
     * @return array
190 3
     */
191
    abstract public function getVisible();
192 3
193 3
    /**
194 3
     * Get the hidden attributes for the model.
195 3
     *
196 3
     * @return array
197 3
     */
198 3
    abstract public function getHidden();
199 3
200 3
    /**
201 3
     * Get the primary key for the model.
202
     *
203 3
     * @return string
204 3
     */
205 3
    abstract public function getKeyName();
206 3
207 3
    /**
208 3
     * Get the current connection name for the model.
209 3
     *
210 3
     * @return string
211
     */
212 3
    abstract public function getConnectionName();
213 3
214 3
    /**
215
     * Get the database connection for the model.
216 3
     *
217 3
     * @return \Illuminate\Database\Connection
218 3
     */
219 3
    abstract public function getConnection();
220 3
221
    /**
222 1
     * Get all of the current attributes on the model.
223 3
     *
224
     * @return array
225 1
     */
226
    abstract public function getAttributes();
227 1
228
    /**
229 1
     * Get the table associated with the model.
230
     *
231 3
     * @return string
232 2
     */
233 2
    abstract public function getTable();
234 3
235 3
    /**
236 3
     * Get the fillable attributes for the model.
237 3
     *
238 3
     * @return array
239 3
     */
240 3
    abstract public function getFillable();
241
242
    abstract public function getCasts();
243
244
    /**
245
     * Dig up all defined getters on the model.
246
     *
247
     * @return array
248
     */
249
    protected function collectGetters()
250
    {
251
        $getterz = [];
252
        $methods = get_class_methods($this);
253
        foreach ($methods as $method) {
254
            if (12 < strlen($method) && 'get' == substr($method, 0, 3)) {
255
                if ('Attribute' == substr($method, -9)) {
256
                    $getterz[] = $method;
257
                }
258
            }
259
        }
260
        $methods = [];
261
262
        foreach ($getterz as $getter) {
263
            $residual = substr($getter, 3);
264
            $residual = substr(/* @scrutinizer ignore-type */$residual, 0, -9);
265
            $methods[] = $residual;
266
        }
267
        return $methods;
268
    }
269
270
    /**
271
     * Used to be supplemental function to retrieve cast array for Laravel versions that do not supply hasCasts.
272
     *
273
     * @return array
274
     */
275
    public function retrieveCasts()
276
    {
277
        return $this->getCasts();
278
    }
279
280
    /**
281
     * Return list of relations to be eager-loaded by Laravel query provider.
282
     *
283
     * @return array
284
     */
285
    public function getEagerLoad()
286
    {
287
        return $this->loadEagerRelations;
288
    }
289
290
    /**
291
     * Set list of relations to be eager-loaded.
292
     *
293
     * @param array $relations
294
     */
295
    public function setEagerLoad(array $relations)
296
    {
297
        $this->loadEagerRelations = array_map('strval', $relations);
298
    }
299
300
    /**
301
     * Extract entity gubbins detail for later downstream use.
302
     *
303
     * @throws InvalidOperationException
304
     * @throws \ReflectionException
305
     * @throws \Doctrine\DBAL\DBALException
306
     * @throws \Exception
307
     * @return EntityGubbins
308
     */
309
    public function extractGubbins()
310
    {
311
        $gubbins = new EntityGubbins();
312
        $gubbins->setName($this->getEndpointName());
313
        $gubbins->setClassName(get_class($this));
314
315
        $lowerNames = [];
316
317
        $fields = $this->metadata();
318
        $entityFields = [];
319
        foreach ($fields as $name => $field) {
320
            if (in_array(strtolower($name), $lowerNames)) {
321
                $msg = 'Property names must be unique, without regard to case';
322
                throw new \Exception($msg);
323
            }
324
            $lowerNames[] = strtolower($name);
325
            $nuField = new EntityField();
326
            $nuField->setName($name);
327
            $nuField->setIsNullable($field['nullable']);
328
            $nuField->setReadOnly(false);
329
            $nuField->setCreateOnly(false);
330
            $nuField->setDefaultValue($field['default']);
331
            $nuField->setIsKeyField($this->getKeyName() == $name);
332
            $nuField->setFieldType(EntityFieldType::PRIMITIVE());
333
            $nuField->setPrimitiveType(new EntityFieldPrimitiveType($field['type']));
334
            $entityFields[$name] = $nuField;
335
        }
336
        $isEmpty = (0 === count($entityFields));
337
        if (!($isEmpty && $this->isRunningInArtisan())) {
338
            $gubbins->setFields($entityFields);
339
        }
340
341
        $rawRels = $this->getRelationships();
342
        $stubs = [];
343
        foreach ($rawRels as $propertyName) {
344
            if (in_array(strtolower($propertyName), $lowerNames)) {
345
                $msg = 'Property names must be unique, without regard to case';
346
                throw new \Exception($msg);
347
            }
348
            $stub = AssociationStubFactory::associationStubFromRelation(/** @scrutinizer ignore-type */$this, $propertyName);
349
            $stubs[$propertyName] = $stub;
350
        }
351
        $gubbins->setStubs($stubs);
352
353
        return $gubbins;
354
    }
355
356
    public function isRunningInArtisan()
357
    {
358
        return App::runningInConsole() && !App::runningUnitTests();
359
    }
360
361
    /**
362
     * Get columns for selected table.
363
     *
364
     * @return array
365
     */
366
    protected function getTableColumns()
367
    {
368
        if (0 === count(self::$tableColumns)) {
369
            $table = $this->getTable();
370
            $connect = $this->getConnection();
371
            $builder = $connect->getSchemaBuilder();
372
            $columns = $builder->getColumnListing($table);
373
374
            self::$tableColumns = (array)$columns;
375
        }
376
        return self::$tableColumns;
377
    }
378
379
    /**
380
     * Get Doctrine columns for selected table.
381
     *
382
     * @throws \Doctrine\DBAL\DBALException
383
     * @return array
384
     */
385
    protected function getTableDoctrineColumns()
386
    {
387
        if (0 === count(self::$tableColumnsDoctrine)) {
388
            $table = $this->getTable();
389
            $connect = $this->getConnection();
390
            $columns = $connect->getDoctrineSchemaManager()->listTableColumns($table);
391
392
            self::$tableColumnsDoctrine = $columns;
393
        }
394
        return self::$tableColumnsDoctrine;
395
    }
396
}
397