GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (baedab)
by Steffen
03:55 queued 01:15
created

ExchangeRatesMapper::parseExchangeRates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace SteffenBrand\CurrCurr\Mapper;
4
5
use DateTime;
6
use Psr\Http\Message\ResponseInterface;
7
use SimpleXMLElement;
8
use SteffenBrand\CurrCurr\Exception\ExchangeRatesMappingFailedException;
9
use SteffenBrand\CurrCurr\Model\ExchangeRate;
10
11
class ExchangeRatesMapper implements MapperInterface
12
{
13
14
    /**
15
     * @param ResponseInterface $response
16
     * @return ExchangeRate[]
17
     */
18
    public function map(ResponseInterface $response): array
19
    {
20
        $body = $response->getBody()->getContents();
21
        $xml = $this->parseBody($body);
22
        $date = $this->parseDate($xml);
23
        $exchangeRates = $this->parseExchangeRates($xml, $date);
24
25
        return $exchangeRates;
26
    }
27
28
    /**
29
     * @param string $body
30
     * @return SimpleXMLElement
31
     */
32
    private function parseBody(string $body): SimpleXMLElement
33
    {
34
        if (empty($body) === false) {
35
            libxml_use_internal_errors(true);
36
            $xml = simplexml_load_string($body);
37
            $errors = libxml_get_errors();
38
            if (empty($errors) === true) {
39
                return $xml;
40
            }
41
        }
42
43
        throw new ExchangeRatesMappingFailedException();
44
    }
45
46
    /**
47
     * @param SimpleXMLElement $xml
48
     * @return DateTime
49
     */
50
    private function parseDate(SimpleXMLElement $xml): DateTime
51
    {
52
        $date = DateTime::createFromFormat('Y-m-d', $xml->Cube->Cube['time']);
53
        if (false !== $date) {
54
            $date->setTime(0, 0);
55
            return $date;
56
        }
57
58
        throw new ExchangeRatesMappingFailedException();
59
    }
60
61
    /**
62
     * @param SimpleXMLElement $xml
63
     * @param DateTime $date
64
     * @return array
65
     */
66
    private function parseExchangeRates(SimpleXMLElement $xml, DateTime $date): array
67
    {
68
        $exchangeRates = [];
69
70
        foreach ($xml->Cube->Cube->Cube as $item) {
71
            $currency = strval($item['currency']);
72
            $rate = floatval($item['rate']);
73
            $exchangeRates[$currency] = new ExchangeRate(
74
                $currency,
75
                $rate,
76
                $date
77
            );
78
        }
79
80
        return $exchangeRates;
81
    }
82
83
}