PaginationQuery::createQuery()   F
last analyzed

Complexity

Conditions 23
Paths 1920

Size

Total Lines 80
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 552

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 39
dl 0
loc 80
ccs 0
cts 56
cp 0
rs 0
c 3
b 1
f 0
cc 23
nc 1920
nop 2
crap 552

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 Jobs\Repository\Filter;
10
11
use Core\Repository\Filter\AbstractPaginationQuery;
12
use Jobs\Entity\Status;
13
use Auth\Entity\User;
14
use Laminas\Authentication\AuthenticationService;
15
use Laminas\Permissions\Acl\Acl;
16
use Laminas\Stdlib\Parameters;
17
use Auth\Entity\UserInterface;
18
use DateTime;
19
use MongoDB\BSON\ObjectId;
20
21
/**
22
 * maps query parameters to entity attributes
23
 *
24
 * @author Carsten Bleek <[email protected]>
25
 * @author Miroslav Fedeleš <[email protected]>
26
 */
27
class PaginationQuery extends AbstractPaginationQuery
28
{
29
30
    /**
31
     * @var AuthenticationService
32
     */
33
    protected $auth;
34
35
    /**
36
     * @var Acl
37
     */
38
    protected $acl;
39
40
    /**
41
     * @var array
42
     */
43
    protected $value;
44
45
    /**
46
     * @var UserInterface
47
     */
48
    protected $user;
49
50
    /**
51
     * @param $auth
52
     * @param $acl
53
     */
54
    public function __construct(AuthenticationService $auth, Acl $acl)
55
    {
56
        $this->auth = $auth;
57
        $this->acl = $acl;
58
    }
59
60
    /**
61
     * for people
62
     * following parameter are relevant
63
     * by     => 'all', 'me', 'guest'
64
     * status => Status::CREATED, 'all'
65
     * user   => User::ROLE_RECRUITER, User::ROLE_ADMIN, User::ROLE_USER
66
     *
67
     * @param $params Parameters
68
     * @param $queryBuilder \Doctrine\ODM\MongoDB\Query\Builder
69
     * @return mixed
70
     */
71
    public function createQuery($params, $queryBuilder)
72
    {
73
        $this->value = $params;
74
75
        if (isset($params['q'])) {
76
            $params['search'] = $params['q'];
77
        }
78
79
        /*
80
         * search jobs by keywords
81
         */
82
        if (isset($params['search']) && !empty($params['search'])) {
83
            $search = strtolower($params['search']);
84
            //$expression = $queryBuilder->expr()->operator('$text', ['$search' => $search]);
85
            //$queryBuilder->field(null)->equals($expression->getQuery());
86
            $queryBuilder->text($search);
87
        }
88
        if (isset($params['o']) && !empty($params['o'])) {
89
            $queryBuilder->field('organization')->equals(new ObjectId($params['o']));
90
//            $queryBuilder->field('metaData.companyName')->equals(new \MongoRegex('/' . $params['o'] . '/i'));
91
        }
92
93
        if (isset($params['l'])) {
94
            $coords = $params['l']->getCoordinates()->getCoordinates();
95
            $queryBuilder->field('locations.coordinates')->geoWithinCenter((float) $coords[0], (float) $coords[1], (float) $this->value['d']/100);
96
        }
97
98
        if (isset($params['channel']) && !empty($params['channel']) && $params['channel']!="default") {
99
            $queryBuilder->field('portals')->equals($params['channel']);
100
        }
101
102
        $this->user = $this->auth->getUser();
0 ignored issues
show
introduced by
The method getUser() does not exist on Laminas\Authentication\AuthenticationService. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

102
        /** @scrutinizer ignore-call */ 
103
        $this->user = $this->auth->getUser();
Loading history...
103
        $isRecruiter = $this->user->getRole() == User::ROLE_RECRUITER || $this->acl->inheritsRole($this->user, User::ROLE_RECRUITER);
104
105
        if ($isRecruiter && (!isset($this->value['by']) || $this->value['by'] != 'guest')) {
106
            /*
107
             * a recruiter can see his jobs and jobs from users who gave permissions to do so
108
             */
109
            if (isset($params['by']) && 'me' == $params['by']) {
110
                $queryBuilder->addAnd(
111
                    $queryBuilder->expr()
112
                        ->addOr($queryBuilder->expr()->field('user')->equals($this->user->getId()))
113
                        ->addOr($queryBuilder->expr()->field('metaData.organizations:managers.id')->equals($this->user->getId()))
114
                );
115
            } else {
116
                $queryBuilder->field('permissions.view')->equals($this->user->getId());
117
            }
118
            if (
119
                isset($params['status']) &&
120
                !empty($params['status']) &&
121
                $params['status'] != 'all'
122
            ) {
123
                $queryBuilder->field('status.name')->equals((string) $params['status']);
124
            }
125
        } else {
126
            /*
127
             * an applicants or guests can see all active jobs
128
             * Active jobs are also jobs which are WAITING_FOR_APPROVAL, because the actual change is
129
             * only in the snapshot.
130
             */
131
            $queryBuilder->field('status.name')->in([Status::ACTIVE, Status::WAITING_FOR_APPROVAL]);
132
        }
133
134
        if (isset($params['publishedSince'])) {
135
            $publishedSince = $params['publishedSince'];
136
137
            if (!$publishedSince instanceof DateTime) {
138
                $publishedSince = new DateTime($publishedSince);
139
            }
140
141
            $queryBuilder->field('datePublishStart.date')->gte($publishedSince);
142
        }
143
144
        if (isset($this->value['sort'])) {
145
            foreach (explode(",", $this->value['sort']) as $sort) {
146
                $queryBuilder->sort($this->filterSort($sort));
147
            }
148
        }
149
150
        return $queryBuilder;
151
    }
152
153
    protected function filterSort($sort)
154
    {
155
        if (0 === strpos($sort, '-')) {
156
            $sortProp = substr($sort, 1);
157
            $sortDir  = -1;
158
        } else {
159
            $sortProp = $sort;
160
            $sortDir = 1;
161
        }
162
        switch ($sortProp) {
163
            case "date":
164
                $sortProp = "datePublishStart.date";
165
                break;
166
            case "title":
167
                $sortProp = "title";
168
                break;
169
            case "cam":
170
                $sortProp = "atsEnabled";
171
                break;
172
173
            default:
174
                break;
175
        }
176
        return array($sortProp => $sortDir);
177
    }
178
}
179