RbcRateBuilder::fromJson()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Movavi\Builder;
4
5
use Movavi\Entity\Rate;
6
use Movavi\Exception\NoneRateException;
7
8
/**
9
 * Class RbcRateBuilder
10
 *
11
 * Builder for the Movavi\Entity\Rate
12
 * Implementation of Builder pattern (GoF)
13
 *
14
 * @package Movavi\Builder
15
 */
16
class RbcRateBuilder
17
{
18
    /**
19
     * Returns an instance of Movavi\Entity\Rate,
20
     * by string in json-format from cash.rbc.ru
21
     *
22
     * @param string $currencyFrom
23
     * @param string $currencyTo
24
     * @param \DateTime $date
25
     * @param string $jsonString
26
     *
27
     * @return Rate
28
     *
29
     * @throws NoneRateException
30
     */
31
    public function fromJson(string $currencyFrom, string $currencyTo, \DateTime $date, string $jsonString): Rate
32
    {
33
        $dataObj = json_decode($jsonString);
34
35
        if (empty($dataObj->data->sum_result)) {
36
            throw new NoneRateException('Service cash.rbc.ru has not send any rate data');
37
        }
38
39
        return new Rate($currencyFrom, $currencyTo, $date, $dataObj->data->sum_result);
40
    }
41
}
42