Completed
Push — master ( 66b793...4a3c9b )
by Barry vd.
05:45 queued 03:32
created

ParametersTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 77
ccs 14
cts 18
cp 0.7778
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setParameter() 0 6 1
A getParameter() 0 4 1
A getParameters() 0 4 1
A initialize() 0 6 1
A validate() 0 9 3
1
<?php
2
3
namespace Omnipay\Common;
4
5
use Omnipay\Common\Exception\InvalidRequestException;
6
use Symfony\Component\HttpFoundation\ParameterBag;
7
8
trait ParametersTrait
9
{
10
    /**
11
     * Internal storage of all of the parameters.
12
     *
13
     * @var ParameterBag
14
     */
15
    protected $parameters;
16
17
    /**
18
     * Set one parameter.
19
     *
20
     * @param string $key Parameter key
21
     * @param mixed $value Parameter value
22
     * @return $this
23
     */
24 476
    protected function setParameter($key, $value)
25
    {
26 476
        $this->parameters->set($key, $value);
27
28 476
        return $this;
29
    }
30
31
    /**
32
     * Get one parameter.
33
     *
34
     * @return mixed A single parameter value.
35
     */
36 440
    protected function getParameter($key)
37
    {
38 440
        return $this->parameters->get($key);
39
    }
40
41
    /**
42
     * Get all parameters.
43
     *
44
     * @return array An associative array of parameters.
45
     */
46 27
    public function getParameters()
47
    {
48 27
        return $this->parameters->all();
49
    }
50
51
    /**
52
     * Initialize the object with parameters.
53
     *
54
     * If any unknown parameters passed, they will be ignored.
55
     *
56
     * @param array $parameters An associative array of parameters
57
     * @return $this.
0 ignored issues
show
Documentation introduced by
The doc-type $this. could not be parsed: Unknown type name "$this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
58
     */
59
    public function initialize(array $parameters = [])
60
    {
61
        $this->parameters = new ParameterBag;
62
        Helper::initialize($this, $parameters);
63
        return $this;
64
    }
65
66
    /**
67
     * Validate the request.
68
     *
69
     * This method is called internally by gateways to avoid wasting time with an API call
70
     * when the request is clearly invalid.
71
     *
72
     * @param string ... a variable length list of required parameters
73
     * @throws InvalidRequestException
74
     */
75 12
    public function validate(...$args)
76
    {
77 12
        foreach ($args as $key) {
78 12
            $value = $this->parameters->get($key);
79 12
            if (! isset($value)) {
80 6
                throw new InvalidRequestException("The $key parameter is required");
81
            }
82 8
        }
83 9
    }
84
}
85