Passed
Pull Request — master (#5)
by Jan-Marten
02:19
created

OperationHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Johmanx10\Transaction;
8
9
use Johmanx10\Transaction\Visitor\OperationVisitorAwareInterface;
10
use Johmanx10\Transaction\Visitor\OperationVisitorInterface;
11
12
class OperationHandler implements
13
    OperationHandlerInterface,
14
    OperationVisitorAwareInterface
15
{
16
    /** @var OperationVisitorInterface[] */
17
    private $visitors = [];
18
19
    /** @var TransactionFactoryInterface */
20
    private $factory;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param TransactionFactoryInterface|null $factory
26
     */
27 1
    public function __construct(TransactionFactoryInterface $factory = null)
28
    {
29 1
        $this->factory = $factory ?? new TransactionFactory();
30 1
    }
31
32
    /**
33
     * Handle the given operations.
34
     *
35
     * @param OperationInterface ...$operations
36
     *
37
     * @return void
38
     */
39 1
    public function handle(OperationInterface ...$operations): void
40
    {
41
        $this
42 1
            ->factory
43 1
            ->createTransaction(...$operations)
44 1
            ->commit(...array_values($this->visitors));
0 ignored issues
show
Unused Code introduced by
The call to Johmanx10\Transaction\Tr...tionInterface::commit() has too many arguments starting with array_values($this->visitors). ( Ignorable by Annotation )

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

44
            ->/** @scrutinizer ignore-call */ commit(...array_values($this->visitors));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
45 1
    }
46
47
    /**
48
     * Attach operation visitors to the current container.
49
     *
50
     * @param OperationVisitorInterface ...$visitors
51
     *
52
     * @return void
53
     */
54 1
    public function attachVisitor(OperationVisitorInterface ...$visitors): void
55
    {
56 1
        foreach ($visitors as $visitor) {
57 1
            $this->visitors[spl_object_hash($visitor)] = $visitor;
58
        }
59 1
    }
60
61
    /**
62
     * Detach operation visitors to the current container.
63
     *
64
     * @param OperationVisitorInterface ...$visitors
65
     *
66
     * @return void
67
     */
68 1
    public function detachVisitor(OperationVisitorInterface ...$visitors): void
69
    {
70 1
        foreach ($visitors as $visitor) {
71 1
            unset($this->visitors[spl_object_hash($visitor)]);
72
        }
73 1
    }
74
}
75