Issues (14)

src/PatchManager/OperationMatcher.php (2 issues)

1
<?php
2
3
namespace Cypress\PatchManager;
4
5
use Cypress\PatchManager\Request\Operations;
6
use PhpCollection\Sequence;
7
8
/**
9
 * Match the correct handlers based on actual operations
10
 */
11
class OperationMatcher
12
{
13
    /**
14
     * @var Sequence
15
     */
16
    private Sequence $handlers;
0 ignored issues
show
Private member variable "handlers" must contain a leading underscore
Loading history...
17
18
    /**
19
     * @var Operations
20
     */
21
    private Operations $operations;
0 ignored issues
show
Private member variable "operations" must contain a leading underscore
Loading history...
22
23
    /**
24
     * @param Operations $operations
25
     */
26 7
    public function __construct(Operations $operations)
27
    {
28 7
        $this->handlers = new Sequence();
29 7
        $this->operations = $operations;
30
    }
31
32
    /**
33
     * @param PatchOperationHandler $handler
34
     */
35 6
    public function addHandler(PatchOperationHandler $handler): void
36
    {
37 6
        $this->handlers->add($handler);
38
    }
39
40
    /**
41
     * @param array|Patchable|\Traversable $subject a Patchable instance or a collection of instances
42
     *
43
     * @throws Exception\MissingOperationNameRequest
44
     * @throws Exception\MissingOperationRequest
45
     * @throws Exception\InvalidJsonRequestContent
46
     * @return Sequence
47
     */
48 7
    public function getMatchedOperations($subject): Sequence
49
    {
50 7
        $handlers = $this->handlers;
51
52 7
        return $this->operations
53 7
            ->all()
54 7
            ->foldLeft(
55 7
                new Sequence(),
56 7
                function (Sequence $matchedOperations, array $operationData) use ($handlers, $subject) {
57 7
                    $handler = $handlers->find(fn (PatchOperationHandler $patchHandler) => $operationData[Operations::OP_KEY_NAME] === $patchHandler->getName());
58 7
                    if ($handler->isDefined()) {
59
                        /** @var PatchOperationHandler $patchOperationHandler */
60 4
                        $patchOperationHandler = $handler->get();
61 4
                        if ($patchOperationHandler->canHandle($subject)) {
62 3
                            $matchedOperations->add(MatchedPatchOperation::create($operationData, $handler->get()));
63
                        }
64
                    }
65
66 7
                    return $matchedOperations;
67 7
                }
68 7
            );
69
    }
70
71
    /**
72
     * @param array|Patchable|\Traversable $subject a Patchable instance or a collection of instances
73
     * @throws Exception\InvalidJsonRequestContent
74
     * @throws Exception\MissingOperationNameRequest
75
     * @throws Exception\MissingOperationRequest
76
     * @return Sequence
77
     */
78 2
    public function getUnmatchedOperations($subject): Sequence
79
    {
80 2
        return $this->operations
81 2
            ->all()
82 2
            ->filter(fn (array $operationData) => $operationData !== $this->getMatchedOperations($subject))
83 2
            ->map(fn (array $operationData) => $operationData['op']);
84
    }
85
}
86