Completed
Pull Request — develop (#274)
by
unknown
08:38
created

Job::getUserJobs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
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\Cursor;
19
use Jobs\Entity\StatusInterface;
20
21
/**
22
 * Class Job
23
 *
24
 */
25
class Job extends AbstractRepository
26
{
27
    /**
28
     * Gets a pagination cursor to the jobs collection
29
     *
30
     * @param $params
31
     * @return mixed
32
     */
33 View Code Duplication
    public function getPaginatorCursor($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $filter = $this->getService('filterManager')->get('Jobs/PaginationQuery');
36
        /* @var $filter \Core\Repository\Filter\AbstractPaginationQuery  */
37
        $qb = $filter->filter($params, $this->createQueryBuilder());
38
        return $qb->getQuery()->execute();
39
    }
40
41
    /**
42
     * Checks, if a job posting with a certain applyId (external job id) exists
43
     *
44
     * @param $applyId
45
     * @return bool
46
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
47
     */
48 View Code Duplication
    public function existsApplyId($applyId)
0 ignored issues
show
Coding Style introduced by
function existsApplyId() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $qb = $this->createQueryBuilder();
51
        $qb->hydrate(false)
52
           ->select('applyId')
53
           ->field('applyId')->equals($applyId);
54
           
55
        $result = $qb->getQuery()->execute();
56
        $count = $result->count();
57
        return (bool) $count;
58
    }
59
60
    /**
61
     * @param $resourceId
62
     * @return array
63
     */
64
    public function findByAssignedPermissionsResourceId($resourceId)
65
    {
66
        return $this->findBy(
67
            array(
68
            'permissions.assigned.' . $resourceId => array(
69
                '$exists' => true
70
            )
71
            )
72
        );
73
    }
74
75
    /**
76
     * Gets the Job Titles of a certain user.
77
     *
78
     * @param $query
79
     * @param $userId
80
     * @return mixed
81
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
82
     */
83
    public function getTypeAheadResults($query, $userId)
84
    {
85
        $qb = $this->createQueryBuilder();
86
        $qb->hydrate(false)
87
           ->select('title', 'applyId')
88
           ->field('permissions.view')->equals($userId)
89
           ->field('title')->equals(new \MongoRegex('/' . $query . '/i'))
90
           ->sort('title')
91
           ->limit(5);
92
        
93
        $result = $qb->getQuery()->execute();
94
        
95
        return $result;
96
    }
97
98
    /**
99
     * Look for an drafted Document of a given user
100
     *
101
     * @param $user
102
     * @return \Jobs\Entity\Job|null
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
103
     */
104 View Code Duplication
    public function findDraft($user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        if ($user instanceof UserInterface) {
107
            $user = $user->getId();
108
        }
109
110
        $document = $this->findOneBy(
111
            array(
112
            'isDraft' => true,
113
            'user' => $user
114
            )
115
        );
116
117
        if (!empty($document)) {
118
            return $document;
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getUniqueReference()
128
    {
129
        return uniqid();
130
    }
131
132
    /**
133
     * Selects job postings of a certain organization
134
     *
135
     * @param int $organizationId
136
     * @return \Jobs\Entity\Job[]
137
     */
138
    public function findByOrganization($organizationId)
139
    {
140
        return $this->findBy([
141
            'organization' => new \MongoId($organizationId)
142
        ]);
143
    }
144
145
    /**
146
     * Selects all Organizations with Active Jobs
147
     *
148
     * @return mixed
149
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
150
     */
151
    public function findActiveOrganizations()
152
    {
153
        $qb = $this->createQueryBuilder();
154
        $qb->distinct('organization')
155
            ->hydrate(true)
156
           ->field('status.name')->notIn([ StatusInterface::EXPIRED, StatusInterface::INACTIVE ]);
157
        $q = $qb->getQuery();
158
        $r = $q->execute();
159
        $r = $r->toArray();
160
161
        $qb = $this->dm->createQueryBuilder('Organizations\Entity\Organization');
162
        $qb->field('_id')->in($r);
163
        $q = $qb->getQuery();
164
        $r = $q->execute();
165
166
        return $r;
167
    }
168
169
    /**
170
     * @return  Cursor
171
     * @throws  \Doctrine\ODM\MongoDB\MongoDBException
172
     */
173
    public function findActiveJob($hydrate = true)
174
    {
175
        $qb = $this->createQueryBuilder()
176
            ->hydrate($hydrate)
177
            ->refresh()
178
            ->field('status.name')->in([StatusInterface::ACTIVE])
179
            ->field('isDraft')->equals(false)
180
        ;
181
        $q  = $qb->getQuery();
182
        $r  = $q->execute();
183
184
        return $r;
185
    }
186
    
187
    /**
188
     * Get jobs for given user ID
189
     *
190
     * @param string $userId
191
     * @param int $limit
0 ignored issues
show
Documentation introduced by
Should the type for parameter $limit not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
192
     * @return Cursor
193
     * @since 0.27
194
     */
195 View Code Duplication
    public function getUserJobs($userId, $limit = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
    {
197
        $qb = $this->createQueryBuilder()
198
            ->field('user')->equals($userId)
199
            ->sort(['dateCreated.date' => -1]);
200
        
201
        if (isset($limit)) {
202
            $qb->limit($limit);
203
        }
204
    
205
        return $qb->getQuery()->execute();
206
    }
207
}
208