Completed
Pull Request — develop (#246)
by ANTHONIUS
07:21
created

Job::findActiveJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license   MIT
7
 */
8
9
namespace Jobs\Repository;
10
11
use Auth\Entity\UserInterface;
12
use Core\Repository\AbstractRepository;
13
use Core\Repository\DoctrineMongoODM\PaginatorAdapter;
14
use Doctrine\ODM\MongoDB\Cursor;
15
use Jobs\Entity\StatusInterface;
16
17
/**
18
 * Class Job
19
 *
20
 */
21
class Job extends AbstractRepository
22
{
23
    /**
24
     * Gets a pagination cursor to the jobs collection
25
     *
26
     * @param $params
27
     * @return mixed
28
     */
29 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...
30
    {
31
        $filter = $this->getService('filterManager')->get('Jobs/PaginationQuery');
32
        /* @var $filter \Core\Repository\Filter\AbstractPaginationQuery  */
33
        $qb = $filter->filter($params, $this->createQueryBuilder());
34
        return $qb->getQuery()->execute();
35
    }
36
37
    /**
38
     * Checks, if a job posting with a certain applyId (external job id) exists
39
     *
40
     * @param $applyId
41
     * @return bool
42
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
43
     */
44 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...
45
    {
46
        $qb = $this->createQueryBuilder();
47
        $qb->hydrate(false)
48
           ->select('applyId')
49
           ->field('applyId')->equals($applyId);
50
           
51
        $result = $qb->getQuery()->execute();
52
        $count = $result->count();
53
        return (bool) $count;
54
    }
55
56
    /**
57
     * @param $resourceId
58
     * @return array
59
     */
60
    public function findByAssignedPermissionsResourceId($resourceId)
61
    {
62
        return $this->findBy(
63
            array(
64
            'permissions.assigned.' . $resourceId => array(
65
                '$exists' => true
66
            )
67
            )
68
        );
69
    }
70
71
    /**
72
     * Gets the Job Titles of a certain user.
73
     *
74
     * @param $query
75
     * @param $userId
76
     * @return mixed
77
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
78
     */
79
    public function getTypeAheadResults($query, $userId)
80
    {
81
        $qb = $this->createQueryBuilder();
82
        $qb->hydrate(false)
83
           ->select('title', 'applyId')
84
           ->field('permissions.view')->equals($userId)
85
           ->field('title')->equals(new \MongoRegex('/' . $query . '/i'))
86
           ->sort('title')
87
           ->limit(5);
88
        
89
        $result = $qb->getQuery()->execute();
90
        
91
        return $result;
92
    }
93
94
    /**
95
     * Look for an drafted Document of a given user
96
     *
97
     * @param $user
98
     * @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...
99
     */
100 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...
101
    {
102
        if ($user instanceof UserInterface) {
103
            $user = $user->getId();
104
        }
105
106
        $document = $this->findOneBy(
107
            array(
108
            'isDraft' => true,
109
            'user' => $user
110
            )
111
        );
112
113
        if (!empty($document)) {
114
            return $document;
115
        }
116
117
        return null;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getUniqueReference()
124
    {
125
        return uniqid();
126
    }
127
128
    /**
129
     * Selects job postings of a certain organization
130
     *
131
     * @param int $organizationId
132
     * @return \Jobs\Entity\Job[]
133
     */
134
    public function findByOrganization($organizationId)
135
    {
136
        return $this->findBy([
137
            'organization' => new \MongoId($organizationId)
138
        ]);
139
    }
140
141
    /**
142
     * Selects all Organizations with Active Jobs
143
     *
144
     * @return mixed
145
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
146
     */
147
    public function findActiveOrganizations()
148
    {
149
        $qb = $this->createQueryBuilder();
150
        $qb->distinct('organization')
151
            ->hydrate(true)
152
           ->field('status.name')->notIn([ StatusInterface::EXPIRED, StatusInterface::INACTIVE ]);
153
        $q = $qb->getQuery();
154
        $r = $q->execute();
155
        $r = $r->toArray();
156
157
        $qb = $this->dm->createQueryBuilder('Organizations\Entity\Organization');
158
        $qb->field('_id')->in($r);
159
        $q = $qb->getQuery();
160
        $r = $q->execute();
161
162
        return $r;
163
    }
164
165
    /**
166
     * @return  Cursor
167
     * @throws  \Doctrine\ODM\MongoDB\MongoDBException
168
     */
169
    public function findActiveJob($hydrate = true)
170
    {
171
        $qb = $this->createQueryBuilder()
172
            ->hydrate($hydrate)
173
            ->refresh()
174
            ->field('status.name')->in([StatusInterface::ACTIVE])
175
            ->field('isDraft')->equals(false)
176
        ;
177
        $q  = $qb->getQuery();
178
        $r  = $q->execute();
179
180
        return $r;
181
    }
182
}
183