fillGatewaySpecificFields()   C
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 14
Ratio 58.33 %

Code Coverage

Tests 19
CRAP Score 7.2576

Importance

Changes 0
Metric Value
dl 14
loc 24
ccs 19
cts 23
cp 0.8261
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 3
nop 1
crap 7.2576
1
<?php
2
3
namespace Omnipay\Spreedly\Message\Concerns;
4
5
use Omnipay\Common\Exception\InvalidRequestException;
6
use Omnipay\Spreedly\Arr;
7
8
trait HasGatewaySpecificFields
9
{
10
    protected $gatewaySpecificFieldsConfig = array(
11
        'test' => array(
12
            'required' => array(),
13
            'optional' => array(),
14
        ),
15
        'fake' => array(
16
            'required' => array(
17
                'foo',
18
            ),
19
            'optional' => array(
20
                'bar',
21
            ),
22
        ),
23
        'conekta' => array(
24
            'required' => array(),
25
            'optional' => array(
26
                'device_fingerprint',
27
                'customer',
28
                'phone',
29
            ),
30
        ),
31
    );
32
33 18
    public function getGatewaySpecificFields()
34
    {
35 18
        return $this->getParameter('gateways_specific_fields');
0 ignored issues
show
Bug introduced by
It seems like getParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
    }
37
38 2
    public function setGatewaySpecificFields($value)
39
    {
40 2
        return $this->setParameter('gateways_specific_fields', $value);
0 ignored issues
show
Bug introduced by
It seems like setParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
    }
42
43
    /**
44
     * @param array $data
45
     * @return array
46
     * @throws InvalidRequestException
47
     */
48 18
    protected function fillGatewaySpecificFields($data)
49
    {
50 18
        $gateway = $this->getGateway();
0 ignored issues
show
Bug introduced by
The method getGateway() does not exist on Omnipay\Spreedly\Message...asGatewaySpecificFields. Did you maybe mean getGatewaySpecificFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
51 18
        $gatewaySpecificFieldsData = $this->getGatewaySpecificFields();
52 18
        $gatewaySpecificFieldsConfig = Arr::get($this->gatewaySpecificFieldsConfig, $gateway);
53 18
        if ($gatewaySpecificFieldsData && $gatewaySpecificFieldsConfig) {
54 2 View Code Duplication
            foreach ($gatewaySpecificFieldsConfig['required'] as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55 2
                $value = Arr::get($gatewaySpecificFieldsData, $field);
56 2
                if (!is_null($value)) {
57 2
                    Arr::set($data, 'gateway_specific_fields.' . $gateway . '.' . $field, $value);
58 2
                } else {
59
                    throw new InvalidRequestException("Missing gateway specific field: $field.");
60
                }
61 2
            }
62 2 View Code Duplication
            foreach ($gatewaySpecificFieldsConfig['optional'] as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63 2
                $value = Arr::get($gatewaySpecificFieldsData, $field);
64 2
                if (!is_null($value)) {
65 2
                    Arr::set($data, 'gateway_specific_fields.' . $gateway . '.' . $field, $value);
66 2
                }
67 2
            }
68 2
        }
69
70 18
        return $data;
71
    }
72
}
73