Completed
Push — develop ( eb5f8c...2537a4 )
by
unknown
13:09
created

IndexController::listJobs()   C

Complexity

Conditions 9
Paths 28

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 47
rs 5.2941
cc 9
eloc 29
nc 28
nop 0
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
    public function attachDefaultListeners()
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 job postings
78
     *
79
     * @return ViewModel
80
     */
81
    public function indexAction()
82
    {
83
        /* @var $request \Zend\Http\Request */
84
        $request     = $this->getRequest();
85
        $params      = $request->getQuery();
86
        $jsonFormat  = 'json' == $params->get('format');
87
        $isRecruiter = $this->acl()->isRole(User::ROLE_RECRUITER);
88
89
        if (!$jsonFormat && !$request->isXmlHttpRequest()) {
90
            $session       = new Session('Jobs\Index');
91
            $sessionKey    = $this->auth()->isLoggedIn() ? 'userParams' : 'guestParams';
92
            $sessionParams = $session[$sessionKey];
93
94
            if ($sessionParams) {
95
                foreach ($sessionParams as $key => $value) {
96
                    $params->set($key, $params->get($key, $value));
97
                }
98
            } elseif ($isRecruiter) {
99
                $params->set('by', 'me');
100
            }
101
102
            $session[$sessionKey] = $params->toArray();
103
104
            $this->searchForm->bind($params);
105
106
        }
107
108
        if (!isset($params['sort'])) {
109
            $params['sort'] = '-date';
110
        }
111
112
        $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...
113
114
        $return = array(
115
            'by'   => $params->get('by', 'all'),
116
            'jobs' => $paginator,
117
            'showPendingJobs' => false,
118
        );
119
        if (isset($this->searchForm)) {
120
            $return['filterForm'] = $this->searchForm;
121
        }
122
123
        $model = new ViewModel();
124
        $model->setVariables($return);
125
        $model->setTemplate('jobs/index/index');
126
        return $model;
127
    }
128
129
    /**
130
     * Handles the dashboard widget for the jobs module.
131
     *
132
     * @return array
133
     */
134
    public function dashboardAction()
135
    {
136
        /* @var $request \Zend\Http\Request */
137
        $services    = $this->getServiceLocator();
138
        $request     = $this->getRequest();
139
        $params      = $request->getQuery();
140
        $isRecruiter = $this->acl()->isRole(User::ROLE_RECRUITER);
141
142
        if ($isRecruiter) {
143
            $params->set('by', 'me');
144
        }
145
146
        $myJobs    = $services->get('repositories')->get('Jobs/Job');
147
        $paginator = $this->paginator('Jobs/Job');
148
149
        return array(
150
            'script' => 'jobs/index/dashboard',
151
            'type'   => $this->params('type'),
152
            'myJobs' => $myJobs,
153
            'jobs'   => $paginator
154
        );
155
    }
156
157
    /**
158
     * Action hook for Job search bar in list filter.
159
     *
160
     * @return JsonModel
161
     */
162
    public function typeaheadAction()
163
    {
164
        /* @var $repository \Jobs\Repository\Job */
165
        $query      = $this->params()->fromQuery('q', '*');
166
        $repository = $this->getServiceLocator()
167
                           ->get('repositories')
168
                           ->get('Jobs/Job');
169
170
        $return = array();
171
        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...
172
            $return[] = array(
173
                'id'      => $id,
174
                'title'   => $item['title'],
175
                'applyId' => $item['applyId'],
176
            );
177
        }
178
179
        return new JsonModel($return);
180
    }
181
}