OpenSubtitles   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 78
rs 10
c 2
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A login() 0 4 1
A __get() 0 12 3
A find() 0 3 1
A logout() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OpenSubtitles;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\GuzzleException;
9
use OpenSubtitles\Endpoints\Authentication;
10
use OpenSubtitles\Endpoints\Download;
11
use OpenSubtitles\Endpoints\Endpoint;
12
use OpenSubtitles\Endpoints\Subtitle;
13
use OpenSubtitles\Exceptions\UnsupportedEndpoint;
14
use Psr\Http\Client\ClientInterface;
15
16
/**
17
 * Class OpenSubtitles
18
 * @package OpenSubtitles
19
 *
20
 * @property Authentication $authentication
21
 * @property Subtitle $subtitle
22
 * @property Download $download
23
 */
24
class OpenSubtitles
25
{
26
    private string $baseUrl = 'https://www.opensubtitles.com/api/v1';
27
28
    private ?string $apiKey;
29
30
    private ClientInterface $client;
31
32
    private array $routes = [
33
        'authentication' => Authentication::class,
34
        'subtitle' => Subtitle::class,
35
        'download' => Download::class,
36
    ];
37
38
    private array $container = [];
39
40
    public function __construct(string $apiKey = null, ClientInterface $client = null)
41
    {
42
        $this->apiKey = $apiKey;
43
        $this->client = $client ?: new Client();
44
    }
45
46
    /**
47
     * Create a token to authenticate a user
48
     *
49
     * @param string $username
50
     * @param string $password
51
     * @return mixed
52
     * @throws GuzzleException
53
     */
54
    public function login(string $username, string $password)
55
    {
56
        return (new Authentication($this->client, $this->baseUrl, $this->apiKey))->login(
57
            compact('username', 'password')
58
        );
59
    }
60
61
    /**
62
     * Destroy a user token to end a session
63
     *
64
     * @param string $accessToken
65
     * @return mixed
66
     * @throws GuzzleException
67
     */
68
    public function logout(string $accessToken)
69
    {
70
        return (new Authentication($this->client, $this->baseUrl, $this->apiKey))->logout($accessToken);
71
    }
72
73
    /**
74
     * Find subtitle for a movie or tv show
75
     *
76
     * @param array $params
77
     * @return mixed
78
     * @throws GuzzleException
79
     */
80
    public function find(array $params)
81
    {
82
        return (new Subtitle($this->client, $this->baseUrl, $this->apiKey))->find($params);
83
    }
84
85
    /**
86
     * @param $name
87
     * @return Endpoint
88
     * @throws UnsupportedEndpoint
89
     */
90
    public function __get($name): Endpoint
91
    {
92
        $key = strtolower($name);
93
        if (!array_key_exists($key, $this->routes)) {
94
            throw new UnsupportedEndpoint();
95
        }
96
97
        if (array_key_exists($key, $this->container)) {
98
            return $this->container[$key];
99
        }
100
101
        return $this->container[$key] = (new $this->routes[$key]($this->client, $this->baseUrl, $this->apiKey));
102
    }
103
}
104