Completed
Push — develop ( 62651e...ddc457 )
by
unknown
09:17
created

InitializeJob::get()   D

Complexity

Conditions 13
Paths 160

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 4.8178
c 0
b 0
f 0
cc 13
eloc 29
nc 160
nop 2

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 Zend\Mvc\Controller\Plugin\AbstractPlugin;
15
use Core\Repository\RepositoryService;
16
use Auth\AuthenticationService;
17
use Zend\Mvc\Controller\Plugin\Params;
18
use Acl\Controller\Plugin\Acl;
19
use Core\Entity\Exception\NotFoundException;
20
21
/**
22
 * Class InitializeJob
23
 *
24
 * @package Jobs\Controller\Plugin
25
 */
26
class InitializeJob extends AbstractPlugin
27
{
28
29
    /**
30
     * @var RepositoryService
31
     */
32
    protected $repositoryService;
33
34
    /**
35
     * @var AuthenticationService
36
     */
37
    protected $auth;
38
39
    /**
40
     * @var \Acl\Controller\Plugin\Acl
41
     */
42
    protected $acl;
43
44
    public function __construct(RepositoryService $repositoryService, AuthenticationService $auth, Acl $acl)
45
    {
46
        $this->repositoryService=$repositoryService;
47
        $this->auth=$auth;
48
        $this->acl=$acl;
49
    }
50
51
    public function __invoke()
52
    {
53
        return $this;
54
    }
55
56
    /**
57
     * @param Params $params
58
     * @param bool   $allowDraft
59
     *
60
     * @return \Jobs\Entity\Job|object
61
     * @throws \Doctrine\ODM\MongoDB\LockException
62
     * @throws NotFoundException
63
     */
64
    public function get(Params $params, $allowDraft = false)
65
    {
66
        /* @var \Jobs\Repository\Job $jobRepository */
67
        $jobRepository  = $this->repositoryService->get('Jobs/Job');
68
        $idFromRoute   = $params('id', 0);
69
        $idFromQuery   = $params->fromQuery('id', 0);
70
        $idFromSubForm = $params->fromPost('job', 0);
71
72
        $id = empty($idFromRoute)? (empty($idFromQuery)?$idFromSubForm:$idFromQuery) : $idFromRoute;
73
        $snapshotId = $params->fromPost('snapshot') ?: ($params->fromQuery('snapshot') ?: null);
74
75
        if (empty($id) && empty($snapshotId) && $allowDraft) {
76
            $this->acl->__invoke('Jobs/Manage', 'new');
77
            $user = $this->auth->getUser();
78
            /** @var \Jobs\Entity\Job $job */
79
            $job = $jobRepository->findDraft($user);
80
            if (empty($job)) {
81
                $job = $jobRepository->create();
82
                $job->setIsDraft(true);
83
                $job->setUser($user);
84
                $this->repositoryService->store($job);
85
            }
86
            return $job;
87
        }
88
89
        if ($snapshotId) {
90
            $snapshotRepo = $this->repositoryService->get('Jobs/JobSnapshot');
91
            $job = $snapshotRepo->find($snapshotId);
92
93
        } else {
94
            $job = $jobRepository->find($id);
95
            if (!$job->isDraft()) {
96
                $snapshotRepo = $this->repositoryService->get('Jobs/JobSnapshot');
97
                $snapshot = $snapshotRepo->findLatest($job->getId(), /*isDraft*/ true);
98
99
                $job = $snapshot ?: $snapshotRepo->create($job, true);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Doctrine\ODM\MongoDB\DocumentRepository. Did you maybe mean createQueryBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
100
            }
101
        }
102
103
        if (!$job) {
104
            throw new NotFoundException($id);
105
        }
106
107
        return $job;
108
    }
109
}
110