Issues (14)

src/PatchManager/Request/Operations.php (1 issue)

1
<?php
2
3
namespace Cypress\PatchManager\Request;
4
5
use Cypress\PatchManager\Exception\InvalidJsonRequestContent;
6
use Cypress\PatchManager\Exception\MissingOperationNameRequest;
7
use Cypress\PatchManager\Exception\MissingOperationRequest;
8
use PhpCollection\Sequence;
9
10
class Operations
11
{
12
    public const OP_KEY_NAME = 'op';
13
14
    /**
15
     * @var Adapter
16
     */
17
    private Adapter $adapter;
18
19
    /**
20
     * @param Adapter $adapter
21
     */
22 8
    public function __construct(Adapter $adapter)
23
    {
24 8
        $this->adapter = $adapter;
25
    }
26
27
    /**
28
     * @throws InvalidJsonRequestContent
29
     * @throws MissingOperationNameRequest
30
     * @throws MissingOperationRequest
31
     * @return Sequence
32
     */
33 8
    public function all(): Sequence
34
    {
35 8
        $operationsJson = $this->parseJson($this->adapter->getRequestBody());
36 5
        $operations = $this->toSequence($operationsJson);
37 5
        $operationsWithoutOpKey = $operations->filterNot(fn ($operationData) => array_key_exists(self::OP_KEY_NAME, $operationData));
38
39 5
        if (!$operationsWithoutOpKey->isEmpty()) {
40
            /** @var array $operationData */
41 2
            $operationData = $operationsWithoutOpKey->first()->get();
42
43 2
            throw new MissingOperationNameRequest($operationData);
44
        }
45
46 3
        return $operations;
47
    }
48
49
    /**
50
     * @param string $string
51
     * @throws InvalidJsonRequestContent
52
     * @throws MissingOperationRequest
53
     * @return array
54
     */
55 8
    private function parseJson(?string $string): array
56
    {
57
        try {
58 8
            $json = json_decode($string, true, 512, JSON_THROW_ON_ERROR);
0 ignored issues
show
It seems like $string can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            $json = json_decode(/** @scrutinizer ignore-type */ $string, true, 512, JSON_THROW_ON_ERROR);
Loading history...
59
60
            //we need this control because json_decode('2', true, 512, JSON_THROW_ON_ERROR) returns a valid result: int(2)
61 6
            if (!is_array($json)) {
62 1
                throw new MissingOperationRequest();
63
            }
64
65 5
            return $json;
66 3
        } catch (\JsonException $e) {
67 2
            throw new InvalidJsonRequestContent();
68
        }
69
    }
70
71
    /**
72
     * @param array $operations
73
     * @return Sequence
74
     */
75 5
    private function toSequence(array $operations): Sequence
76
    {
77 5
        $operations = $this->isAssociative($operations) ? [$operations] : $operations;
78
79 5
        return new Sequence($operations);
80
    }
81
82
    /**
83
     * @param array $arr
84
     * @return bool
85
     */
86 5
    private function isAssociative($arr): bool
87
    {
88 5
        return array_keys($arr) !== range(0, count($arr) - 1);
89
    }
90
}
91