Completed
Pull Request — master (#14)
by Pavel
04:14
created

SearchArgumentsTransformer::transformCriteria()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 11.5625

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 27
cts 36
cp 0.75
rs 6.8372
c 0
b 0
f 0
cc 10
eloc 33
nc 8
nop 1
crap 11.5625

How to fix   Long Method    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 Bankiru\Api\Doctrine\Rpc;
4
5
use Bankiru\Api\Doctrine\ApiEntityManager;
6
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
7
use Bankiru\Api\Doctrine\Mapping\EntityMetadata;
8
use Bankiru\Api\Doctrine\Proxy\ApiCollection;
9
use Doctrine\Common\Collections\Collection;
10
11
/** @internal */
12
final class SearchArgumentsTransformer
13
{
14
    /** @var  ApiMetadata */
15
    private $metadata;
16
    /** @var  ApiEntityManager */
17
    private $manager;
18
19
    /**
20
     * SearchArgumentsTransformer constructor.
21
     *
22
     * @param ApiMetadata      $metadata
23
     * @param ApiEntityManager $manager
24
     */
25 19
    public function __construct(ApiMetadata $metadata, ApiEntityManager $manager)
26
    {
27 19
        $this->metadata = $metadata;
28 19
        $this->manager  = $manager;
29 19
    }
30
31
    /**
32
     * Converts doctrine entity criteria to API-ready criteria (converts types and field names)
33
     *
34
     * @param array $criteria
35
     *
36
     * @return array API-ready criteria
37
     */
38 16
    public function transformCriteria(array $criteria)
39
    {
40 16
        $apiCriteria = [];
41 16
        foreach ($criteria as $field => $values) {
42 16
            if ($this->metadata->hasAssociation($field)) {
43 4
                $mapping = $this->metadata->getAssociationMapping($field);
44
                /** @var EntityMetadata $target */
45 4
                $target = $this->manager->getClassMetadata($mapping['target']);
46
47
                $converter = function ($value) use ($target) {
48 4
                    if (!is_object($value)) {
49 1
                        return $value;
50
                    }
51
52 4
                    $ids = $target->getIdentifierValues($value);
53 4
                    if ($target->isIdentifierComposite) {
54
                        return $ids;
55
                    }
56
57 4
                    return array_shift($ids);
58 4
                };
59
60 4
                if ($values instanceof Collection) {
61
                    if ($values instanceof ApiCollection && !$values->isInitialized()) {
62
                        continue;
63
                    }
64
                    $values = $values->toArray();
65
                }
66
                
67 4
                if (is_array($values)) {
68
                    $values = array_map($converter, $values);
69
                } else {
70 4
                    $values = $converter($values);
71
                }
72 4
            } else {
73 16
                $caster = function ($value) use ($field) {
74 16
                    $type = $this->manager
75 16
                        ->getConfiguration()
76 16
                        ->getTypeRegistry()->get($this->metadata->getTypeOfField($field));
77
78 16
                    return $type->toApiValue($value);
79 16
                };
80
81 16
                if (is_array($values)) {
82
                    $values = array_map($caster, $values);
83
                } else {
84 16
                    $values = $caster($values);
85
                }
86
            }
87
88 16
            $apiCriteria[$this->metadata->getApiFieldName($field)] = $values;
89 16
        }
90
91 16
        return $apiCriteria;
92
    }
93
94
    /**
95
     * Converts doctrine entity order to API-ready order (converts field names)
96
     *
97
     * @param array $orderBy
98
     *
99
     * @return array API-ready order
100
     */
101 5
    public function transformOrder(array $orderBy = null)
102
    {
103 5
        $apiOrder = [];
104 5
        foreach ((array)$orderBy as $field => $direction) {
105
            $apiOrder[$this->metadata->getApiFieldName($field)] = $direction;
106 5
        }
107
108 5
        return $apiOrder;
109
    }
110
}
111