Completed
Push — master ( 95446c...178a89 )
by Iqbal
06:10
created

Collection::getFieldValues()   C

Complexity

Conditions 14
Paths 2

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 43
rs 5.0865
cc 14
eloc 26
nc 2
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
 * 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;
12
13
use Borobudur\Collection\Collection as BaseCollection;
14
use Borobudur\Collection\Criteria;
15
use Borobudur\Cqrs\Exception\InvalidArgumentException;
16
use Borobudur\Cqrs\ReadModel\ReadModelInterface;
17
use Borobudur\Cqrs\Serializer\SerializableInterface;
18
19
/**
20
 * @author      Iqbal Maulana <[email protected]>
21
 * @created     8/20/15
22
 */
23
class Collection extends BaseCollection
24
{
25
    /**
26
     * @var string|null
27
     */
28
    private $classMapper;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param array       $elements
34
     * @param string|null $classMapper
35
     */
36
    public function __construct(array $elements = array(), $classMapper = null)
37
    {
38
        $this->assertValidClassMapper($classMapper);
39
        $this->classMapper = $classMapper;
40
41
        parent::__construct($elements);
42
    }
43
44
    /**
45
     * Find collection by id.
46
     *
47
     * @param mixed $id
48
     *
49
     * @return ReadModelInterface
50
     */
51
    public function findById($id)
52
    {
53
        $collection = $this->matching(Criteria::create()->where(
54
            Criteria::expr()->equal('id', $id)
55
        ));
56
57
        return $collection->first();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function set($key, $value)
64
    {
65
        if (null !== $this->classMapper && false === is_object($value)) {
66
            $class = $this->classMapper;
67
            $value = $class::{'deserialize'}(new ParameterBag($value));
68
        }
69
70
        parent::set($key, $value);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getFieldValues($field = null)
77
    {
78
        $fields = func_get_args();
79
        $elements = array();
80
        if ($fields || (isset($this->elements[0]) && is_array($this->elements[0]))) {
81
            $this->each(function ($element) use (&$elements, $fields) {
82
                if (is_object($element)) {
83
                    if ($fields) {
84
                        foreach ($fields as $field) {
85
                            $methodGet = 'get' . ucfirst($field);
86
                            $methodIs = 'is' . ucfirst($field);
87
88
                            if (method_exists($element, $methodGet)) {
89
                                $elements[] = $element->{$methodGet}();
90
                            } elseif (method_exists($element, $methodIs)) {
91
                                $elements[] = $element->{$methodIs}();
92
                            } elseif (property_exists($element, $field)) {
93
                                $elements[] = $element->{$field};
94
                            }
95
                        }
96
                    }
97
98
                    return;
99
                }
100
101
                if (is_array($element)) {
102
                    if ($fields) {
103
                        foreach ($fields as $field) {
104
                            if (array_key_exists($field, $element)) {
105
                                $elements[] = $element[$field];
106
                            }
107
                        }
108
                    } else {
109
                        $elements = array_merge($elements, array_values($element));
110
                    }
111
                }
112
            });
113
114
            return $elements;
115
        }
116
117
        return array();
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function toArray($resortIndex = false)
124
    {
125
        $elements = parent::toArray($resortIndex);
126
127
        if (null !== $this->classMapper) {
128
            return array_map(function (SerializableInterface $element) {
129
                return $element->serialize();
130
            }, (array) $elements);
131
        }
132
133
        return $elements;
134
    }
135
136
    /**
137
     * Assert that class mapper should be exists and can be deserialize.
138
     *
139
     * @param string $classMapper
140
     *
141
     * @throws InvalidArgumentException
142
     */
143
    private function assertValidClassMapper($classMapper)
144
    {
145
        if (null !== $classMapper) {
146
            $classMapper = '\\' . ltrim($classMapper, '\\');
147
            if (!class_exists($classMapper)) {
148
                throw new InvalidArgumentException(sprintf('Class "%s" is undefined.', $classMapper));
149
            }
150
151
            if (!in_array('Borobudur\Cqrs\Serializer\SerializableInterface', class_implements($classMapper))) {
152
                throw new InvalidArgumentException(sprintf(
153
                    'Class "%s" should implement \Borobudur\Cqrs\Serializer\SerializableInterface',
154
                    $classMapper
155
                ));
156
            }
157
        }
158
    }
159
}
160