Completed
Pull Request — master (#8)
by Tomáš
03:09
created

Validator::validateResponseItemHasAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
ccs 5
cts 5
cp 1
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
namespace Inspirum\Balikobot\Services;
4
5
use Inspirum\Balikobot\Exceptions\BadRequestException;
6
use Inspirum\Balikobot\Exceptions\UnauthorizedException;
7
8
class Validator
9
{
10
    /**
11
     * Validate status code
12
     *
13
     * @param int                $statusCode
14
     * @param array<mixed,mixed> $response
15
     *
16
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
17
     */
18 313
    public function validateStatus(int $statusCode, array $response = []): void
19
    {
20
        // unauthorized
21 313
        if ($statusCode === 401 || $statusCode === 403) {
22 2
            throw new UnauthorizedException(null, $statusCode);
23
        }
24
25
        // request error
26 311
        if ($statusCode >= 400) {
27 108
            throw new BadRequestException($response, (int) ($response['status'] ?? $statusCode));
28
        }
29 271
    }
30
31
    /**
32
     * Validate response item status
33
     *
34
     * @param array<mixed,mixed>      $responseItem
35
     * @param array<mixed,mixed>|null $response
36
     * @param bool                    $shouldHaveStatus
37
     *
38
     * @return void
39
     *
40
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
41
     */
42 271
    public function validateResponseStatus(
43
        array $responseItem,
44
        array $response = null,
45
        bool $shouldHaveStatus = true
46
    ): void {
47 271
        if ($shouldHaveStatus === false && isset($responseItem['status']) === false) {
48 31
            return;
49
        }
50
51 261
        $this->validateStatus((int) ($responseItem['status'] ?? 500), $response ?: $responseItem);
52 197
    }
53
54
    /**
55
     * Validate that every response item has given attribute
56
     *
57
     * @param array<int,array<string,mixed>> $items
58
     * @param string                         $attribute
59
     * @param array<mixed,mixed>             $response
60
     *
61
     * @return void
62
     *
63
     * @throws \Inspirum\Balikobot\Exceptions\BadRequestException
64
     */
65 30
    public function validateResponseItemHasAttribute(array $items, string $attribute, array $response): void
66
    {
67 30
        foreach ($items as $item) {
68 30
            if (isset($item[$attribute]) === false) {
69 6
                throw new BadRequestException($response);
70
            }
71
        }
72 24
    }
73
74
    /**
75
     * Validate response array has correct indexes
76
     *
77
     * @param array<mixed,mixed> $response
78
     * @param int                $count
79
     *
80
     * @return void
81
     *
82
     * @throws \Inspirum\Balikobot\Exceptions\BadRequestException
83
     */
84 83
    public function validateIndexes(array $response, int $count): void
85
    {
86 83
        if (array_keys($response) !== range(0, $count - 1)) {
87 15
            throw new BadRequestException($response);
88
        }
89 68
    }
90
}
91