Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

APIBusinessEntityResolver::getBusinessEntity()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 3
eloc 17
nc 3
nop 1
1
<?php
2
3
namespace Victoire\Bundle\APIBusinessEntityBundle\Resolver;
4
5
use Victoire\Bundle\APIBusinessEntityBundle\Chain\ApiAuthenticationChain;
6
use Victoire\Bundle\APIBusinessEntityBundle\Entity\APIBusinessEntity;
7
use Victoire\Bundle\APIBusinessEntityBundle\Entity\APIEndpoint;
8
use Victoire\Bundle\BusinessEntityBundle\Converter\ParameterConverter;
9
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessProperty;
10
use Victoire\Bundle\BusinessEntityBundle\Resolver\BusinessEntityResolverInterface;
11
use Victoire\Bundle\CoreBundle\Entity\EntityProxy;
12
13
/**
14
 * Class APIBusinessEntityResolver.
15
 */
16
class APIBusinessEntityResolver implements BusinessEntityResolverInterface
17
{
18
    /**
19
     * @var ParameterConverter
20
     */
21
    private $parameterConverter;
22
    /**
23
     * @var ApiAuthenticationChain
24
     */
25
    private $authenticationChain;
26
27
    /**
28
     * APIBusinessEntityResolver constructor.
29
     *
30
     * @param ParameterConverter     $parameterConverter
31
     * @param ApiAuthenticationChain $authenticationChain
32
     */
33
    public function __construct(ParameterConverter $parameterConverter, APIAuthenticationChain $authenticationChain)
34
    {
35
        $this->parameterConverter = $parameterConverter;
36
        $this->authenticationChain = $authenticationChain;
37
    }
38
39
    /**
40
     * Fetch API to get a single entity.
41
     *
42
     * @param EntityProxy $entityProxy
43
     *
44
     * @return mixed
45
     */
46
    public function getBusinessEntity(EntityProxy $entityProxy)
47
    {
48
        /** @var APIBusinessEntity $businessEntity */
49
        $businessEntity = $entityProxy->getBusinessEntity();
50
        $getMethod = $businessEntity->getGetMethod();
51
        preg_match_all('/{{([a-zA-Z]+)}}/', $getMethod, $matches);
52
        // build an array with businessIdentifiers properties names
53
        $identifiers = array_map(function ($property) {
54
            return $property->getName();
55
        },
56
            $businessEntity->getBusinessIdentifiers()->toArray()
57
        );
58
        foreach ($matches[1] as $match) {
59
            if (in_array($match, $identifiers)) {
60
                $value = $entityProxy->getRessourceId();
61
            } else {
62
                $props = $entityProxy->getAdditionnalProperties();
63
                $value = $props[$match];
64
            }
65
            $getMethod = $this->parameterConverter->convert($getMethod, $match, $value);
66
        }
67
68
        $entity = $this->callApi($businessEntity->getEndpoint(), $getMethod);
69
        // Then the BusinessEntity is an API result, it's not a proper object but a decoded json result.
70
        // It has no classname so we cannot resolve the related BusinessEntity definition, so we
71
        // store the businessEntity object into the entity.
72
        $entity->_businessEntity = $entityProxy->getBusinessEntity();
73
74
        return $entity;
75
    }
76
77
    /**
78
     * Fetch API to get a list of entities.
79
     *
80
     * @param APIBusinessEntity $businessEntity
81
     *
82
     * @return array
83
     */
84
    public function getBusinessEntities(APIBusinessEntity $businessEntity, $page = 1)
85
    {
86
        $data = $this->callApi($businessEntity->getEndpoint(), $businessEntity->getListMethod($page));
87
88
        foreach ($data as $entity) {
89
            $entity->_businessEntity = $businessEntity;
90
        }
91
        if (count($data) > 0) {
92
            $data = array_merge($data, $this->getBusinessEntities($businessEntity, ++$page));
93
        }
94
95
        return $data;
96
    }
97
98
    /**
99
     * filter API to get a list of entities.
100
     *
101
     * @param APIBusinessEntity $businessEntity
102
     * @param array             $filters
0 ignored issues
show
Documentation introduced by
There is no parameter named $filters. Did you maybe mean $filter?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
103
     *
104
     * @return mixed
105
     */
106
    public function searchBusinessEntities(APIBusinessEntity $businessEntity, BusinessProperty $businessProperty, $filter)
107
    {
108
        $getMethod = $businessEntity->getListMethod()
109
            .(false !== strpos($businessEntity->getListMethod(), '?') ? '&' : '?')
110
            .$businessProperty->getFilterMethod();
111
        $getMethod = preg_replace('/{{([a-zA-Z]+)}}/', $filter, $getMethod);
112
113
        $data = $this->callApi($businessEntity->getEndpoint(), $getMethod);
114
115
        foreach ($data as $entity) {
116
            $entity->businessEntity = $businessEntity;
117
        }
118
119
        return $data;
120
    }
121
122
    /**
123
     * Sends a curl request to a given path.
124
     *
125
     * @param APIEndpoint $endPoint
126
     *
127
     * @return array
128
     */
129
    protected function callApi(APIEndpoint $endPoint, $getMethod)
130
    {
131
        $host = $endPoint->getHost();
132
        $token = $endPoint->getToken();
133
        $curl = curl_init();
134
        if ($tokenType = $endPoint->getTokenType()) {
135
            $this->authenticationChain->resolve($tokenType)->handle($curl, $getMethod, $token);
136
        }
137
        curl_setopt($curl, CURLOPT_URL, $host.$getMethod);
138
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
139
140
        $result = curl_exec($curl);
141
142
        curl_close($curl);
143
144
        return json_decode($result);
145
    }
146
}
147