Completed
Pull Request — master (#812)
by Paul
06:29
created

APIBusinessEntityResolver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 0
loc 126
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getBusinessEntity() 0 26 3
A getBusinessEntities() 0 13 3
A searchBusinessEntities() 0 15 3
A callApi() 0 16 2
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
        $identifiers = array_map(function ($property) {
53
            return $property->getName();
54
        },
55
            $businessEntity->getBusinessIdentifiers()->toArray()
56
        );
57
        foreach ($matches[1] as $match) {
58
            if (in_array($match, $identifiers)) {
59
                $value = $entityProxy->getRessourceId();
60
            } else {
61
                $props = $entityProxy->getAdditionnalProperties();
62
                $value = $props[$match];
63
            }
64
            $getMethod = $this->parameterConverter->convert($getMethod, $match, $value);
65
        }
66
67
        $entity = $this->callApi($businessEntity->getEndpoint()->getHost(), $getMethod, $businessEntity->getEndpoint());
68
        $entity->_businessEntity = $entityProxy->getBusinessEntity();
69
70
        return $entity;
71
    }
72
73
    /**
74
     * Fetch API to get a list of entities.
75
     *
76
     * @param APIBusinessEntity $businessEntity
77
     *
78
     * @return array
79
     */
80
    public function getBusinessEntities(APIBusinessEntity $businessEntity, $page = 1)
81
    {
82
        $data = $this->callApi($businessEntity->getEndpoint()->getHost(), $businessEntity->getListMethod($page), $businessEntity->getEndpoint());
83
84
        foreach ($data as $entity) {
85
            $entity->_businessEntity = $businessEntity;
86
        }
87
        if (count($data) > 0) {
88
            $data = array_merge($data, $this->getBusinessEntities($businessEntity, ++$page));
89
        }
90
91
        return $data;
92
    }
93
94
    /**
95
     * filter API to get a list of entities.
96
     *
97
     * @param APIBusinessEntity $businessEntity
98
     * @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...
99
     *
100
     * @return mixed
101
     */
102
    public function searchBusinessEntities(APIBusinessEntity $businessEntity, BusinessProperty $businessProperty, $filter)
103
    {
104
        $getMethod = $businessEntity->getListMethod()
105
            .(false !== strpos($businessEntity->getListMethod(), '?') ? '&' : '?')
106
            .$businessProperty->getFilterMethod();
107
        $getMethod = preg_replace('/{{([a-zA-Z]+)}}/', $filter, $getMethod);
108
109
        $data = $this->callApi($businessEntity->getEndpoint()->getHost(), $getMethod, $businessEntity->getEndpoint());
110
111
        foreach ($data as $entity) {
112
            $entity->businessEntity = $businessEntity;
113
        }
114
115
        return $data;
116
    }
117
118
    /**
119
     * Sends a curl request to a given path.
120
     *
121
     * @param APIEndpoint $endPoint
122
     *
123
     * @return array
124
     */
125
    protected function callApi($host, $getMethod, APIEndpoint $endPoint)
126
    {
127
        $token = $endPoint->getToken();
128
        $curl = curl_init();
129
        if ($tokenType = $endPoint->getTokenType()) {
130
            $this->authenticationChain->resolve($tokenType)->handle($curl, $getMethod, $token);
131
        }
132
        curl_setopt($curl, CURLOPT_URL, $host.$getMethod);
133
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
134
135
        $result = curl_exec($curl);
136
137
        curl_close($curl);
138
139
        return json_decode($result);
140
    }
141
}
142