1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cardinity\Method; |
4
|
|
|
|
5
|
|
|
use Cardinity\Exception; |
6
|
|
|
use Cardinity\Method\Payment\AuthorizationInformation; |
7
|
|
|
use Cardinity\Method\Payment\PaymentInstrumentCard; |
8
|
|
|
use Cardinity\Method\Payment\PaymentInstrumentRecurring; |
9
|
|
|
use Cardinity\Method\Payment\ThreeDS2AuthorizationInformation; |
10
|
|
|
|
11
|
|
|
class ResultObjectMapper implements ResultObjectMapperInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Map each item in response data to instance of ResultObjectInterface |
15
|
|
|
* @param array $response |
16
|
|
|
* @param MethodResultCollectionInterface $method |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public function mapCollection(array $response, MethodResultCollectionInterface $method) |
21
|
|
|
{ |
22
|
|
|
$return = []; |
23
|
|
|
|
24
|
|
|
foreach ($response as $item) { |
25
|
|
|
$return[] = $this->map($item, $method->createResultObject()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Map response data to instance of ResultObjectInterface |
33
|
|
|
* @param array $response |
34
|
|
|
* @param ResultObjectInterface $result |
35
|
|
|
* |
36
|
|
|
* @return ResultObjectInterface |
37
|
|
|
*/ |
38
|
|
|
public function map(array $response, ResultObjectInterface $result) |
39
|
|
|
{ |
40
|
|
|
foreach ($response as $field => $value) { |
41
|
|
|
$method = $this->getSetterName($field); |
42
|
|
|
|
43
|
|
|
if (!method_exists($result, $method)) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($field == 'payment_instrument') { |
48
|
|
|
$value = $this->transformPaymentInstrumentValue($value, $response['payment_method']); |
49
|
|
|
} elseif ($field == 'authorization_information') { |
50
|
|
|
$value = $this->transformAuthorizationInformationValue($value); |
51
|
|
|
} elseif ($field == 'threeds2_data') { |
52
|
|
|
$value = $this->transformThreeDS2DataValue($value); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$result->$method($value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Extracts camelCased setter name from underscore notation. |
63
|
|
|
* Eg. my_field_name => myFieldName |
64
|
|
|
* @param string $field |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
private function getSetterName($field) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$parts = explode('_', $field); |
70
|
|
|
array_map('ucfirst', $parts); |
71
|
|
|
$name = 'set' . implode('', $parts); |
72
|
|
|
|
73
|
|
|
return $name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Transform PaymentInstrument result array to object |
78
|
|
|
* @param array $data |
79
|
|
|
* @param string $method |
80
|
|
|
* @return PaymentInstrumentCard|PaymentInstrumentRecurring |
81
|
|
|
* @throws Exception\Runtime for unsupported methods |
82
|
|
|
*/ |
83
|
|
|
private function transformPaymentInstrumentValue(array $data, $method) |
84
|
|
|
{ |
85
|
|
|
if ($method == 'card') { |
86
|
|
|
$instrument = new PaymentInstrumentCard(); |
87
|
|
|
} elseif ($method == 'recurring') { |
88
|
|
|
$instrument = new PaymentInstrumentRecurring(); |
89
|
|
|
} else { |
90
|
|
|
throw new Exception\Runtime(sprintf('Method "%s" is not supported', $method)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->map($data, $instrument); |
94
|
|
|
|
95
|
|
|
return $instrument; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Transform AuthorizationInformation result array to object |
100
|
|
|
* @param array $data |
101
|
|
|
* @return AuthorizationInformation |
102
|
|
|
*/ |
103
|
|
|
private function transformAuthorizationInformationValue($data) |
104
|
|
|
{ |
105
|
|
|
$info = new AuthorizationInformation(); |
106
|
|
|
$this->map($data, $info); |
107
|
|
|
|
108
|
|
|
return $info; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param ARRAY $data |
113
|
|
|
* @return ThreeDS2AuthorizationInformation |
114
|
|
|
*/ |
115
|
|
|
private function transformThreeDS2DataValue($data) |
116
|
|
|
{ |
117
|
|
|
$threeds2 = new ThreeDS2AuthorizationInformation(); |
118
|
|
|
$this->map($data, $threeds2); |
119
|
|
|
return $threeds2; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.