UpdateController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 1
eloc 11
nc 1
nop 5
1
<?php
2
3
namespace Sebaks\Crud\Controller;
4
5
use Zend\Mvc\MvcEvent;
6
7
use Zend\Mvc\Controller\AbstractActionController;
8
use T4webDomainInterface\Service\UpdaterInterface;
9
use Sebaks\Crud\View\Model\UpdateViewModelInterface;
10
11
class UpdateController extends AbstractActionController
12
{
13
    /**
14
     * @var int
15
     */
16
    private $id;
17
18
    /**
19
     * @var array
20
     */
21
    private $data;
22
23
    /**
24
     * @var UpdaterInterface
25
     */
26
    private $updater;
27
28
    /**
29
     * @var UpdateViewModelInterface
30
     */
31
    private $viewModel;
32
33
    /**
34
     * @var string
35
     */
36
    private $redirectTo;
37
38
    /**
39
     * @param $id
40
     * @param array $data
41
     * @param UpdaterInterface $updater
42
     * @param UpdateViewModelInterface $viewModel
43
     * @param null $redirectTo
44
     */
45
    public function __construct(
46
        $id,
47
        array $data,
48
        UpdaterInterface $updater,
49
        UpdateViewModelInterface $viewModel,
50
        $redirectTo = null
51
    )
52
    {
53
        $this->id = $id;
54
        $this->data = $data;
55
        $this->updater = $updater;
56
        $this->viewModel = $viewModel;
57
        $this->redirectTo = $redirectTo;
58
    }
59
60
    /**
61
     * Execute the request
62
     *
63
     * @param  MvcEvent $e
64
     * @return UpdateViewModelInterface|\Zend\Http\Response
65
     */
66
    public function onDispatch(MvcEvent $e)
67
    {
68
        $entity = $this->updater->update($this->id, $this->data);
69
70
        if (!$entity) {
71
            return $this->notFoundAction();
72
        }
73
74
        $this->viewModel->setEntity($entity);
75
        $e->setResult($this->viewModel);
76
77 View Code Duplication
        if ($this->updater->hasErrors()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
78
            $this->viewModel->setErrors($this->updater->getErrors());
79
            $this->viewModel->setInputData($this->data);
80
            return $this->viewModel;
81
        }
82
83
        if ($this->redirectTo) {
84
            return $this->redirect()->toRoute($this->redirectTo);
85
        }
86
87
        return $this->viewModel;
88
    }
89
}
90