Completed
Push — develop ( b559a4...dfd31d )
by
unknown
10:04
created

AssignUserController::common()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Jobs\Controller;
12
13
use Jobs\Repository\Job as JobsRepository;
14
use Zend\Mvc\Controller\AbstractActionController;
15
use Zend\View\Model\JsonModel;
16
use Zend\View\Model\ViewModel;
17
18
/**
19
 * ${CARET}
20
 *
21
 * @method \Acl\Controller\Plugin\Acl acl()
22
 *
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @todo write test
25
 */
26
class AssignUserController extends AbstractActionController
27
{
28
29
    protected $repository;
30
31
    /**
32
     * The job entity
33
     *
34
     * @var \Jobs\Entity\Job
35
     */
36
    protected $job;
37
38
    public function __construct(JobsRepository $repository)
39
    {
40
        $this->repository = $repository;
41
    }
42
43
44
    public function indexAction()
45
    {
46
        /* @var $request \Zend\Http\Request */
47
        $request = $this->getRequest();
48
49
        if (!$request->isXmlHttpRequest()) {
50
            throw new \RuntimeException('This action must be called via ajax request ONLY!');
51
        }
52
53
        $this->common();
54
        return $request->isPost()
55
               ? $this->save()
56
               : $this->render();
57
    }
58
59
    protected function common()
60
    {
61
        $id = $this->params()->fromQuery('id');
62
        $job = $this->repository->find($id);
63
64
        if (!$job) {
65
            throw new \RuntimeException('No job found with id "' . $id . '"');
66
        }
67
68
        $this->acl($job, 'edit');
0 ignored issues
show
Unused Code introduced by
The call to AssignUserController::acl() has too many arguments starting with $job.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
        $this->job = $job;
70
    }
71
72
    protected function render()
73
    {
74
        $organization = $this->job->getOrganization();
75
        if ($organization->isHiringOrganization()) {
76
            $organization = $organization->getParent();
77
        }
78
79
        $users = array($organization->getUser());
80
        foreach ($organization->getEmployees() as $emp) {
81
            /* @var $emp \Organizations\Entity\Employee */
82
            $users[] = $emp->getUser();
83
        }
84
85
        $model = new ViewModel();
86
        $model->setVariables(
87
            array(
88
            'currentUser' => $this->job->getUser(),
89
            'users' => $users,
90
            'organization' => $organization,
91
            'job' => $this->job,
92
            )
93
        );
94
        $model->setTemplate('jobs/assign-user');
95
        return $model;
96
    }
97
98
    protected function save()
99
    {
100
        $userId = $this->params()->fromPost('userId');
101
102
        $org = $this->job->getOrganization();
103
        if ($org->isHiringOrganization()) {
104
            $org = $org->getParent();
105
        }
106
107
        /*
108
         * Maybe we should inject the user repository also, to prevent this
109
         * rather expensive loop. On the other hand... how often will someone change the job user?
110
         */
111
        if ($org->getUser()->getId() == $userId) {
112
            $this->job->setUser($org->getUser());
113
        } else {
114
            /* @var \Organizations\Entity\Employee  $emp */
115
            foreach ($org->getEmployees() as $emp) {
116
                $user = $emp->getUser();
117
                if ($user->getId() == $userId) {
118
                    $this->job->setUser($user);
119
                }
120
            }
121
        }
122
123
        $model = new JsonModel(array('success' => true));
124
125
        return $model;
126
    }
127
}
128