Completed
Push — master ( 8db672...d57acc )
by Nikola
05:16
created

Rate::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of the Exchange Rate Bundle, 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\Bundle\ExchangeRate\Form\Dto;
11
12
use RunOpenCode\ExchangeRate\Contract\RateInterface;
13
use RunOpenCode\ExchangeRate\Model\Rate as ExchangeRate;
14
15
/**
16
 * Class Rate
17
 *
18
 * @package RunOpenCode\Bundle\ExchangeRate\Form\Dto
19
 */
20
class Rate
21
{
22
    /**
23
     * @var string
24
     */
25
    private $rate;
26
27
    /**
28
     * @var \DateTime
29
     */
30
    private $date;
31
32
    /**
33
     * @var float
34
     */
35
    private $value;
36
37
    /**
38
     * @var string
39
     */
40
    private $baseCurrencyCode;
41
42
    /**
43
     * Rate constructor.
44
     *
45
     * @param null $rate
46
     * @param \DateTime|null $date
47
     * @param null $value
48
     */
49 3
    public function __construct($rate = null, \DateTime $date = null, $value = null, $baseCurrencyCode = null)
50
    {
51 3
        $this->rate = $rate;
52 3
        $this->date = $date;
53 3
        $this->value = $value;
54 3
        $this->baseCurrencyCode = $baseCurrencyCode;
55 3
    }
56
57
    /**
58
     * @return string
59
     */
60 3
    public function getRate()
61
    {
62 3
        return $this->rate;
63
    }
64
65
    /**
66
     * @param string $rate
67
     * @return Rate
68
     */
69 2
    public function setRate($rate)
70
    {
71 2
        $this->rate = $rate;
72 2
        return $this;
73
    }
74
75
    /**
76
     * @return \DateTime
77
     */
78 3
    public function getDate()
79
    {
80 3
        return $this->date;
81
    }
82
83
    /**
84
     * @param \DateTime $date
85
     * @return Rate
86
     */
87 2
    public function setDate(\DateTime $date)
88
    {
89 2
        $this->date = $date;
90 2
        return $this;
91
    }
92
93
    /**
94
     * @return float
95
     */
96 3
    public function getValue()
97
    {
98 3
        return $this->value;
99
    }
100
101
    /**
102
     * @param float $value
103
     * @return Rate
104
     */
105 1
    public function setValue($value)
106
    {
107 1
        $this->value = $value;
108 1
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getBaseCurrencyCode()
115
    {
116
        return $this->baseCurrencyCode;
117
    }
118
119
    /**
120
     * @param string $baseCurrencyCode
121
     * @return Rate
122
     */
123
    public function setBaseCurrencyCode($baseCurrencyCode)
124
    {
125
        $this->baseCurrencyCode = $baseCurrencyCode;
126
        return $this;
127
    }
128
129
    /**
130
     * Build \RunOpenCode\ExchangeRate\Model\Rate from DTO object.
131
     *
132
     * @param ExchangeRate|null $exchangeRate Initial rate object that can be used for data population
133
     * @return ExchangeRate
134
     */
135 2
    public function toRate(ExchangeRate $exchangeRate = null)
136
    {
137 2
        list ($sourceName, $rateType, $currencyCode) = explode('.', $this->getRate());
138
139 2
        return new ExchangeRate(
140
            $sourceName, 
141 2
            $this->getValue(),
142
            $currencyCode,
143
            $rateType,
144 2
            $this->getDate(),
145 2
            $this->baseCurrencyCode,
146 2
            (null !== $exchangeRate) ? $exchangeRate->getCreatedAt() : new \DateTime('now'),
147 2
            new \DateTime('now')
148
        );
149
    }
150
151
    /**
152
     * Create DTO object from RateInterface.
153
     *
154
     * @param RateInterface $exchangeRate
155
     * @return Rate
156
     */
157
    public static function fromRateInterface(RateInterface $exchangeRate)
158
    {
159
        $rate = new static();
160
161
        $rate
162
            ->setBaseCurrencyCode($exchangeRate->getBaseCurrencyCode())
163
            ->setDate($exchangeRate->getDate())
164
            ->setValue($exchangeRate->getValue())
165
            ->setRate(sprintf('%s.%s.%s', $exchangeRate->getSourceName(), $exchangeRate->getRateType(), $exchangeRate->getCurrencyCode()));
166
167
        return $rate;
168
    }
169
}
170