PaginationQuery::createQuery()   F
last analyzed

Complexity

Conditions 17
Paths 320

Size

Total Lines 60
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 60
ccs 20
cts 20
cp 1
rs 2.8833
c 1
b 0
f 0
cc 17
nc 320
nop 2
crap 17

How to fix   Long Method    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
 * YAWIK
4
 *
5
 * @copyright https://yawik.org/COPYRIGHT.php
6
 * @license   MIT
7
 */
8
9
namespace Applications\Repository\Filter;
10
11
use Core\Repository\Filter\AbstractPaginationQuery;
12
use Doctrine\MongoDB\Query\Builder;
0 ignored issues
show
Bug introduced by
The type Doctrine\MongoDB\Query\Builder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Laminas\Stdlib\Parameters;
14
use MongoDB\BSON\Regex;
15
use Organizations\Entity\EmployeeInterface;
16
17
/**
18
 * maps query parameters to entity attributes
19
 *
20
 * @author Carsten Bleek <[email protected]>
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @author Mathias Weitz <[email protected]>
23
 * @since 0.29.2 [mg] - modified to match new ApplicationsFilter form parameters.
24
 */
25
class PaginationQuery extends AbstractPaginationQuery
26
{
27
    /**
28
     * Repository to query
29
     *
30
     * @var String
31
     */
32
    protected $repositoryName="Applications/Application";
33
34
    /**
35
     * Sortable fields
36
     *
37
     * @var array
38
     */
39
    protected $sortPropertiesMap = array(
40
        'date' => 'dateCreated.date',
41
    );
42
43
    protected $auth;
44
45
    /**
46 2
     * Constructs pagination query.
47
     *
48 2
     * @param \Auth\AuthenticationService $auth
49
     */
50
    public function __construct($auth)
51
    {
52
        $this->auth = $auth;
53
    }
54
55
    /**
56
     * Creates a query for filtering applications
57
     * @see \Core\Repository\Filter\AbstractPaginationQuery::createQuery()
58 1
     * @param array|Parameters $params
59
     * @param Builder $queryBuilder
60 1
     * @return Builder
61 1
     */
62 1
    public function createQuery($params, $queryBuilder)
63
    {
64 1
        $user = $this->auth->getUser();
65
        $userID = $user->getId();
66
        if ($params instanceof Parameters) {
67 1
            $value = $params->toArray();
68 1
        } else {
69
            $value = $params;
70
        }
71 1
72 1
        if (isset($value['job']) && !empty($value['job'])) {
73
            $queryBuilder->field('job')->equals($value['job']);
74
        }
75 1
76 1
        if (isset($value['unread']) && !empty($value['unread'])) {
77 1
            $queryBuilder->field('readBy')->notEqual($userID);
78
        }
79 1
80 1
        if (isset($value['q']) && !empty($value['q'])) {
81
            $search = strtolower($value['q']);
82 1
            $searchPatterns = array();
83
84
            foreach (explode(' ', $search) as $searchItem) {
85
                $searchPatterns[] = new Regex('^' . $searchItem, 'i');
86
            }
87
            $queryBuilder->field('keywords')->all($searchPatterns);
88
        }
89 1
90 1
        /*
91
         * We only show applications to which the user has view permissions.
92 1
         * and which are not in draft mode
93 1
         */
94
        $queryBuilder->field('permissions.view')->equals($userID)
95
                     ->field('isDraft')->equals(false);
96 1
97 1
        if (!isset($value['sort'])) {
98
            $value['sort'] = '-date';
99 1
        }
100
101 1
        if (isset($value['status']) && 'all' != $value['status']) {
102
            $queryBuilder->field('status.name')->equals($value['status']);
103
        }
104
105
        if ($user->hasOrganization()) {
106
            $org = $user->getOrganization();
107
            if (!$org->isOwner()) {
108
                $org = $org->getOrganization();
109
                $settings = $org->getWorkflowSettings();
110
                if ($settings->hasActiveWorkflow() && $settings->getAssignDepartmentManagersToJobs()) {
0 ignored issues
show
Bug introduced by
The method hasActiveWorkflow() does not exist on Organizations\Entity\WorkflowSettingsInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Organizations\Entity\WorkflowSettingsInterface. ( Ignorable by Annotation )

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

110
                if ($settings->/** @scrutinizer ignore-call */ hasActiveWorkflow() && $settings->getAssignDepartmentManagersToJobs()) {
Loading history...
111
                    $employee = $org->getEmployee($userID);
112
                    if ($employee->getRole() === EmployeeInterface::ROLE_DEPARTMENT_MANAGER) {
113
                        $queryBuilder->field('refs.jobManagers')->equals($userID);
114
                    }
115
                }
116
            }
117
        }
118
119
        $queryBuilder->sort($this->filterSort($value['sort']));
120
121
        return $queryBuilder;
122
    }
123
}
124