InitializeJob::get()   C
last analyzed

Complexity

Conditions 17
Paths 32

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 44
c 0
b 0
f 0
ccs 0
cts 35
cp 0
rs 5.2166
cc 17
nc 32
nop 3
crap 306

How to fix   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
 * @filesource
6
 * @copyright (c) 2013-2016 Cross Solution (http://cross-solution.de)
7
 * @author Carsten Bleek <[email protected]>
8
 * @author Miroslav Fedeleš <[email protected]>
9
 * @license   MIT
10
 */
11
12
namespace Jobs\Controller\Plugin;
13
14
use Jobs\Entity\StatusInterface;
15
use Laminas\Mvc\Controller\Plugin\AbstractPlugin;
16
use Core\Repository\RepositoryService;
17
use Auth\AuthenticationService;
18
use Laminas\Mvc\Controller\Plugin\Params;
19
use Acl\Controller\Plugin\Acl;
20
use Core\Entity\Exception\NotFoundException;
21
22
/**
23
 * Class InitializeJob
24
 *
25
 * @package Jobs\Controller\Plugin
26
 */
27
class InitializeJob extends AbstractPlugin
28
{
29
30
    /**
31
     * @var RepositoryService
32
     */
33
    protected $repositoryService;
34
35
    /**
36
     * @var AuthenticationService
37
     */
38
    protected $auth;
39
40
    /**
41
     * @var \Acl\Controller\Plugin\Acl
42
     */
43
    protected $acl;
44
45
    public function __construct(RepositoryService $repositoryService, AuthenticationService $auth, Acl $acl)
46
    {
47
        $this->repositoryService=$repositoryService;
48
        $this->auth=$auth;
49
        $this->acl=$acl;
50
    }
51
52
    public function __invoke()
53
    {
54
        return $this;
55
    }
56
57
    /**
58
     * @param Params $params
59
     * @param bool   $allowDraft
60
     *
61
     * @return \Jobs\Entity\Job|object
62
     * @throws \Doctrine\ODM\MongoDB\LockException
63
     * @throws NotFoundException
64
     */
65
    public function get(Params $params, $allowDraft = false, $getSnapshot = false)
66
    {
67
        /* @var \Jobs\Repository\Job $jobRepository */
68
        $jobRepository  = $this->repositoryService->get('Jobs/Job');
69
        $idFromRoute   = $params('id', 0);
70
        $idFromQuery   = $params->fromQuery('id', 0);
71
        $idFromSubForm = $params->fromPost('job', 0);
72
73
        $id = empty($idFromRoute)? (empty($idFromQuery)?$idFromSubForm:$idFromQuery) : $idFromRoute;
74
        $snapshotId = $params->fromPost('snapshot') ?: ($params->fromQuery('snapshot') ?: null);
75
76
        if (empty($id) && empty($snapshotId) && $allowDraft) {
77
            $this->acl->__invoke('Jobs/Manage', 'new');
78
            $user = $this->auth->getUser();
79
            /** @var \Jobs\Entity\Job $job */
80
            $job = $jobRepository->findDraft($user);
81
            if (empty($job)) {
82
                $job = $jobRepository->create();
83
                $job->setIsDraft(true);
84
                $job->setUser($user);
85
                $this->repositoryService->store($job);
86
            }
87
            return $job;
88
        }
89
90
        if ($snapshotId) {
91
            $snapshotRepo = $this->repositoryService->get('Jobs/JobSnapshot');
92
            $job = $snapshotRepo->find($snapshotId);
93
        } else {
94
            /* @var \Jobs\Entity\Job $job */
95
            $job = $jobRepository->find($id);
96
            if ($job && $getSnapshot && !$job->isDraft() && $job->getStatus()->getName() != \Jobs\Entity\StatusInterface::CREATED) {
0 ignored issues
show
Deprecated Code introduced by
The function Jobs\Entity\Status::getName() has been deprecated: since 0,29, use __toString() ( Ignorable by Annotation )

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

96
            if ($job && $getSnapshot && !$job->isDraft() && /** @scrutinizer ignore-deprecated */ $job->getStatus()->getName() != \Jobs\Entity\StatusInterface::CREATED) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
97
                $snapshotRepo = $this->repositoryService->get('Jobs/JobSnapshot');
98
                $snapshot = $snapshotRepo->findLatest($job->getId(), /*isDraft*/ true);
0 ignored issues
show
Bug introduced by
The method findLatest() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Core\Repository\SnapshotRepository. ( Ignorable by Annotation )

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

98
                /** @scrutinizer ignore-call */ 
99
                $snapshot = $snapshotRepo->findLatest($job->getId(), /*isDraft*/ true);
Loading history...
99
100
                $job = $snapshot ?: $snapshotRepo->create($job, true);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Core\Repository\SnapshotRepository or Core\Repository\AbstractRepository. ( Ignorable by Annotation )

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

100
                $job = $snapshot ?: $snapshotRepo->/** @scrutinizer ignore-call */ create($job, true);
Loading history...
101
            }
102
        }
103
104
        if (!$job || $job->isDeleted()) {
105
            throw new NotFoundException($id);
106
        }
107
108
        return $job;
109
    }
110
}
111