Utils::verifyOptions()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\Api.
7
 *
8
 * (c) Eric Sizemore <[email protected]>
9
 *
10
 * This source file is subject to the MIT license. For the full
11
 * copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Esi\Api;
16
17
use InvalidArgumentException;
18
19
use function array_filter;
20
use function implode;
21
use function ltrim;
22
use function str_ends_with;
23
24
use const ARRAY_FILTER_USE_BOTH;
25
26
abstract class Utils
27
{
28
    /**
29
     * @var string[]
30
     */
31
    public const AvailableMethods = ['HEAD', 'GET', 'DELETE', 'OPTIONS', 'PATCH', 'POST', 'PUT', ];
32
33
    /**
34
     * @var string[]
35
     */
36
    public const ValidGuzzleOptions = [
37
        'allow_redirects',
38
        'auth',
39
        'body',
40
        'cert',
41
        'cookies',
42
        'connect_timeout',
43
        'debug',
44
        'decode_content',
45
        'delay',
46
        'expect',
47
        'force_ip_resolve',
48
        'form_params',
49
        'headers',
50
        'idn_conversion',
51
        'json',
52
        'multipart',
53
        'on_headers',
54
        'on_stats',
55
        'progress',
56
        'proxy',
57
        'query',
58
        'read_timeout',
59
        'sink',
60
        'ssl_key',
61
        'stream',
62
        'synchronous',
63
        'verify',
64
        'timeout',
65
        'version',
66
    ];
67
68 16
    public static function normalizeEndpoint(?string $endpoint, string $apiUrl): string
69
    {
70 16
        $endpoint = ltrim((string) $endpoint, '/');
71
72 16
        if (!str_ends_with($apiUrl, '/')) {
73 9
            $endpoint = \sprintf('/%s', $endpoint);
74
        }
75
76 16
        return $endpoint;
77
    }
78
79 17
    public static function verifyMethod(string $method): void
80
    {
81
        // Check for a valid method
82 17
        if (!\in_array($method, self::AvailableMethods, true)) {
83 1
            throw new InvalidArgumentException(\sprintf(
84 1
                'Invalid request method specified, must be one of %s.',
85 1
                implode(', ', self::AvailableMethods)
86 1
            ));
87
        }
88
    }
89
90
    /**
91
     * Performs a rather bare-bones 'verification' of Guzzle options.
92
     *
93
     * @param array<string, mixed> $options
94
     */
95 16
    public static function verifyOptions(array $options): void
96
    {
97
        // Remove options not recognized by Guzzle, or ones we want to keep as default.
98 16
        unset($options['persistentHeaders'], $options['http_errors']);
99
100 16
        $invalidOptions = [];
101
102 16
        array_filter($options, static function ($value, int|string $key) use (&$invalidOptions): bool {
103 13
            if (!\in_array($key, self::ValidGuzzleOptions, true)) {
104 1
                $invalidOptions[] = $key;
105
106 1
                return true;
107
            }
108
109 12
            return false;
110 16
        }, ARRAY_FILTER_USE_BOTH);
111
112 16
        if ($invalidOptions !== []) {
113 1
            throw new InvalidArgumentException(\sprintf('Invalid option(s) specified: %s', implode(', ', $invalidOptions)));
114
        }
115
    }
116
}
117