Test Failed
Pull Request — master (#12)
by
unknown
02:04
created

ResultObjectMapper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 7.21 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 8
loc 111
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A mapCollection() 0 10 2
B map() 0 22 6
A getSetterName() 8 8 1
A transformPaymentInstrumentValue() 0 14 3
A transformAuthorizationInformationValue() 0 7 1
A transformThreeDS2DataValue() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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