Passed
Push — bump-dependencies ( 3eb1c7...418b80 )
by Mattia
15:26 queued 12:02
created

Operations::toSequence()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cypress\PatchManager\Request;
6
7
use Cypress\PatchManager\Exception\InvalidJsonRequestContent;
8
use Cypress\PatchManager\Exception\MissingOperationNameRequest;
9
use Cypress\PatchManager\Exception\MissingOperationRequest;
10
use PhpCollection\Sequence;
11
12
class Operations
13
{
14
    public const OP_KEY_NAME = 'op';
15
16
    private Adapter $adapter;
0 ignored issues
show
Coding Style introduced by
Private member variable "adapter" must contain a leading underscore
Loading history...
17
18 8
    public function __construct(Adapter $adapter)
19
    {
20 8
        $this->adapter = $adapter;
21
    }
22
23
    /**
24
     * @throws InvalidJsonRequestContent
25
     * @throws MissingOperationNameRequest
26
     * @throws MissingOperationRequest
27
     */
28 8
    public function all(): Sequence
29
    {
30 8
        $operationsJson = $this->parseJson($this->adapter->getRequestBody());
31 5
        $operations = $this->toSequence($operationsJson);
32 5
        $operationsWithoutOpKey = $operations->filterNot(fn ($operationData) => array_key_exists(self::OP_KEY_NAME, $operationData));
33
34 5
        if (!$operationsWithoutOpKey->isEmpty()) {
35
            /** @var array $operationData */
36 2
            $operationData = $operationsWithoutOpKey->first()->get();
37
38 2
            throw new MissingOperationNameRequest($operationData);
39
        }
40
41 3
        return $operations;
42
    }
43
44
    /**
45
     * @throws InvalidJsonRequestContent
46
     * @throws MissingOperationRequest
47
     */
48 8
    private function parseJson(?string $string): array
49
    {
50 8
        if (is_null($string)) {
51 1
            throw new InvalidJsonRequestContent();
52
        }
53
54
        try {
55 7
            $json = json_decode($string, associative: true, flags: JSON_THROW_ON_ERROR);
56
57
            // we need this control because json_decode('2', true, 512, JSON_THROW_ON_ERROR) returns a valid result: int(2)
58 6
            if (!is_array($json)) {
59 1
                throw new MissingOperationRequest();
60
            }
61
62 5
            return $json;
63 2
        } catch (\JsonException $e) {
64 1
            throw new InvalidJsonRequestContent();
65
        }
66
    }
67
68 5
    private function toSequence(array $operations): Sequence
69
    {
70 5
        $operations = $this->isAssociative($operations) ? [$operations] : $operations;
71
72 5
        return new Sequence($operations);
73
    }
74
75 5
    private function isAssociative(array $arr): bool
76
    {
77 5
        return array_keys($arr) !== range(0, count($arr) - 1);
78
    }
79
}
80