Completed
Pull Request — develop (#274)
by
unknown
39:07
created

Application   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 217
Duplicated Lines 20.74 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
c 1
b 0
f 0
lcom 1
cbo 7
dl 45
loc 217
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A findBy() 9 9 3
A findOneBy() 9 9 3
A createQueryBuilder() 8 8 2
A getPaginatorCursor() 0 6 1
A getPaginationQueryBuilder() 7 7 1
A getPaginationList() 0 11 1
A loadApplicationsForJob() 0 7 1
A loadUnreadApplicationsForJob() 0 8 1
A findComment() 0 15 4
A findProfile() 0 10 3
A getStates() 0 7 1
B findDraft() 0 25 4
A getUserApplications() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author Carsten Bleek <[email protected]>
9
 * @author Mathias Gelhausen <[email protected]>
10
 * @author Mathias Weitz <weitz@xenon>
11
 * @author Miroslav Fedeleš <[email protected]>
12
 */
13
namespace Applications\Repository;
14
15
use Core\Repository\AbstractRepository;
16
use Applications\Entity\Application as ApplicationEntity;
17
use Applications\Entity\CommentInterface;
18
use Zend\Stdlib\ArrayUtils;
19
use Auth\Entity\UserInterface;
20
21
/**
22
 * class for accessing applications
23
 *
24
 * @todo find better mechanism for loading drafts or applications or both states.
25
 *
26
 * @package Applications
27
 */
28
class Application extends AbstractRepository
29
{
30
   
31
    /**
32
     * {@inheritDoc}
33
     */
34 View Code Duplication
    public function findBy(array $criteria, array $sort = null, $limit = null, $skip = 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...
35
    {
36
        if (!array_key_exists('isDraft', $criteria)) {
37
            $criteria['isDraft'] = false;
38
        } elseif (null === $criteria['isDraft']) {
39
            unset($criteria['isDraft']);
40
        }
41
        return parent::findBy($criteria, $sort, $limit, $skip);
42
    }
43
    
44
    /**
45
     * {@inheritDoc}
46
     */
47 View Code Duplication
    public function findOneBy(array $criteria)
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...
48
    {
49
        if (!array_key_exists('isDraft', $criteria)) {
50
            $criteria['isDraft'] = false;
51
        } elseif (null === $criteria['isDraft']) {
52
            unset($criteria['isDraft']);
53
        }
54
        return parent::findOneBy($criteria);
55
    }
56
    
57
    /**
58
     * {@inheritDoc}
59
     */
60 View Code Duplication
    public function createQueryBuilder($findDrafts = false)
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...
61
    {
62
        $qb = parent::createQueryBuilder();
63
        if (null !== $findDrafts) {
64
            $qb->field('isDraft')->equals($findDrafts);
65
        }
66
        return $qb;
67
    }
68
    
69
    /**
70
     * Gets a pointer to an application
71
     *
72
     * @param array $params
73
     */
74
    public function getPaginatorCursor($params)
75
    {
76
        return $this->getPaginationQueryBuilder($params)
77
                    ->getQuery()
78
                    ->execute();
79
    }
80
    
81
    /**
82
     * Gets a query builder to search for applications
83
     *
84
     * @param array $params
85
     * @return unknown
86
     */
87 View Code Duplication
    protected function getPaginationQueryBuilder($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...
88
    {
89
        $filter = $this->getService('filterManager')->get('PaginationQuery/Applications');
90
        $qb = $filter->filter($params, $this->createQueryBuilder());
91
        
92
        return $qb;
93
    }
94
    
95
    /**
96
     * Gets a result list of applications
97
     *
98
     * @param array $params
99
     * @return \Applications\Repository\PaginationList
100
     */
101
    public function getPaginationList($params)
102
    {
103
        $qb = $this->getPaginationQueryBuilder($params);
104
        $cursor = $qb->hydrate(false)
105
                     ->select('_id')
106
                     ->getQuery()
107
                     ->execute();
108
        
109
        $list = new PaginationList(array_keys(ArrayUtils::iteratorToArray($cursor)));
110
        return $list;
111
    }
112
113
    /**
114
     * @param $job
115
     *
116
     * @return mixed
117
     */
118
    public function loadApplicationsForJob($job)
119
    {
120
        return $this->createQueryBuilder()
121
                    ->field("job")->equals(new \MongoId($job->id))
122
                    ->getQuery()
123
                    ->execute();
124
    }
125
    
126
    /**
127
     * Get unread applications
128
     *
129
     * @param \Jobs\Entity\JobInterface $job
130
     * @return array|bool|\Doctrine\MongoDB\ArrayIterator|\Doctrine\MongoDB\Cursor|\Doctrine\MongoDB\EagerCursor|mixed|null
131
     */
132
    public function loadUnreadApplicationsForJob($job)
133
    {
134
        $auth=$this->getService('AuthenticationService');
135
        $qb=$this->createQueryBuilder()
136
                  ->field("readBy")->notIn(array($auth->getUser()->id))
137
                  ->field("job")->equals(new \MongoId($job->id));
0 ignored issues
show
Bug introduced by
Accessing id on the interface Jobs\Entity\JobInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
138
        return $qb->getQuery()->execute();
139
    }
140
141
    /**
142
     * Get comments of an applications
143
     *
144
     * @param $commentOrId
145
     * @internal param \Application\Entity\Comment $comment | Id
146
     * @return \Applications\Entity\Comment|NULL
147
     */
148
    public function findComment($commentOrId)
149
    {
150
        if ($commentOrId instanceof CommentInterface) {
151
            $commentOrId = $commentOrId->getId();
152
        }
153
        
154
        $application = $this->findOneBy(array('comments.id' => $commentOrId));
155
        /* @var \Applications\Entity\Comment $comment */
156
        foreach ($application->getComments() as $comment) {
157
            if ($comment->getId() == $commentOrId) {
158
                return $comment;
159
            }
160
        }
161
        return null;
162
    }
163
    
164
    /**
165
     * Gets social profiles of an application
166
     *
167
     * @param String $profileId
168
     * @return $profile|NULL
0 ignored issues
show
Documentation introduced by
The doc-type $profile|NULL could not be parsed: Unknown type name "$profile" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
169
     */
170
    public function findProfile($profileId)
171
    {
172
        $application = $this->findOneBy(array('isDraft' => null, 'profiles._id' => new \MongoId($profileId)));
173
        foreach ($application->getProfiles() as $profile) {
174
            if ($profile->getId() == $profileId) {
175
                return $profile;
176
            }
177
        }
178
        return null;
179
    }
180
181
    /**
182
     * @return mixed
183
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
184
     */
185
    public function getStates()
186
    {
187
        $qb = $this->createQueryBuilder();
188
        $qb->hydrate(false)->distinct('status.name');
189
        $result = $qb->getQuery()->execute();
190
        return $result;
191
    }
192
193
    /**
194
     * @param $user UserInterface
195
     * @param $applyId
196
     * @return ApplicationEntity|null
197
     */
198
    public function findDraft($user, $applyId)
199
    {
200
        if ($user instanceof UserInterface) {
201
            $user = $user->getId();
202
        }
203
204
        $documents = $this->findBy(
205
            [
206
                'isDraft' => true,
207
                '$or'     => [
208
                    ['user' => $user],
209
                    ['permissions.change' => $user]
210
                ]
211
            ]
212
        );
213
214
        /* @var $document ApplicationEntity */
215
        foreach ($documents as $document) {
216
            if ($applyId == $document->getJob()->getApplyId()) {
217
                return $document;
218
            }
219
        }
220
        
221
        return null;
222
    }
223
224
    /**
225
     * Get applications for given user ID
226
     *
227
     * @param string $userId
228
     * @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...
229
     * @return Cursor
230
     * @since 0.27
231
     */
232 View Code Duplication
    public function getUserApplications($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...
233
    {
234
        $qb = $this->createQueryBuilder(null)
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
235
            ->field('user')->equals($userId)
236
            ->sort(['date' => -1]);
237
    
238
        if (isset($limit)) {
239
            $qb->limit($limit);
240
        }
241
    
242
        return $qb->getQuery()->execute();
243
    }
244
}
245