CreateController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A onDispatch() 0 19 3
1
<?php
2
3
namespace Sebaks\Crud\Controller;
4
5
use Zend\Mvc\MvcEvent;
6
use Zend\Mvc\Controller\AbstractActionController;
7
use T4webDomainInterface\Service\CreatorInterface;
8
use Sebaks\Crud\View\Model\CreateViewModelInterface;
9
10
class CreateController extends AbstractActionController
11
{
12
    /**
13
     * @var array
14
     */
15
    private $data;
16
17
    /**
18
     * @var CreatorInterface
19
     */
20
    private $creator;
21
22
    /**
23
     * @var CreateViewModelInterface
24
     */
25
    private $viewModel;
26
27
    /**
28
     * @var string
29
     */
30
    private $redirectTo;
31
32
    /**
33
     * @param array $data
34
     * @param CreatorInterface $creator
35
     * @param CreateViewModelInterface $viewModel
36
     * @param null $redirectTo
37
     */
38
    public function __construct(
39
        array $data,
40
        CreatorInterface $creator,
41
        CreateViewModelInterface $viewModel,
42
        $redirectTo = null)
43
    {
44
        $this->data = $data;
45
        $this->creator = $creator;
46
        $this->viewModel = $viewModel;
47
        $this->redirectTo = $redirectTo;
48
    }
49
50
    /**
51
     * Execute the request
52
     *
53
     * @param  MvcEvent $e
54
     * @return CreateViewModelInterface|\Zend\Http\Response
55
     */
56
    public function onDispatch(MvcEvent $e)
57
    {
58
        $entity = $this->creator->create($this->data);
59
60
        if ($entity) {
61
            if ($this->redirectTo) {
62
                return $this->redirect()->toRoute($this->redirectTo);
63
            }
64
65
            $this->viewModel->setEntity($entity);
66
        } else {
67
            $this->viewModel->setErrors($this->creator->getErrors());
68
            $this->viewModel->setInputData($this->data);
69
        }
70
71
        $e->setResult($this->viewModel);
72
73
        return $this->viewModel;
74
    }
75
}
76