Completed
Push — develop ( 959dbb...96e586 )
by
unknown
07:26
created

IndexController::typeaheadAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 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()
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->serviceLocator;
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 job postings
78
     *
79
     * @return ViewModel
80
     */
81
    public function indexAction()
82
    {
83
        /* @var $request \Zend\Http\Request */
84
        $request     = $this->getRequest();
85
        $queryParams = $request->getQuery();
86
        $params      = $queryParams->get('params', []);
87
        $jsonFormat  = 'json' == $queryParams->get('format');
88
        $isRecruiter = $this->acl()->isRole(User::ROLE_RECRUITER);
89
90
        if (!$jsonFormat && !$request->isXmlHttpRequest()) {
91
            $session       = new Session('Jobs\Index');
92
            $sessionKey    = $this->auth()->isLoggedIn() ? 'userParams' : 'guestParams';
93
            $sessionParams = $session[$sessionKey];
94
95
            if ($sessionParams) {
96
                $params = array_merge($sessionParams, $params);
97
            } elseif ($isRecruiter) {
98
                $params['by'] = 'me';
99
            }
100
101
            $session[$sessionKey] = $params;
102
            $queryParams->set('params', $params);
103
104
            $this->searchForm->bind($queryParams);
105
        }
106
107
        if (!isset($params['sort'])) {
108
            $params['sort'] = '-date';
109
        }
110
111
        $paginator = $this->paginator('Jobs/Job', $params);
0 ignored issues
show
Unused Code introduced by
The call to IndexController::paginator() has too many arguments starting with 'Jobs/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...
112
113
        $return = [
114
            'by'   => $queryParams->get('by', 'all'),
115
            'jobs' => $paginator,
116
        ];
117
        if (isset($this->searchForm)) {
118
            $return['filterForm'] = $this->searchForm;
119
        }
120
121
        $model = new ViewModel();
122
        $model->setVariables($return);
123
        $model->setTemplate('jobs/index/index');
124
        return $model;
125
    }
126
127
    /**
128
     * Handles the dashboard widget for the jobs module.
129
     *
130
     * @return array
131
     */
132
    public function dashboardAction()
133
    {
134
        /* @var $request \Zend\Http\Request */
135
        $request     = $this->getRequest();
136
        $params      = $request->getQuery();
137
        $isRecruiter = $this->acl()->isRole(User::ROLE_RECRUITER);
138
139
        if ($isRecruiter) {
140
            $params->set('by', 'me');
141
        }
142
143
        $paginator = $this->paginator('Jobs/Job');
0 ignored issues
show
Unused Code introduced by
The call to IndexController::paginator() has too many arguments starting with 'Jobs/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...
144
145
        return [
146
            'script' => 'jobs/index/dashboard',
147
            'type'   => $this->params('type'),
148
            'myJobs' => $this->jobRepository,
149
            'jobs'   => $paginator
150
        ];
151
    }
152
}
153