Completed
Push — master ( 77ceca...7f809c )
by Dmitriy
03:33
created

DeleteController::onDispatch()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 4
Ratio 18.18 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 4
loc 22
rs 8.9197
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace Sebaks\Crud\Controller;
4
5
use Zend\Mvc\MvcEvent;
6
use Zend\Mvc\Controller\AbstractActionController;
7
use T4webDomainInterface\Service\DeleterInterface;
8
use Sebaks\Crud\View\Model\DeleteViewModelInterface;
9
10
class DeleteController extends AbstractActionController
11
{
12
    /**
13
     * @var int
14
     */
15
    private $id;
16
17
    /**
18
     * @var DeleterInterface
19
     */
20
    private $deleter;
21
22
    /**
23
     * @var DeleteViewModelInterface
24
     */
25
    private $viewModel;
26
27
    /**
28
     * @var string
29
     */
30
    private $redirectTo;
31
32
    /**
33
     * @param $id
34
     * @param DeleterInterface $deleter
35
     * @param DeleteViewModelInterface $viewModel
36
     * @param null $redirectTo
37
     */
38
    public function __construct(
39
        $id,
40
        DeleterInterface $deleter,
41
        DeleteViewModelInterface $viewModel,
42
        $redirectTo = null
43
    )
44
    {
45
        $this->id = $id;
46
        $this->deleter = $deleter;
47
        $this->viewModel = $viewModel;
48
        $this->redirectTo = $redirectTo;
49
    }
50
51
    /**
52
     * Execute the request
53
     *
54
     * @param  MvcEvent $e
55
     * @return DeleteViewModelInterface|\Zend\Http\Response
56
     */
57
    public function onDispatch(MvcEvent $e)
58
    {
59
        $entity = $this->deleter->delete($this->id);
60
61
        if (!$entity) {
62
            return $this->notFoundAction();
63
        }
64
        
65
        if ($this->redirectTo) {
66
            return $this->redirect()->toRoute($this->redirectTo);
67
        }
68
69
        $this->viewModel->setEntity($entity);
70
        $e->setResult($this->viewModel);
71
72 View Code Duplication
        if ($this->deleter->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...
73
            $this->viewModel->setErrors($this->deleter->getErrors());
74
            $this->viewModel->setInputData([$this->id]);
0 ignored issues
show
Bug introduced by
The method setInputData() does not seem to exist on object<Sebaks\Crud\View\...leteViewModelInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
        }
76
77
        return $this->viewModel;
78
    }
79
}
80