Passed
Push — 2.x ( 5e81a5...d097d1 )
by Aleksei
14:17
created

WrappedCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Command\Special;
6
7
use Closure;
8
use Cycle\ORM\Command\CommandInterface;
9
use Cycle\ORM\Command\Database\Insert;
10
use Cycle\ORM\Command\Database\Update;
11
use Cycle\ORM\Heap\State;
12
use Cycle\Database\DatabaseInterface;
13
use Cycle\ORM\MapperInterface;
14
15
class WrappedCommand implements CommandInterface
16
{
17
    private ?Closure $beforeExecute = null;
18
19
    private ?Closure $afterExecute = null;
20
21 200
    protected function __construct(
22
        protected CommandInterface $command
23
    ) {
24
    }
25
26 16
    public static function createInsert(
27
        DatabaseInterface $db,
28
        string $table,
29
        State $state,
30
        ?MapperInterface $mapper,
31
        array $primaryKeys = [],
32
        string $pkColumn = null
33
    ): WrappedStoreCommand {
34 16
        return new WrappedStoreCommand(new Insert($db, $table, $state, $mapper, $primaryKeys, $pkColumn));
35
    }
36
37
    public static function createUpdate(
38
        DatabaseInterface $db,
39
        string $table,
40
        State $state,
41
        ?MapperInterface $mapper,
42
        array $primaryKeys = []
43
    ): WrappedStoreCommand {
44
        return new WrappedStoreCommand(new Update($db, $table, $state, $mapper, $primaryKeys));
45
    }
46
47 32
    public static function wrapCommand(CommandInterface $command): static
48
    {
49 32
        return new static($command);
50
    }
51
52 168
    public function withBeforeExecution(?callable $callable): static
53
    {
54 168
        $clone = clone $this;
55 168
        $clone->beforeExecute = $callable instanceof Closure ? $callable : Closure::fromCallable($callable);
0 ignored issues
show
Bug introduced by
It seems like $callable can also be of type null; however, parameter $callback of Closure::fromCallable() does only seem to accept callable, 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

55
        $clone->beforeExecute = $callable instanceof Closure ? $callable : Closure::fromCallable(/** @scrutinizer ignore-type */ $callable);
Loading history...
56 168
        return $clone;
57
    }
58
59 32
    public function withAfterExecution(?callable $callable): static
60
    {
61 32
        $clone = clone $this;
62 32
        $clone->afterExecute = $callable instanceof Closure ? $callable : Closure::fromCallable($callable);
0 ignored issues
show
Bug introduced by
It seems like $callable can also be of type null; however, parameter $callback of Closure::fromCallable() does only seem to accept callable, 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

62
        $clone->afterExecute = $callable instanceof Closure ? $callable : Closure::fromCallable(/** @scrutinizer ignore-type */ $callable);
Loading history...
63 32
        return $clone;
64
    }
65
66
    public function isReady(): bool
67
    {
68
        return $this->command->isReady();
69
    }
70
71 200
    public function isExecuted(): bool
72
    {
73 200
        return $this->command->isExecuted();
74
    }
75
76 200
    public function execute(): void
77
    {
78 200
        if ($this->beforeExecute !== null) {
79 168
            Closure::bind($this->beforeExecute, null, static::class)($this->command);
80
        }
81 200
        $this->command->execute();
82 200
        if ($this->afterExecute !== null) {
83 32
            Closure::bind($this->afterExecute, null, static::class)($this->command);
84
        }
85
    }
86
87 200
    public function getDatabase(): ?DatabaseInterface
88
    {
89 200
        return $this->command->getDatabase();
90
    }
91
92
    public function getCommand(): CommandInterface
93
    {
94
        return $this->command;
95
    }
96
}
97