LibrariesIO::user()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 14
rs 10
ccs 10
cts 10
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\LibrariesIO.
7
 *
8
 * (c) 2023-2024 Eric Sizemore <https://github.com/ericsizemore>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13
14
namespace Esi\LibrariesIO;
15
16
use Esi\LibrariesIO\Exception\InvalidApiKeyException;
17
use Esi\LibrariesIO\Exception\InvalidEndpointException;
18
use Esi\LibrariesIO\Exception\RateLimitExceededException;
19
use GuzzleHttp\Exception\ClientException;
20
use GuzzleHttp\Exception\GuzzleException;
21
use Psr\Http\Message\ResponseInterface;
22
use SensitiveParameter;
23
24
/**
25
 * @see Tests\LibrariesIOTest
26
 *
27
 * @psalm-api
28
 */
29
class LibrariesIO extends AbstractClient
30
{
31
    /**
32
     * @param null|array<string, mixed> $clientOptions
33
     *
34
     * @throws InvalidApiKeyException
35
     */
36 40
    public function __construct(#[SensitiveParameter] string $apiKey, ?string $cachePath = null, ?array $clientOptions = null)
37
    {
38 40
        parent::__construct($apiKey, $cachePath, $clientOptions);
39
    }
40
41
    /**
42
     * @throws ClientException
43
     * @throws GuzzleException
44
     * @throws RateLimitExceededException
45
     */
46 4
    public function platform(): ResponseInterface
47
    {
48
        // The only valid endpoint is 'platforms' currently
49 4
        return $this->request('GET', 'platforms');
50
    }
51
52
    /**
53
     * Performs a request to the 'project' endpoint and a subset endpoint, which can be:
54
     * contributors, dependencies, dependent_repositories, dependents, search, sourcerank, or project
55
     *
56
     * @param array<array-key, int|string> $options
57
     *
58
     * @throws InvalidEndpointException
59
     * @throws ClientException
60
     * @throws GuzzleException
61
     * @throws RateLimitExceededException
62
     */
63 10
    public function project(string $endpoint, array $options): ResponseInterface
64
    {
65 10
        [$endpointFormat, $requestMethod] = Utils::endpointParameters('project', $endpoint, $options);
66
67 8
        $options = Utils::validatePagination($options);
68
69 8
        $query = [
70 8
            'page'     => $options['page'],
71 8
            'per_page' => $options['per_page'],
72 8
        ];
73
74
        // If on the 'search' endpoint, we have to provide the query and sort parameters.
75 8
        if ($endpoint === 'search') {
76 2
            $query += [
77 2
                'q'    => $options['query'],
78 2
                'sort' => Utils::searchVerifySortOption((string) $options['sort']),
79 2
            ];
80 2
            $query += Utils::searchAdditionalParams($options);
81
        }
82
83 8
        $query = ['query' => $query];
84
85 8
        return $this->request($requestMethod, $endpointFormat, $query);
86
    }
87
88
    /**
89
     * Performs a request to the 'repository' endpoint and a subset endpoint, which can be:
90
     * dependencies, projects, or repository
91
     *
92
     * @param array<array-key, int|string> $options
93
     *
94
     * @throws InvalidEndpointException
95
     * @throws ClientException
96
     * @throws GuzzleException
97
     * @throws RateLimitExceededException
98
     */
99 8
    public function repository(string $endpoint, array $options): ResponseInterface
100
    {
101 8
        [$endpointFormat, $requestMethod] = Utils::endpointParameters('repository', $endpoint, $options);
102
103 6
        $options = Utils::validatePagination($options);
104
105 6
        $query = [
106 6
            'query' => [
107 6
                'page'     => $options['page'],
108 6
                'per_page' => $options['per_page'],
109 6
            ],
110 6
        ];
111
112 6
        return $this->request($requestMethod, $endpointFormat, $query);
113
    }
114
115
    /**
116
     * Performs a request to the 'subscription' endpoint and a subset endpoint, which can be:
117
     * subscribe, check, update, unsubscribe
118
     *
119
     * @param array<array-key, int|string> $options
120
     *
121
     * @throws InvalidEndpointException
122
     * @throws ClientException
123
     * @throws GuzzleException
124
     * @throws RateLimitExceededException
125
     */
126 6
    public function subscription(string $endpoint, array $options): ResponseInterface
127
    {
128 6
        [$endpointFormat, $requestMethod] = Utils::endpointParameters('subscription', $endpoint, $options);
129
130 4
        $query = [];
131
132 4
        if (isset($options['include_prerelease'])) {
133 2
            $query = ['query' => ['include_prerelease' => $options['include_prerelease']]];
134
        }
135
136 4
        return $this->request($requestMethod, $endpointFormat, $query);
137
    }
138
139
    /**
140
     * Performs a request to the 'user' endpoint and a subset endpoint, which can be:
141
     * dependencies, package_contributions, packages, repositories, repository_contributions, or subscriptions
142
     *
143
     * @param array<array-key, int|string> $options
144
     *
145
     * @throws InvalidEndpointException
146
     * @throws ClientException
147
     * @throws GuzzleException
148
     * @throws RateLimitExceededException
149
     */
150 11
    public function user(string $endpoint, array $options): ResponseInterface
151
    {
152 11
        [$endpointFormat, $requestMethod] = Utils::endpointParameters('user', $endpoint, $options);
153
154 9
        $options = Utils::validatePagination($options);
155
156 9
        $query = [
157 9
            'query' => [
158 9
                'page'     => $options['page'],
159 9
                'per_page' => $options['per_page'],
160 9
            ],
161 9
        ];
162
163 9
        return $this->request($requestMethod, $endpointFormat, $query);
164
    }
165
}
166