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

Operations   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 23
c 3
b 0
f 0
dl 0
loc 66
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isAssociative() 0 3 1
A all() 0 14 2
A toSequence() 0 5 2
A parseJson() 0 17 4
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