Passed
Push — master ( 240c83...03f39b )
by Mathias
06:41
created

Job::userJobsQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright https://yawik.org/COPYRIGHT.php
6
 * @license   MIT
7
 * @author Carsten Bleek <[email protected]>
8
 * @author Mathias Gelhausen <[email protected]>
9
 * @author Rafal Ksiazek <[email protected]>
10
 * @author Anthonius Munthi <[email protected]>
11
 * @author Miroslav Fedeleš <[email protected]>
12
 */
13
14
namespace Jobs\Repository;
15
16
use Auth\Entity\UserInterface;
17
use Core\Repository\AbstractRepository;
18
use Doctrine\ODM\MongoDB\MongoDBException;
19
use Doctrine\ODM\MongoDB\Query\Builder as QueryBuilder;
20
use Jobs\Entity\Job as JobEntity;
21
use Jobs\Entity\StatusInterface;
22
use MongoDB\BSON\ObjectId;
23
use MongoDB\BSON\Regex;
24
use Organizations\Entity\Organization;
25
26
/**
27
 * Class Job
28
 *
29
 */
30
class Job extends AbstractRepository
31
{
32
    /**
33
     * Gets a pagination cursor to the jobs collection
34
     *
35
     * @param $params
36
     * @return mixed
37
     */
38
    public function getPaginatorCursor($params)
39
    {
40
        $filter = $this->getService('filterManager')->get('Jobs/PaginationQuery');
41
        /* @var $filter \Core\Repository\Filter\AbstractPaginationQuery  */
42
        $qb = $filter->filter($params, $this->createQueryBuilder());
43
        return $qb->getQuery()->execute();
44
    }
45
46
    /**
47
     * Checks, if a job posting with a certain applyId (external job id) exists
48
     *
49
     * @param $applyId
50
     * @return bool
51
     * @throws MongoDBException
52
     */
53
    public function existsApplyId($applyId)
54
    {
55
        $qb = $this->createQueryBuilder();
56
        $qb->hydrate(false)
57
           ->select('applyId')
58
           ->field('applyId')->equals($applyId);
59
60
        $result = $qb->getQuery()->execute();
61
        $count = $result->count();
62
        return (bool) $count;
63
    }
64
65
    /**
66
     * @param $resourceId
67
     * @return array
68
     */
69
    public function findByAssignedPermissionsResourceId($resourceId)
70
    {
71
        $criteria = $this->getIsDeletedCriteria(
72
                ['permissions.assigned.' . $resourceId => [ '$exists' => true]]
73
        );
74
75
        return $this->findBy($criteria);
76
    }
77
78
    /**
79
     * Look for an drafted Document of a given user
80
     *
81
     * @param $user
82
     * @return JobEntity|null
83
     */
84
    public function findDraft($user)
85
    {
86
        if ($user instanceof UserInterface) {
87
            $user = $user->getId();
88
        }
89
90
        $criteria = $this->getIsDeletedCriteria([
91
            'isDraft' => true,
92
            'user' => $user,
93
        ]);
94
95
        $document = $this->findOneBy($criteria);
96
97
        if (!empty($document)) {
98
            return $document;
99
        }
100
101
        return null;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getUniqueReference()
108
    {
109
        return uniqid();
110
    }
111
112
    /**
113
     * Selects job postings of a certain organization
114
     *
115
     * @param int $organizationId
116
     * @return JobEntity[]
117
     */
118
    public function findByOrganization($organizationId, $status = null)
119
    {
120
        $criteria = $this->getIsDeletedCriteria([
121
            'organization' => new ObjectId($organizationId),
122
        ]);
123
124
        if ($status) {
125
            $criteria['status.name'] = $status;
126
        }
127
        return $this->findBy($criteria);
128
    }
129
130
    /**
131
     * Selects all Organizations with Active Jobs
132
     *
133
     * @param ?string $term
134
     * @param bool $execute
135
     * @return null|Organization[]|QueryBuilder
136
     * @throws MongoDBException
137
     */
138
    public function findActiveOrganizations(?string $term = null, bool $execute = true)
139
    {
140
        $qb = $this->createQueryBuilder();
141
        $qb->distinct('organization')
142
            ->hydrate(true)
143
           ->field('status.name')->notIn([ StatusInterface::EXPIRED, StatusInterface::INACTIVE ]);
144
145
        $q = $qb->getQuery();
146
        $r = $q->execute();
147
        $r = $r->toArray();
148
149
        $qb = $this->dm->createQueryBuilder('Organizations\Entity\Organization');
150
        $qb->field('_id')->in($r);
151
        if ($term) {
152
            $qb->field('_organizationName')
153
                ->equals(new Regex('/' . addslashes($term) . '/i'));
154
        }
155
156
        if($execute){
157
            return $qb->getQuery()->execute();
158
        }
159
160
        return $qb;
161
    }
162
163
    /**
164
     * @param bool $hydrate
165
     * @return array|JobEntity[]
166
     * @throws MongoDBException
167
     */
168
    public function findActiveJob($hydrate = true)
169
    {
170
        $qb = $this->createQueryBuilder()
171
            ->hydrate($hydrate)
172
            ->refresh()
173
            ->field('status.name')->in([StatusInterface::ACTIVE])
174
            ->field('isDraft')->equals(false)
175
        ;
176
        $q  = $qb->getQuery();
177
        $r  = $q->execute();
178
179
        return $r;
180
    }
181
182
    /**
183
     * Get jobs for given user ID
184
     *
185
     * @param string $userId
186
     * @param int|null $limit
187
     * @return JobEntity[]|null
188
     * @throws MongoDBException
189
     * @since 0.27
190
     */
191
    public function getUserJobs(string $userId, ?int $limit = null)
192
    {
193
        $qb = $this->userJobsQuery($userId);
194
195
        if (!is_null($limit)) {
196
            $qb->limit($limit);
197
        }
198
        return $qb->getQuery()->execute();
199
    }
200
201
    /**
202
     * @param string $userId
203
     * @return int
204
     * @throws MongoDBException
205
     */
206
    public function countUserJobs(string $userId): int
207
    {
208
        return $this->userJobsQuery($userId)
209
            ->count()
210
            ->getQuery()
211
            ->execute();
212
    }
213
214
    private function userJobsQuery(string $userId)
215
    {
216
        return $this->createQueryBuilder()
217
            ->field('user')
218
            ->equals($userId)
219
            ->sort(['dateCreated.date' => -1]);
220
    }
221
222
    /**
223
     * Create a query builder instance.
224
     *
225
     * @param bool $isDeleted Value of the isDeleted flag. Pass "null" to ignore this field.
226
     *
227
     * @return QueryBuilder
228
     */
229
    public function createQueryBuilder($isDeleted = false): QueryBuilder
230
    {
231
        $qb =  parent::createQueryBuilder();
232
233
        if (null !== $isDeleted) {
0 ignored issues
show
introduced by
The condition null !== $isDeleted is always true.
Loading history...
234
            $qb->addAnd(
235
               $qb->expr()->addOr($qb->expr()->field('isDeleted')->equals($isDeleted))
236
                          ->addOr($qb->expr()->field('isDeleted')->exists(false))
237
            );
238
        }
239
240
        return $qb;
241
    }
242
243
    private function getIsDeletedCriteria($criteria)
244
    {
245
        $criteria['$or'] = [
246
            ['isDeleted' => ['$exists' => false]],
247
            ['isDeleted' => false],
248
        ];
249
250
        return $criteria;
251
    }
252
253
    public function findOneByApplyId(string $appId)
254
    {
255
        return $this->findOneBy(['applyId' => $appId]);
256
    }
257
}
258