Test Failed
Branch main (8f4107)
by Bingo
08:27 queued 02:15
created

QueryMaxResultsLimitUtil::checkMaxResultsLimit()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 19
rs 8.4444
cc 8
nc 6
nop 3
1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
use Jabe\Engine\BadUserRequestException;
6
use Jabe\Engine\Impl\ProcessEngineException;
7
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
8
use Jabe\Engine\Impl\Context\Context;
9
10
class QueryMaxResultsLimitUtil
11
{
12
    public static function checkMaxResultsLimit(int $resultsCount, int $maxResultsLimit = null, bool $isUserAuthenticated = null): void
13
    {
14
        if ($maxResultsLimit === null && $isUserAuthenticated === null) {
15
            $processEngineConfiguration = Context::getProcessEngineConfiguration();
16
            if ($processEngineConfiguration === null) {
17
                throw new ProcessEngineException("Command context unset.");
18
            }
19
20
            self::checkMaxResultsLimit(
21
                $resultsCount,
22
                self::getMaxResultsLimit($processEngineConfiguration),
0 ignored issues
show
Bug introduced by
The method getMaxResultsLimit() does not exist on Jabe\Engine\Impl\Util\QueryMaxResultsLimitUtil. ( Ignorable by Annotation )

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

22
                self::/** @scrutinizer ignore-call */ 
23
                      getMaxResultsLimit($processEngineConfiguration),

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...
23
                self::isUserAuthenticated($processEngineConfiguration)
24
            );
25
        } else {
26
            if ($isUserAuthenticated && $maxResultsLimit < PHP_INT_MAX) {
27
                if ($resultsCount == PHP_INT_MAX) {
28
                    throw new BadUserRequestException("An unbound number of results is forbidden!");
29
                } elseif ($resultsCount > $maxResultsLimit) {
30
                    throw new BadUserRequestException("Max results limit of " . $maxResultsLimit . " exceeded!");
31
                }
32
            }
33
        }
34
    }
35
36
    protected static function isUserAuthenticated(ProcessEngineConfigurationImpl $processEngineConfig): bool
37
    {
38
        $userId = self::getAuthenticatedUserId($processEngineConfig);
39
        return $userId != null && !empty($userId);
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $userId of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
40
    }
41
42
    protected static function getAuthenticatedUserId(ProcessEngineConfigurationImpl $processEngineConfig): ?string
43
    {
44
        $identityService = $processEngineConfig->getIdentityService();
0 ignored issues
show
Bug introduced by
The method getIdentityService() 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

44
        /** @scrutinizer ignore-call */ 
45
        $identityService = $processEngineConfig->getIdentityService();

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...
45
        $currentAuthentication = $identityService->getCurrentAuthentication();
46
        if ($currentAuthentication === null) {
47
            return null;
48
        } else {
49
            return $currentAuthentication->getUserId();
50
        }
51
    }
52
}
53