Test Failed
Push — main ( a2096a...c7561b )
by Bingo
15:21
created

HistoricProcessInstanceReportImpl::doAuthCheck()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 27
rs 7.3166
c 0
b 0
f 0
cc 11
nc 14
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Jabe\Engine\Impl;
4
5
use Jabe\Engine\Exception\NotValidException;
6
use Jabe\Engine\History\HistoricProcessInstanceReportInterface;
7
use Jabe\Engine\Impl\Context\Context;
8
use Jabe\Engine\Impl\Db\TenantCheck;
9
use Jabe\Engine\Impl\Interceptor\{
10
    CommandInterface,
11
    CommandContext,
12
    CommandExecutorInterface
13
};
14
use Jabe\Engine\Impl\Util\{
15
    CompareUtil,
16
    EnsureUtil
17
};
18
use Jabe\Engine\Repository\ProcessDefinitionInterface;
19
20
class HistoricProcessInstanceReportImpl implements HistoricProcessInstanceReportInterface
21
{
22
    protected $startedAfter;
23
    protected $startedBefore;
24
    protected $processDefinitionIdIn = [];
25
    protected $processDefinitionKeyIn = [];
26
27
    protected $durationPeriodUnit;
28
29
    protected $commandExecutor;
30
31
    protected $tenantCheck;
32
33
    public function __construct(CommandExecutorInterface $commandExecutor = null)
34
    {
35
        $this->tenantCheck = new TenantCheck();
36
        $this->commandExecutor = $commandExecutor;
37
    }
38
39
    public function startedAfter(string $startedAfter): HistoricProcessInstanceReportInterface
40
    {
41
        EnsureUtil::ensureNotNull(NotValidException::class, "startedAfter", $startedAfter);
42
        $this->startedAfter = $startedAfter;
43
        return $this;
44
    }
45
46
    public function startedBefore(string $startedBefore): HistoricProcessInstanceReportInterface
47
    {
48
        EnsureUtil::ensureNotNull(NotValidException::class, "startedBefore", $startedBefore);
49
        $this->startedBefore = $startedBefore;
50
        return $this;
51
    }
52
53
    public function processDefinitionIdIn(array $processDefinitionIds): HistoricProcessInstanceReportInterface
54
    {
55
        EnsureUtil::ensureNotNull("processDefinitionIdIn", "processDefinitionIds", $processDefinitionIds);
56
        $this->processDefinitionIdIn = $processDefinitionIds;
57
        return $this;
58
    }
59
60
    public function processDefinitionKeyIn(array $processDefinitionKeys): HistoricProcessInstanceReportInterface
61
    {
62
        EnsureUtil::ensureNotNull("processDefinitionKeyIn", "processDefinitionKeys", $processDefinitionKeys);
63
        $this->processDefinitionKeyIn = $processDefinitionKeys;
64
        return $this;
65
    }
66
67
    public function duration(string $periodUnit): array
68
    {
69
        EnsureUtil::ensureNotNull(NotValidException::class, "periodUnit", $periodUnit);
70
        $this->durationPeriodUnit = $periodUnit;
71
72
        $commandContext = Context::getCommandContext();
73
74
        if ($commandContext == null) {
75
            return $commandExecutor->execute(new ExecuteDurationReportCmd($this));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $commandExecutor seems to be never defined.
Loading history...
76
        } else {
77
            return $this->executeDurationReport($commandContext);
78
        }
79
    }
80
81
    public function executeDurationReport(CommandContext $commandContext): array
82
    {
83
        $this->doAuthCheck($commandContext);
84
85
        if (CompareUtil::areNotInAscendingOrder($startedAfter, $startedBefore)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $startedAfter seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $startedBefore seems to be never defined.
Loading history...
86
            return [];
87
        }
88
89
        return $commandContext
90
            ->getHistoricReportManager()
91
            ->selectHistoricProcessInstanceDurationReport($this);
92
    }
93
94
    protected function doAuthCheck(CommandContext $commandContext): void
95
    {
96
        // since a report does only make sense in context of historic
97
        // data, the authorization check will be performed here
98
        foreach ($commandContext->getProcessEngineConfiguration()->getCommandCheckers() as $checker) {
0 ignored issues
show
Bug introduced by
The method getCommandCheckers() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. ( Ignorable by Annotation )

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

98
        foreach ($commandContext->getProcessEngineConfiguration()->/** @scrutinizer ignore-call */ getCommandCheckers() as $checker) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
            if (empty($this->processDefinitionIdIn) && empty($this->processDefinitionKeyIn)) {
100
                $checker->checkReadHistoryAnyProcessDefinition();
101
            } else {
102
                $processDefinitionKeys = [];
103
                if (!empty($this->processDefinitionKeyIn)) {
104
                    $processDefinitionKeys = $this->processDefinitionKeyIn;
105
                }
106
107
                if (!empty($this->processDefinitionIdIn)) {
108
                    foreach ($this->processDefinitionIdIn as $processDefinitionId) {
109
                        $processDefinition = $commandContext->getProcessDefinitionManager()
110
                            ->findLatestProcessDefinitionById($processDefinitionId);
111
112
                        if ($processDefinition != null && $processDefinition->getKey() != null) {
113
                            $processDefinitionKeys[] = $processDefinition->getKey();
114
                        }
115
                    }
116
                }
117
118
                if (!empty($processDefinitionKeys)) {
119
                    foreach ($processDefinitionKeys as $processDefinitionKey) {
120
                        $checker->checkReadHistoryProcessDefinition($processDefinitionKey);
121
                    }
122
                }
123
            }
124
        }
125
    }
126
127
    public function getStartedAfter(): string
128
    {
129
        return $this->startedAfter;
130
    }
131
132
    public function getStartedBefore(): string
133
    {
134
        return $this->startedBefore;
135
    }
136
137
    public function getProcessDefinitionIdIn(): array
138
    {
139
        return $this->processDefinitionIdIn;
140
    }
141
142
    public function getProcessDefinitionKeyIn(): array
143
    {
144
        return $this->processDefinitionKeyIn;
145
    }
146
147
    public function getTenantCheck(): TenantCheck
148
    {
149
        return $this->tenantCheck;
150
    }
151
152
    public function getReportPeriodUnitName(): string
153
    {
154
        return $this->durationPeriodUnit;
155
    }
156
}
157