1 | <?php |
||
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) |
||
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 $filter |
||
103 | * |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public function searchBusinessEntities(APIBusinessEntity $businessEntity, BusinessProperty $businessProperty, $filter) |
||
121 | |||
122 | /** |
||
123 | * Sends a curl request to a given path. |
||
124 | * |
||
125 | * @param APIEndpoint $endPoint |
||
126 | * @param string $getMethod |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | protected function callApi(APIEndpoint $endPoint, $getMethod) |
||
147 | } |
||
148 |