Passed
Push — bump-dependencies ( b4f034...4ebfc0 )
by Mattia
12:55 queued 09:49
created

Operations::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
     * @return Sequence
28
     */
29 8
    public function all(): Sequence
30
    {
31 8
        $operationsJson = $this->parseJson($this->adapter->getRequestBody());
32 5
        $operations = $this->toSequence($operationsJson);
33 5
        $operationsWithoutOpKey = $operations->filterNot(fn ($operationData) => array_key_exists(self::OP_KEY_NAME, $operationData));
34
35 5
        if (!$operationsWithoutOpKey->isEmpty()) {
36
            /** @var array $operationData */
37 2
            $operationData = $operationsWithoutOpKey->first()->get();
38
39 2
            throw new MissingOperationNameRequest($operationData);
40
        }
41
42 3
        return $operations;
43
    }
44
45
    /**
46
     * @throws InvalidJsonRequestContent
47
     * @throws MissingOperationRequest
48
     */
49 8
    private function parseJson(?string $string): array
50
    {
51 8
        if (is_null($string)) {
52 1
            throw new InvalidJsonRequestContent();
53
        }
54
55
        try {
56 7
            $json = json_decode($string, associative: true, flags: JSON_THROW_ON_ERROR);
57
58
            // we need this control because json_decode('2', true, 512, JSON_THROW_ON_ERROR) returns a valid result: int(2)
59 6
            if (!is_array($json)) {
60 1
                throw new MissingOperationRequest();
61
            }
62
63 5
            return $json;
64 2
        } catch (\JsonException $e) {
65 1
            throw new InvalidJsonRequestContent();
66
        }
67
    }
68
69
    /**
70
     * @param array $operations
71
     * @return Sequence
72
     */
73 5
    private function toSequence(array $operations): Sequence
74
    {
75 5
        $operations = $this->isAssociative($operations) ? [$operations] : $operations;
76
77 5
        return new Sequence($operations);
78
    }
79
80 5
    private function isAssociative(array $arr): bool
81
    {
82 5
        return array_keys($arr) !== range(0, count($arr) - 1);
83
    }
84
}
85