Completed
Push — master ( acae87...3c5e0a )
by Iqbal
14:22
created

AbstractReadModel::relations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs\ReadModel;
12
13
use Borobudur\Bus\Message\MessageMapperTrait;
14
use Borobudur\Parameterize\Mapper\ParameterMapper;
15
use Borobudur\Parameterize\ParameterBag;
16
use Borobudur\Serialization\Serializer\Mixin\DeserializerTrait;
17
use Borobudur\Serialization\Serializer\Mixin\SerializerTrait;
18
19
/**
20
 * @author      Iqbal Maulana <[email protected]>
21
 * @created     8/18/15
22
 */
23
abstract class AbstractReadModel implements ReadModelInterface
24
{
25
    use SerializerTrait, DeserializerTrait, MessageMapperTrait {
26
        SerializerTrait::serialize as serializeData;
27
        DeserializerTrait::deserialize as deserializeData;
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public static function relations()
34
    {
35
        return array();
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public static function mappings()
42
    {
43
        return array();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public static function deserialize(array $attributes)
50
    {
51
        if ($mappings = static::mappings()) {
0 ignored issues
show
Unused Code introduced by
$mappings is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
            $attributes = static::mapBaseOnKeys($attributes, array_flip(static::cleanMappingKeys($attributes)));
53
        }
54
55
        return static::deserializeData($attributes);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function serialize()
62
    {
63
        $serialized = $this->serializeData();
64
65
        if ($mappings = static::mappings()) {
0 ignored issues
show
Unused Code introduced by
$mappings is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
            $serialized = static::mapBaseOnKeys($serialized, static::cleanMappingKeys($serialized));
67
        }
68
69
        return $serialized;
70
    }
71
72
    /**
73
     * Mapping data base on mapping keys.
74
     *
75
     * @param array $attributes
76
     * @param array $mappingKeys
77
     *
78
     * @return array
79
     */
80
    protected static function mapBaseOnKeys(array $attributes, array $mappingKeys)
81
    {
82
        return (array) ParameterMapper::map(new ParameterBag($attributes), $mappingKeys);
83
    }
84
85
    /**
86
     * @param array $serialized
87
     *
88
     * @return mixed
89
     */
90
    protected static function cleanMappingKeys(array $serialized)
91
    {
92
        return static::mergeMappingKeys(array_keys($serialized), static::mappings());
93
    }
94
95
    /**
96
     * @param array $attributesKeys
97
     * @param array $mappingKeys
98
     *
99
     * @return mixed
100
     */
101
    protected static function mergeMappingKeys(array $attributesKeys, array $mappingKeys)
102
    {
103
        $diffs = array_diff($attributesKeys, static::extraKeys($mappingKeys));
104
105
        return array_merge(array_combine($diffs, $diffs), $mappingKeys);
106
    }
107
108
    /**
109
     * @param array $attributesKeys
110
     *
111
     * @return array
112
     */
113
    protected static function extraKeys(array $attributesKeys)
114
    {
115
        $extracted = array();
116
        foreach (array_keys($attributesKeys) as $key) {
117
            $parts = explode('.', $key);
118
            if (!in_array($parts[0], $extracted)) {
119
                $extracted[] = $parts[0];
120
            }
121
        }
122
123
        return $extracted;
124
    }
125
}
126