Completed
Push — master ( c4d609...60216c )
by Iqbal
03:34
created

AbstractReadModel::fixedDeserializeMapping()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 5
nc 3
nop 2
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
            $record = static::mapBaseOnKeys($attributes, array_flip(static::cleanMappingKeys($attributes)));
53
            $attributes = static::fixedDeserializeMapping($record, $attributes);
54
        }
55
56
        return static::deserializeData($attributes);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function serialize()
63
    {
64
        $serialized = $this->serializeData();
65
66
        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...
67
            $serialized = static::mapBaseOnKeys($serialized, static::cleanMappingKeys($serialized));
68
        }
69
70
        return $serialized;
71
    }
72
73
    /**
74
     * Mapping data base on mapping keys.
75
     *
76
     * @param array $attributes
77
     * @param array $mappingKeys
78
     *
79
     * @return array
80
     */
81
    protected static function mapBaseOnKeys(array $attributes, array $mappingKeys)
82
    {
83
        return (array) ParameterMapper::map(new ParameterBag($attributes), $mappingKeys);
84
    }
85
86
    /**
87
     * @param array $serialized
88
     *
89
     * @return mixed
90
     */
91
    protected static function cleanMappingKeys(array $serialized)
92
    {
93
        return static::mergeMappingKeys(array_keys($serialized), static::mappings());
94
    }
95
96
    /**
97
     * @param array $attributesKeys
98
     * @param array $mappingKeys
99
     *
100
     * @return mixed
101
     */
102
    protected static function mergeMappingKeys(array $attributesKeys, array $mappingKeys)
103
    {
104
        $diffs = array_diff($attributesKeys, static::extraKeys($mappingKeys));
105
106
        return array_merge(array_combine($diffs, $diffs), $mappingKeys);
107
    }
108
109
    /**
110
     * @param array $attributesKeys
111
     *
112
     * @return array
113
     */
114
    protected static function extraKeys(array $attributesKeys)
115
    {
116
        $extracted = array();
117
        foreach (array_keys($attributesKeys) as $key) {
118
            $parts = explode('.', $key);
119
            if (!in_array($parts[0], $extracted)) {
120
                $extracted[] = $parts[0];
121
            }
122
        }
123
124
        return $extracted;
125
    }
126
127
    /**
128
     * @param array $record
129
     * @param array $attributes
130
     *
131
     * @return array
132
     */
133
    protected static function fixedDeserializeMapping(array $record, array $attributes)
134
    {
135
        foreach ($record as $index => &$value) {
136
            if (isset($attributes[$index]) && is_array($attributes[$index])) {
137
                $value = $attributes[$index];
138
            }
139
        }
140
141
        return $record;
142
    }
143
}
144