InstallationService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 108
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createInstallation() 0 7 1
A createDeviceServer() 0 14 1
A createSessionServer() 0 8 1
A listDeviceServers() 0 6 1
A listInstallations() 0 4 1
A installationById() 0 4 1
A serverPublicKey() 0 4 1
1
<?php declare(strict_types = 1);
2
3
namespace Link0\Bunq\Service;
4
5
use Link0\Bunq\Client;
6
use Link0\Bunq\Domain\DeviceServer;
7
use Link0\Bunq\Domain\Id;
8
use Link0\Bunq\Domain\Installation;
9
use Link0\Bunq\Domain\Keypair;
10
use Link0\Bunq\Domain\Token;
11
12
final class InstallationService
13
{
14
    /**
15
     * @var Client
16
     */
17
    private $client;
18
19
    /**
20
     * @param Client $client
21
     */
22
    public function __construct(Client $client)
23
    {
24
        $this->client = $client;
25
    }
26
27
    /**
28
     * @param Keypair $keypair
29
     * @return array
30
     *
31
     * Array(
32
     *   'Id' => ...
33
     *   'Token' => ...
34
     *   'ServerPublicKey' ...
35
     * )
36
     */
37
    public function createInstallation(Keypair $keypair): array
38
    {
39
        // TODO: This doesn't need the signature middleware
40
        return $this->client->post('installation', [
41
            'client_public_key' => (string) $keypair->publicKey(),
42
        ]);
43
    }
44
45
    /**
46
     * @return Id $deviceServerId
47
     */
48
    public function createDeviceServer($token, string $apiKey, string $description): Id
49
    {
50
        $permittedIps = [];
51
52
        $body = [
53
            'description' => $description,
54
            'secret' => $apiKey,
55
            'permitted_ips' => $permittedIps,
56
        ];
57
58
        return $this->client->post('device-server', $body, [
59
            'X-Bunq-Client-Authentication' => (string) $token,
60
        ])[0];
61
    }
62
63
    /**
64
     * @param $token
65
     * @param string $apiKey
66
     * @return array
67
     *
68
     * Array(
69
     *   'Id' => ...
70
     *   'Token' => ...
71
     *   'UserCompany' => ...
72
     * )
73
     */
74
    public function createSessionServer($token, string $apiKey): array
75
    {
76
        $body = ['secret' => $apiKey];
77
78
        return $this->client->post('session-server', $body, [
79
            'X-Bunq-Client-Authentication' => $token,
80
        ]);
81
    }
82
83
    /**
84
     * @param Installation $installation
85
     * @return DeviceServer[]
86
     */
87
    public function listDeviceServers(Installation $installation): array
88
    {
89
        return $this->client->get('device-server', [
90
            'X-Bunq-Client-Authentication' => $installation->token(),
91
        ]);
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function listInstallations()
98
    {
99
        return $this->client->get('installation');
100
    }
101
102
    /**
103
     * @param Id $installationId
104
     * @return Installation[]
105
     */
106
    public function installationById(Id $installationId)
107
    {
108
        return $this->client->get('installation/' . $installationId);
109
    }
110
111
    /**
112
     * @param Installation $installation
113
     * @return array
114
     */
115
    public function serverPublicKey(Installation $installation)
116
    {
117
        return $this->client->get('installation/' . $installation->id() . '/server-public-key');
118
    }
119
}
120