UpdateController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 6.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 6
Bugs 0 Features 4
Metric Value
wmc 5
c 6
b 0
f 4
lcom 1
cbo 5
dl 5
loc 79
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
B onDispatch() 5 23 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
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