Passed
Push — master ( 67a5da...c35f7b )
by Jared
01:13
created

Device::createToken()   B

Complexity

Conditions 3
Paths 8

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 35
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 23
nc 8
nop 2
1
<?php
2
namespace CultureKings\Afterpay\Service\InStore;
3
4
use CultureKings\Afterpay\Exception\ApiException;
5
use CultureKings\Afterpay\Model;
6
use CultureKings\Afterpay\Traits;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\BadResponseException;
9
use GuzzleHttp\HandlerStack;
10
use JMS\Serializer\SerializationContext;
11
use JMS\Serializer\SerializerInterface;
12
13
/**
14
 * Class Device
15
 * @package CultureKings\Afterpay\Service\InStore
16
 */
17
class Device
18
{
19
    use Traits\ClientTrait;
20
    use Traits\AuthorizationTrait;
21
    use Traits\SerializerTrait;
22
23
    /**
24
     * Device constructor.
25
     *
26
     * @param Model\InStore\Authorization $auth
27
     * @param Client                      $client
28
     * @param SerializerInterface         $serializer
29
     */
30
    public function __construct(Model\InStore\Authorization $auth, Client $client, SerializerInterface $serializer)
31
    {
32
        $this->setAuthorization($auth);
33
        $this->setClient($client);
34
        $this->setSerializer($serializer);
35
    }
36
37
    /**
38
     * @param Model\InStore\Device $device
39
     * @param HandlerStack|null    $stack
40
     *
41
     * @return array|\JMS\Serializer\scalar|Model\InStore\Device
42
     */
43
    public function activate(Model\InStore\Device $device, HandlerStack $stack = null)
44
    {
45
        try {
46
            $params = [
47
                'headers' => [
48
                    'Accept' => 'application/json',
49
                    'Content-Type' => 'application/json',
50
                ],
51
                'body' => $this->getSerializer()->serialize(
52
                    $device,
53
                    'json',
54
                    SerializationContext::create()->setGroups(['activateDevice'])
55
                ),
56
            ];
57
            if ($stack !== null) {
58
                $params['handler'] = $stack;
59
            }
60
61
            $result = $this->getClient()->post('devices/activate', $params);
62
63
            return $this->getSerializer()->deserialize(
64
                (string) $result->getBody(),
65
                Model\InStore\Device::class,
66
                'json'
67
            );
68
        } catch (BadResponseException $e) {
69
            throw new ApiException(
70
                $this->getSerializer()->deserialize(
71
                    (string) $e->getResponse()->getBody(),
72
                    Model\ErrorResponse::class,
73
                    'json'
74
                )
75
            );
76
        }
77
    }
78
79
    /**
80
     * @param Model\InStore\Device $device
81
     * @param HandlerStack|null    $stack
82
     *
83
     * @return array|\JMS\Serializer\scalar|Model\InStore\DeviceToken
84
     */
85
    public function createToken(Model\InStore\Device $device, HandlerStack $stack = null)
86
    {
87
        try {
88
            $params = [
89
                'headers' => [
90
                    'Accept' => 'application/json',
91
                    'Content-Type' => 'application/json',
92
                ],
93
                'body' => $this->getSerializer()->serialize(
94
                    $device,
95
                    'json',
96
                    SerializationContext::create()->setGroups(['createToken'])
97
                ),
98
            ];
99
            if ($stack !== null) {
100
                $params['handler'] = $stack;
101
            }
102
103
            $result = $this->getClient()->post(sprintf('devices/%d/token', $device->getDeviceId()), $params);
104
105
            return $this->getSerializer()->deserialize(
106
                (string) $result->getBody(),
107
                Model\InStore\DeviceToken::class,
108
                'json'
109
            );
110
        } catch (BadResponseException $e) {
111
            throw new ApiException(
112
                $this->getSerializer()->deserialize(
113
                    (string) $e->getResponse()->getBody(),
114
                    Model\ErrorResponse::class,
115
                    'json'
116
                )
117
            );
118
        }
119
    }
120
}
121