Completed
Push — master ( 97c16f...13d085 )
by Nikola
05:14
created

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