Rate::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.6666
cc 4
nc 2
nop 8
crap 4

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
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\ExchangeRate\Model;
11
12
use RunOpenCode\ExchangeRate\Contract\RateInterface;
13
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil;
14
15
/**
16
 * Class Rate
17
 *
18
 * Default implementation of exchange rate value.
19
 *
20
 * @package RunOpenCode\ExchangeRate\Model
21
 */
22
class Rate implements RateInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $sourceName;
28
29
    /**
30
     * @var float
31
     */
32
    protected $value;
33
34
    /**
35
     * @var string
36
     */
37
    protected $currencyCode;
38
39
    /**
40
     * @var string
41
     */
42
    protected $rateType;
43
44
    /**
45
     * @var \DateTime
46
     */
47
    protected $date;
48
49
    /**
50
     * @var string
51
     */
52
    protected $baseCurrencyCode;
53
54
    /**
55
     * @var \DateTime
56
     */
57
    protected $createdAt;
58
59
    /**
60
     * @var \DateTime
61
     */
62
    protected $modifiedAt;
63
64
    /**
65
     * Rate constructor.
66
     *
67
     * @param string $sourceName
68
     * @param float $value
69
     * @param string $currencyCode
70
     * @param string $rateType
71
     * @param \DateTime|int $date
72
     * @param string $baseCurrencyCode
73
     * @param null|\DateTime|int $createdAt
74
     * @param null|\DateTime|int $modifiedAt
75
     *
76
     * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException
77
     */
78 41
    public function __construct($sourceName, $value, $currencyCode, $rateType, $date, $baseCurrencyCode, $createdAt = null, $modifiedAt = null)
79
    {
80 41
        $this->sourceName = $sourceName;
81 41
        $this->value = is_numeric($value) ? (float) $value : $value;
82 41
        $this->currencyCode = CurrencyCodeUtil::clean($currencyCode);
83 41
        $this->rateType = $rateType;
84 41
        $this->baseCurrencyCode = CurrencyCodeUtil::clean($baseCurrencyCode);
85
86 41
        $processDate = function ($arg) {
87 41
            $arg = (null === $arg) ? new \DateTime('now') : $arg;
88
89 41
            return is_numeric($arg) ? date_timestamp_set(new \DateTime(), $arg) : clone $arg;
90
        };
91
92 41
        $this->date = $processDate($date);
93 41
        $this->createdAt = $processDate($createdAt);
94 41
        $this->modifiedAt = $processDate($modifiedAt);
95 41
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 36
    public function getSourceName()
101
    {
102 36
        return $this->sourceName;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 26
    public function getValue()
109
    {
110 26
        return $this->value;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 36
    public function getCurrencyCode()
117
    {
118 36
        return $this->currencyCode;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 35
    public function getRateType()
125
    {
126 35
        return $this->rateType;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 35
    public function getDate()
133
    {
134 35
        return ($this->date) ? clone $this->date : null;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 17
    public function getBaseCurrencyCode()
141
    {
142 17
        return $this->baseCurrencyCode;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 7
    public function getCreatedAt()
149
    {
150 7
        return clone $this->createdAt;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 7
    public function getModifiedAt()
157
    {
158 7
        return clone $this->modifiedAt;
159
    }
160
}
161