AbstractPayment::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 6
nop 1
1
<?php
2
3
namespace Eo\KeyClient\Payment;
4
5
use Eo\KeyClient\Exception\KeyClientException;
6
7
/**
8
 * AbstractPayment
9
 */
10
abstract class AbstractPayment
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $params = array();
16
17
    /**
18
     * Class constructor
19
     *
20
     * @param array $params
21
     */
22
    public function __construct(array $params = array())
23
    {
24
        $missing = array();
25
        foreach ($this->getRequiredParams() as $key) {
26
            if (array_key_exists($key, $params) === false) {
27
                array_push($missing, $key);
28
            }
29
        }
30
31
        if (empty($missing) === false) {
32
            throw new KeyClientException('Missing parameters passed: ' . implode(', ', $missing));
33
        }
34
35
        $this->params = $params;
36
    }
37
38
    /**
39
     * Configure params
40
     *
41
     * @return array
42
     */
43
    abstract protected function getRequiredParams();
44
45
    /**
46
     * @return array
47
     */
48
    public function toArray()
49
    {
50
        return $this->params;
51
    }
52
53
    /**
54
     * Setter
55
     *
56
     * @param  string $key
57
     * @param  mixed  $val
58
     * @return self
59
     */
60
    public function set($key, $val)
61
    {
62
        $this->params[$key] = $val;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Getter
69
     *
70
     * @param  string $key
71
     * @param  mixed  $default
72
     * @return mixed
73
     */
74
    public function get($key, $default = null)
75
    {
76
        if (array_key_exists($key, $this->params)) {
77
            return $this->params[$key];
78
        }
79
80
        return $default;
81
    }
82
}