Completed
Push — develop ( 60c2a6...7c4b2f )
by Carsten
61:42 queued 47:32
created

IndexController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
7
 * @license       MIT
8
 */
9
10
/** ActionController of Jobs */
11
namespace Jobs\Controller;
12
13
use Zend\Mvc\Controller\AbstractActionController;
14
use Zend\Session\Container as Session;
15
use Zend\View\Model\JsonModel;
16
use Auth\Entity\User;
17
use Jobs\Repository;
18
use Jobs\Form\ListFilter;
19
use Zend\View\Model\ViewModel;
20
21
/**
22
 * Handles the job listing for recruiters.
23
 *
24
 * @author Mathias Gelhausen <[email protected]>
25
 * @author Mathias Weitz <[email protected]>
26
 * @author Carsten Bleek <[email protected]>
27
 *
28
 * @method \Auth\Controller\Plugin\Auth auth()
29
 * @method \Acl\Controller\Plugin\Acl acl()
30
 * @method \Core\Controller\Plugin\CreatePaginator paginator(string $repositoryName, array $defaultParams = array(), bool $usePostParams = false)
31
 */
32
class IndexController extends AbstractActionController
33
{
34
35
    /**
36
     * @var Repository\Job $jobRepository
37
     */
38
    private $jobRepository;
39
40
    /**
41
     * Formular for searching job postings
42
     *
43
     * @var ListFilter $searchForm
44
     */
45
    private $searchForm;
46
47
    /**
48
     * Construct the index controller
49
     *
50
     * @param Repository\Job $jobRepository
51
     * @param ListFilter $searchForm
52
     */
53
    public function __construct(Repository\Job $jobRepository, ListFilter $searchForm)
54
    {
55
        $this->jobRepository = $jobRepository;
56
        $this->searchForm = $searchForm;
57
    }
58
59
    /**
60
     * attaches further Listeners for generating / processing the output
61
     *
62
     * @return $this
63
     */
64 View Code Duplication
    public function attachDefaultListeners()
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...
65
    {
66
        parent::attachDefaultListeners();
67
68
        $serviceLocator  = $this->getServiceLocator();
69
        $defaultServices = $serviceLocator->get('DefaultListeners');
70
        $events          = $this->getEventManager();
71
        $events->attach($defaultServices);
0 ignored issues
show
Documentation introduced by
$defaultServices is of type object|array, but the function expects a string.

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...
72
73
        return $this;
74
    }
75
76
    /**
77
     * List jobs.
78
     *
79
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be ViewModel?

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...
80
     */
81
    public function indexAction()
82
    {
83
        return $this->listJobs();
84
    }
85
86
    /**
87
     * @param bool $showPendingJobs
0 ignored issues
show
Bug introduced by
There is no parameter named $showPendingJobs. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
88
     *
89
     * @return ViewModel
90
     */
91 View Code Duplication
    protected function listJobs()
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...
92
    {
93
        /* @var $request \Zend\Http\Request */
94
        $request     = $this->getRequest();
95
        $params      = $request->getQuery();
96
        $jsonFormat  = 'json' == $params->get('format');
97
        $isRecruiter = $this->acl()->isRole(User::ROLE_RECRUITER);
98
99
        if (!$jsonFormat && !$request->isXmlHttpRequest()) {
100
            $session       = new Session('Jobs\Index');
101
            $sessionKey    = $this->auth()->isLoggedIn() ? 'userParams' : 'guestParams';
102
            $sessionParams = $session[$sessionKey];
103
104
            if ($sessionParams) {
105
                foreach ($sessionParams as $key => $value) {
106
                    $params->set($key, $params->get($key, $value));
107
                }
108
            } elseif ($isRecruiter) {
109
                $params->set('by', 'me');
110
            }
111
112
            $session[$sessionKey] = $params->toArray();
113
114
            $this->searchForm->bind($params);
115
116
        }
117
118
        if (!isset($params['sort'])) {
119
            $params['sort'] = '-date';
120
        }
121
122
        $paginator = $this->paginatorservice('Jobs/Job', $params);
0 ignored issues
show
Documentation Bug introduced by
The method paginatorservice does not exist on object<Jobs\Controller\IndexController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
123
124
        $return = array(
125
            'by'   => $params->get('by', 'all'),
126
            'jobs' => $paginator,
127
            'showPendingJobs' => false,
128
        );
129
        if (isset($this->searchForm)) {
130
            $return['filterForm'] = $this->searchForm;
131
        }
132
133
        $model = new ViewModel();
134
        $model->setVariables($return);
135
        $model->setTemplate('jobs/index/index');
136
        return $model;
137
    }
138
139
    /**
140
     * Handles the dashboard widget for the jobs module.
141
     *
142
     * @return array
143
     */
144
    public function dashboardAction()
145
    {
146
        /* @var $request \Zend\Http\Request */
147
        $services    = $this->getServiceLocator();
148
        $request     = $this->getRequest();
149
        $params      = $request->getQuery();
150
        $isRecruiter = $this->acl()->isRole(User::ROLE_RECRUITER);
151
152
        if ($isRecruiter) {
153
            $params->set('by', 'me');
154
        }
155
156
        $myJobs    = $services->get('repositories')->get('Jobs/Job');
157
        $paginator = $this->paginator('Jobs/Job');
158
159
        return array(
160
            'script' => 'jobs/index/dashboard',
161
            'type'   => $this->params('type'),
162
            'myJobs' => $myJobs,
163
            'jobs'   => $paginator
164
        );
165
    }
166
167
    /**
168
     * Action hook for Job search bar in list filter.
169
     *
170
     * @return JsonModel
171
     */
172
    public function typeaheadAction()
173
    {
174
        /* @var $repository \Jobs\Repository\Job */
175
        $query      = $this->params()->fromQuery('q', '*');
176
        $repository = $this->getServiceLocator()
177
                           ->get('repositories')
178
                           ->get('Jobs/Job');
179
180
        $return = array();
181
        foreach ($repository->getTypeaheadResults($query, $this->auth('id')) as $id => $item) {
0 ignored issues
show
Unused Code introduced by
The call to IndexController::auth() has too many arguments starting with 'id'.

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...
182
            $return[] = array(
183
                'id'      => $id,
184
                'title'   => $item['title'],
185
                'applyId' => $item['applyId'],
186
            );
187
        }
188
189
        return new JsonModel($return);
190
    }
191
}
192