Completed
Push — feature/middleware ( 4ff047...9d8017 )
by Derek Stephen
03:15
created

DragonController::viewAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
namespace BoneMvc\Module\Dragon\Controller;
4
5
use Bone\Mvc\View\ViewEngine;
6
use Bone\View\Helper\AlertBox;
7
use BoneMvc\Module\Dragon\Collection\DragonCollection;
8
use BoneMvc\Module\Dragon\Entity\Dragon;
9
use BoneMvc\Module\Dragon\Form\DragonForm;
10
use BoneMvc\Module\Dragon\Service\DragonService;
11
use Del\Form\Field\Submit;
12
use Del\Form\Form;
13
use Del\Icon;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Zend\Diactoros\Response\HtmlResponse;
17
18
class DragonController
19
{
20
    /** @var ViewEngine $view */
21
    private $view;
22
23
    /** @var DragonService $service */
24
    private $service;
25
26
    /**
27
     * DragonController constructor.
28
     * @param DragonService $service
29
     */
30
    public function __construct(ViewEngine $view, DragonService $service)
31
    {
32
        $this->view = $view;
33
        $this->service = $service;
34
    }
35
36
    /**
37
     * @param ServerRequestInterface $request
38
     * @param array $args
39
     * @return ResponseInterface
40
     */
41
    public function indexAction(ServerRequestInterface $request, array $args): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
44
        $db = $this->service->getRepository();
45
        $dragons = new DragonCollection($db->findAll());
46
        $body = $this->view->render('dragon::index', [
47
            'dragons' => $dragons,
48
        ]);
49
50
        return new HtmlResponse($body);
51
    }
52
53
    /**
54
     * @param ServerRequestInterface $request
55
     * @param array $args
56
     * @return ResponseInterface
57
     * @throws \Doctrine\ORM\EntityNotFoundException
58
     */
59
    public function viewAction(ServerRequestInterface $request, array $args): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        $db = $this->service->getRepository();
62
        $id = $args['id'];
63
        $dragon = $db->find($id);
64
        $body = $this->view->render('dragon::index', [
65
            'dragons' => [$dragon],
66
        ]);
67
68
        return new HtmlResponse($body);
69
    }
70
71
    /**
72
     * @param ServerRequestInterface $request
73
     * @param array $args
74
     * @return ResponseInterface
75
     * @throws \Doctrine\ORM\ORMException
76
     * @throws \Doctrine\ORM\OptimisticLockException
77
     */
78
    public function createAction(ServerRequestInterface $request, array $args): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        $msg = '';
81
        $form = new DragonForm('createDragon');
82 View Code Duplication
        if ($request->getMethod() === 'POST') {
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...
83
            $post = $request->getParsedBody();
84
            $form->populate($post);
85
            if ($form->isValid()) {
86
                $data = $form->getValues();
87
                $dragon = $this->service->createFromArray($data);
88
                $this->service->saveDragon($dragon);
89
                $msg = $this->alertBox(Icon::CHECK_CIRCLE . ' New dragon added to database.', 'success');
90
                $form = new DragonForm('createDragon');
91
            } else {
92
                $msg = $this->alertBox(Icon::REMOVE . ' There was a problem with the form.', 'danger');
93
            }
94
        }
95
96
        $form = $form->render();
97
        $body = $this->view->render('dragon::create', [
98
            'form' => $form,
99
            'msg' => $msg,
100
        ]);
101
102
        return new HtmlResponse($body);
103
    }
104
105
    /**
106
     * @param ServerRequestInterface $request
107
     * @param array $args
108
     * @return ResponseInterface
109
     * @throws \Doctrine\ORM\ORMException
110
     * @throws \Doctrine\ORM\OptimisticLockException
111
     */
112
    public function editAction(ServerRequestInterface $request, array $args): ResponseInterface
113
    {
114
        $msg = '';
115
        $form = new DragonForm('editDragon');
116
        $id = $args['id'];
117
        $db = $this->service->getRepository();
118
        /** @var Dragon $dragon */
119
        $dragon = $db->find($id);
120
        $form->populate($dragon->toArray());
121
122 View Code Duplication
        if ($request->getMethod() === 'POST') {
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...
123
            $post = $request->getParsedBody();
124
            $form->populate($post);
125
            if ($form->isValid()) {
126
                $data = $form->getValues();
127
                $dragon = $this->service->updateFromArray($dragon, $data);
128
                $this->service->saveDragon($dragon);
129
                $msg = $this->alertBox(Icon::CHECK_CIRCLE . ' Dragon details updated.', 'success');
130
            } else {
131
                $msg = $this->alertBox(Icon::REMOVE . ' There was a problem with the form.', 'danger');
132
            }
133
        }
134
135
        $form = $form->render();
136
        $body = $this->view->render('dragon::edit', [
137
            'form' => $form,
138
            'msg' => $msg,
139
        ]);
140
141
        return new HtmlResponse($body);
142
    }
143
144
    /**
145
     * @param ServerRequestInterface $request
146
     * @param array $args
147
     * @return ResponseInterface
148
     * @throws \Doctrine\ORM\ORMException
149
     * @throws \Doctrine\ORM\OptimisticLockException
150
     */
151
    public function deleteAction(ServerRequestInterface $request, array $args): ResponseInterface
152
    {
153
        $id = $args['id'];
154
        $db = $this->service->getRepository();
155
        $form = new Form('deleteDragon');
156
        $submit = new Submit('submit');
157
        $submit->setValue('Delete');
158
        $submit->setClass('btn btn-danger');
159
        $form->addField($submit);
160
        /** @var Dragon $dragon */
161
        $dragon = $db->find($id);
162
163
        if ($request->getMethod() === 'POST') {
164
            $this->service->deleteDragon($dragon);
165
            $msg = $this->alertBox(Icon::CHECK_CIRCLE . ' Dragon deleted.', 'warning');
166
            $form = '<a href="/dragon" class="btn btn-default">Back</a>';
167
        } else {
168
            $form = $form->render();
169
            $msg = $this->alertBox(Icon::WARNING . ' Warning, please confirm your intention to delete.', 'danger');
170
            $msg .= '<p class="lead">Are you sure you want to delete ' . $dragon->getName() . '?</p>';
0 ignored issues
show
Bug introduced by
Consider using $dragon->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
171
        }
172
173
        $body = $this->view->render('dragon::delete', [
174
            'dragon' => $dragon,
175
            'form' => $form,
176
            'msg' => $msg,
177
        ]);
178
179
        return new HtmlResponse($body);
180
    }
181
182
    /**
183
     * @param string $message
184
     * @param string $class
185
     * @return bool|string
186
     */
187
    private function alertBox(string $message, string $class = ''): string
188
    {
189
        return AlertBox::alertBox([
190
            'message' => $message,
191
            'class' => $class,
192
        ]);
193
    }
194
}