Completed
Pull Request — master (#14)
by Pavel
29:07 queued 23:49
created

SearchArgumentsTransformer::transformCriteria()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 6.8372
c 0
b 0
f 0
cc 10
eloc 33
nc 8
nop 1

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
    public function __construct(ApiMetadata $metadata, ApiEntityManager $manager)
26
    {
27
        $this->metadata = $metadata;
28
        $this->manager  = $manager;
29
    }
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
    public function transformCriteria(array $criteria)
39
    {
40
        $apiCriteria = [];
41
        foreach ($criteria as $field => $values) {
42
            if ($this->metadata->hasAssociation($field)) {
43
                $mapping = $this->metadata->getAssociationMapping($field);
44
                /** @var EntityMetadata $target */
45
                $target = $this->manager->getClassMetadata($mapping['target']);
46
47
                $converter = function ($value) use ($target) {
48
                    if (!is_object($value)) {
49
                        return $value;
50
                    }
51
52
                    $ids = $target->getIdentifierValues($value);
53
                    if ($target->isIdentifierComposite) {
54
                        return $ids;
55
                    }
56
57
                    return array_shift($ids);
58
                };
59
60
                if ($values instanceof Collection) {
61
                    if ($values instanceof ApiCollection && !$values->isInitialized()) {
62
                        continue;
63
                    }
64
                    $values = $values->toArray();
65
                }
66
                
67
                if (is_array($values)) {
68
                    $values = array_map($converter, $values);
69
                } else {
70
                    $values = $converter($values);
71
                }
72
            } else {
73
                $caster = function ($value) use ($field) {
74
                    $type = $this->manager
75
                        ->getConfiguration()
76
                        ->getTypeRegistry()->get($this->metadata->getTypeOfField($field));
77
78
                    return $type->toApiValue($value);
79
                };
80
81
                if (is_array($values)) {
82
                    $values = array_map($caster, $values);
83
                } else {
84
                    $values = $caster($values);
85
                }
86
            }
87
88
            $apiCriteria[$this->metadata->getApiFieldName($field)] = $values;
89
        }
90
91
        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
    public function transformOrder(array $orderBy = null)
102
    {
103
        $apiOrder = [];
104
        foreach ((array)$orderBy as $field => $direction) {
105
            $apiOrder[$this->metadata->getApiFieldName($field)] = $direction;
106
        }
107
108
        return $apiOrder;
109
    }
110
}
111