DeleteController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 5.71 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 9
Bugs 1 Features 4
Metric Value
wmc 5
c 9
b 1
f 4
lcom 1
cbo 5
dl 4
loc 70
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B onDispatch() 4 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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