Completed
Push — develop ( 41c7f2...c75883 )
by
unknown
16:43 queued 08:14
created

AdminController::editAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 36
rs 8.5806
cc 4
eloc 25
nc 4
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\Listener\Events\JobEvent;
14
use Zend\Mvc\Controller\AbstractActionController;
15
use Zend\View\Model\JsonModel;
16
17
/**
18
 * ${CARET}
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @todo write test 
22
 */
23
class AdminController extends AbstractActionController
24
{
25
26
    public function indexAction()
27
    {
28
29
        $params = $this->paginationParams('Jobs_Admin', ['text', 'page' => 1, 'companyId', 'status' ]);
0 ignored issues
show
Bug introduced by
The method paginationParams() does not exist on Jobs\Controller\AdminController. Did you maybe mean params()?

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...
Unused Code introduced by
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
31
        $services = $this->getServiceLocator();
32
        $forms    = $services->get('forms');
33
        $form     = $forms->get('Jobs/AdminSearch');
34
        $paginator = $this->paginator('Jobs/Admin');
0 ignored issues
show
Documentation Bug introduced by
The method paginator does not exist on object<Jobs\Controller\AdminController>? 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...
35
36
        return [
37
            'by'   => $this->params()->fromQuery('by', 'all'),
38
            'jobs' => $paginator,
39
            'form' => $form,
40
        ];
41
42
43
    }
44
45
    public function editAction()
46
    {
47
        $services = $this->getServiceLocator();
48
        $repositories = $services->get('repositories');
49
        $jobs         = $repositories->get('Jobs');
50
        $job          = $jobs->find($this->params()->fromQuery('id'));
51
        $forms        = $services->get('forms');
52
        $form         = $forms->get('Jobs/AdminJobEdit');
53
        $request      = $this->getRequest();
54
55
        if ($request->isPost()) {
56
            $post = $this->params()->fromPost();
57
            $form->setData($post);
58
            $valid = $form->isValid();
59
            $errors = $form->getMessages();
60
61
            if ($valid) {
62
                $job->setDatePublishStart($post['datePublishStart']);
63
                if ($job->getStatus()->getName() != $post['status']) {
64
                    $oldStatus = $job->getStatus();
65
                    $job->changeStatus($post['status'], '[System] Status changed via Admin GUI.');
66
                    $events = $services->get('Jobs/Events');
67
                    $events->trigger(JobEvent::EVENT_STATUS_CHANGED, $this, [ 'job' => $job, 'status' => $oldStatus ]);
68
                }
69
            }
70
71
            return new JsonModel([
72
                'valid' => $valid,
73
                'errors' => $errors
74
                                 ]);
75
        }
76
77
        $form->bind($job);
78
79
        return [ 'form' => $form, 'job' => $job ];
80
    }
81
    
82
}