Completed
Push — master ( 7f7e0b...5ab691 )
by Nikola
02:14
created

Rate::__construct()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3
Metric Value
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 9.4286
cc 3
eloc 12
nc 1
nop 8
crap 3

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace RunOpenCode\ExchangeRate\Model;
4
5
use RunOpenCode\ExchangeRate\Contract\RateInterface;
6
use RunOpenCode\ExchangeRate\Utils\CurrencyCode;
7
8
class Rate implements RateInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $sourceName;
14
15
    /**
16
     * @var float
17
     */
18
    protected $value;
19
20
    /**
21
     * @var string
22
     */
23
    protected $currencyCode;
24
25
    /**
26
     * @var string
27
     */
28
    protected $rateType;
29
30
    /**
31
     * @var \DateTime
32
     */
33
    protected $date;
34
35
    /**
36
     * @var string
37
     */
38
    protected $baseCurrencyCode;
39
40
    /**
41
     * @var \DateTime
42
     */
43
    protected $createdAt;
44
45
    /**
46
     * @var \DateTime
47
     */
48
    protected $modifiedAt;
49
50 2
    public function __construct($sourceName, $value, $currencyCode, $rateType, $date, $baseCurrencyCode, $createdAt = null, $modifiedAt = null)
51
    {
52 2
        $this->sourceName = $sourceName;
53 2
        $this->value = $value;
54 2
        $this->currencyCode = CurrencyCode::validate($currencyCode);
55 2
        $this->rateType = $rateType;
56 2
        $this->baseCurrencyCode = CurrencyCode::validate($baseCurrencyCode);
57
58 2
        $processDate = function($arg) {
59 2
            $arg = (is_null($arg)) ? new \DateTime('now') : $arg;
60 2
            return (is_numeric($arg)) ? date_timestamp_set(new \DateTime(), $arg) : clone $arg;
61 2
        };
62
63 2
        $this->date = $processDate($date);
64 2
        $this->createdAt = $processDate($createdAt);
65 2
        $this->modifiedAt = $processDate($modifiedAt);
66 2
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getSourceName()
72
    {
73
        return $this->sourceName;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    public function getValue()
80
    {
81 2
        return $this->value;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 2
    public function getCurrencyCode()
88
    {
89 2
        return $this->currencyCode;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 2
    public function getRateType()
96
    {
97 2
        return $this->rateType;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getDate()
104
    {
105
        return ($this->date) ? clone $this->date : null;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getBaseCurrencyCode()
112
    {
113
        return $this->baseCurrencyCode;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getCreatedAt()
120
    {
121
        return clone $this->createdAt;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getModifiedAt()
128
    {
129
        return clone $this->modifiedAt;
130
    }
131
}
132