SidFactory::createFromSaleResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Skrill\Factory;
6
7
use Exception;
8
use SimpleXMLElement;
9
use DateTimeImmutable;
10
use Skrill\ValueObject\Sid;
11
use Psr\Http\Message\ResponseInterface;
12
use Skrill\Exception\InvalidSidException;
13
use Skrill\Exception\SkrillResponseException;
14
15
/**
16
 * Class SidFactory.
17
 */
18
final class SidFactory
19
{
20
    /**
21
     * @param string $value
22
     *
23
     * @return Sid
24
     *
25
     * @throws InvalidSidException
26
     * @throws Exception
27
     */
28
    public static function createFromString(string $value): Sid
29
    {
30
        return new Sid($value, (new DateTimeImmutable())->modify('+15 minutes'));
31
    }
32
33
    /**
34
     * @param ResponseInterface $response
35
     *
36
     * @return Sid
37
     *
38
     * @throws InvalidSidException
39
     * @throws SkrillResponseException
40
     */
41
    public static function createFromXMLResponse(ResponseInterface $response): Sid
42
    {
43
        $xml = new SimpleXMLElement($response->getBody()->getContents());
44
45
        if ($xml->xpath('error/error_msg')) {
46
            throw SkrillResponseException::fromSkillError((strval($xml->error->error_msg)));
47
        }
48
49
        return self::createFromString(strval($xml->sid));
50
    }
51
52
    /**
53
     * @param ResponseInterface $response
54
     *
55
     * @return Sid
56
     *
57
     * @throws InvalidSidException
58
     * @throws SkrillResponseException
59
     */
60
    public static function createFromSaleResponse(ResponseInterface $response): Sid
61
    {
62
        $content = $response->getBody()->getContents();
63
        $result = json_decode($content);
64
65
        if (JSON_ERROR_NONE == json_last_error()) {
66
            throw SkrillResponseException::fromSkillError($result->message);
67
        }
68
69
        return self::createFromString($content);
70
    }
71
}
72