Authentication::logout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OpenSubtitles\Endpoints;
6
7
use GuzzleHttp\ClientInterface;
8
use GuzzleHttp\Exception\GuzzleException;
9
use GuzzleHttp\RequestOptions;
10
use OpenSubtitles\HttpClientHandler;
11
12
class Authentication implements Endpoint
13
{
14
    private string $baseUrl;
15
16
    /**
17
     * @var HttpClientHandler
18
     */
19
    private HttpClientHandler $clientHandler;
20
21
    public function __construct(ClientInterface $client, string $baseUrl, string $apiKey = null)
22
    {
23
        $this->baseUrl = $baseUrl;
24
25
        $this->clientHandler = new HttpClientHandler($client);
26
        if ($apiKey) {
27
            $this->clientHandler->setApiKey($apiKey);
28
        }
29
    }
30
31
    /**
32
     * Create a token to authenticate a user
33
     *
34
     * @param array $credentials
35
     * @return mixed
36
     * @throws GuzzleException
37
     */
38
    public function login(array $credentials)
39
    {
40
        return $this->clientHandler->toJson(
41
            $this->clientHandler->post($this->baseUrl . '/login', [RequestOptions::JSON => $credentials])
42
        );
43
    }
44
45
    /**
46
     * Destroy a user token to end a session
47
     *
48
     * @param string $accessToken
49
     * @return mixed
50
     * @throws GuzzleException
51
     */
52
    public function logout(string $accessToken)
53
    {
54
        return $this->clientHandler->toJson(
55
            $this->clientHandler->setAccessToken($accessToken)->delete($this->baseUrl . '/logout')
56
        );
57
    }
58
}
59