Passed
Pull Request — master (#162)
by Alex
04:44
created

ModelSerialiser::bulkSerialise()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
rs 4.8196
cc 10
eloc 23
nc 24
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Serialisers;
4
5
use AlgoWeb\PODataLaravel\Query\LaravelReadQuery;
6
use Illuminate\Database\Eloquent\Model;
7
8
class ModelSerialiser
9
{
10
    // take a supplied Eloquent model with metadata trait and serialise it in bulk.
11
    // Upstream POData implementation has an N+1 lookup problem that interacts badly with how
12
    // Eloquent handles property accesses
13
14
    private static $mutatorCache = [];
15
    private static $metadataCache = [];
16
17
    public function __construct()
18
    {
19
    }
20
21
    /**
22
     * Serialise needed bits of supplied model, taking fast path where possible.
23
     *
24
     * @param  $model
25
     *
26
     * @return mixed
27
     */
28
    public function bulkSerialise($model)
29
    {
30
        $class = get_class($model);
31
        assert($model instanceof Model, $class);
32
        // dig up metadata
33
        if (!isset(self::$metadataCache[$class])) {
34
            self::$metadataCache[$class] = $model->metadata();
35
        }
36
        $meta = self::$metadataCache[$class];
37
        $keys = array_keys($meta);
38
        // dig up getter list - we only care about the mutators that end up in metadata
39
        if (!isset(self::$mutatorCache[$class])) {
40
            $getterz = [];
41
            $datez = $model->getDates();
42
            $castz = $model->retrieveCasts();
43
            foreach ($keys as $key) {
44
                if ($model->hasGetMutator($key) || in_array($key, $datez) || array_key_exists($key, $castz)) {
45
                    $getterz[] = $key;
46
                }
47
            }
48
            self::$mutatorCache[$class] = $getterz;
49
        }
50
        $getterz = self::$mutatorCache[$class];
51
        $modelAttrib = $model->getAttributes();
52
        $result = array_intersect_key($modelAttrib, $meta);
53
        foreach ($keys as $key) {
54
            if (!isset($result[$key])) {
55
                $result[$key] = null;
56
            }
57
        }
58
        foreach ($getterz as $getter) {
59
            $result[$getter] = $model->$getter;
60
        }
61
62
        return $result;
63
    }
64
65
    public function reset()
66
    {
67
        self::$mutatorCache = [];
68
        self::$metadataCache = [];
69
    }
70
}
71