Completed
Push — feature/middleware ( e85030...d864e0 )
by Derek Stephen
05:41
created

OrcController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 185
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 185
loc 185
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A indexAction() 19 19 2
A viewAction() 11 11 1
A createAction() 26 26 3
A editAction() 31 31 3
A deleteAction() 30 30 2
A alertBox() 7 7 1

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 declare(strict_types=1);
2
3
namespace BoneMvc\Module\Orc\Controller;
4
5
use BoneMvc\Module\Orc\Collection\OrcCollection;
6
use BoneMvc\Module\Orc\Entity\Orc;
7
use BoneMvc\Module\Orc\Form\OrcForm;
8
use BoneMvc\Module\Orc\Service\OrcService;
9
use Bone\Mvc\View\ViewEngine;
10
use Bone\View\Helper\AlertBox;
11
use Bone\View\Helper\Paginator;
12
use Del\Form\Field\Submit;
13
use Del\Form\Form;
14
use Del\Icon;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Zend\Diactoros\Response\HtmlResponse;
18
19 View Code Duplication
class OrcController
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
20
{
21
    /** @var int $numPerPage */
22
    private $numPerPage = 10;
23
24
    /** @var Paginator $paginator */
25
    private $paginator;
26
27
    /** @var OrcService $service */
28
    private $service;
29
30
    /** @var ViewEngine $view */
31
    private $view;
32
33
    /**
34
     * @param OrcService $service
35
     */
36
    public function __construct(ViewEngine $view, OrcService $service)
37
    {
38
        $this->paginator = new Paginator();
39
        $this->service = $service;
40
        $this->view = $view;
41
    }
42
43
    /**
44
     * @param ServerRequestInterface $request
45
     * @param array $args
46
     * @return ResponseInterface $response
47
     * @throws \Exception
48
     */
49
    public function indexAction(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...
50
    {
51
        $db = $this->service->getRepository();
52
        $total = $db->getTotalOrcCount();
53
54
        $this->paginator->setUrl('orc?page=:page');
55
        $page = (int) $request->getQueryParams()['page'] ?: 1;
56
        $this->paginator->setCurrentPage($page);
57
        $this->paginator->setPageCountByTotalRecords($total, $this->numPerPage);
58
59
        $orcs = new OrcCollection($db->findBy([], null, $this->numPerPage, ($page *  $this->numPerPage) - $this->numPerPage));
60
61
        $body = $this->view->render('orc::index', [
62
            'orcs' => $orcs,
63
            'paginator' => $this->paginator->render(),
64
        ]);
65
66
        return new HtmlResponse($body);
67
    }
68
69
    /**
70
     * @param ServerRequestInterface $request
71
     * @return ResponseInterface $response
72
     * @throws \Exception
73
     */
74
    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...
75
    {
76
        $db = $this->service->getRepository();
77
        $id = $args['id'];
78
        $orc = $db->find($id);
79
        $body = $this->view->render('orc::view', [
80
            'orc' => $orc,
81
        ]);
82
83
        return new HtmlResponse($body);
84
    }
85
86
    /**
87
     * @param ServerRequestInterface $request
88
     * @return ResponseInterface $response
89
     * @throws \Exception
90
     */
91
    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...
92
    {
93
        $msg = '';
94
        $form = new OrcForm('createOrc');
95
        if ($request->getMethod() === 'POST') {
96
            $post = $request->getParsedBody();
97
            $form->populate($post);
98
            if ($form->isValid()) {
99
                $data = $form->getValues();
100
                $orc = $this->service->createFromArray($data);
101
                $this->service->saveOrc($orc);
102
                $msg = $this->alertBox(Icon::CHECK_CIRCLE . ' New orc added to database.', 'success');
103
                $form = new OrcForm('createOrc');
104
            } else {
105
                $msg = $this->alertBox(Icon::REMOVE . ' There was a problem with the form.', 'danger');
106
            }
107
        }
108
109
        $form = $form->render();
110
        $body = $this->view->render('orc::create', [
111
            'form' => $form,
112
            'msg' => $msg,
113
        ]);
114
115
        return new HtmlResponse($body);
116
    }
117
118
    /**
119
     * @param ServerRequestInterface $request
120
     * @return ResponseInterface $response
121
     * @throws \Exception
122
     */
123
    public function editAction(ServerRequestInterface $request, array $args): ResponseInterface
124
    {
125
        $msg = '';
126
        $form = new OrcForm('editOrc');
127
        $id = $args['id'];
128
        $db = $this->service->getRepository();
129
        /** @var Orc $orc */
130
        $orc = $db->find($id);
131
        $form->populate($orc->toArray());
132
133
        if ($request->getMethod() === 'POST') {
134
            $post = $request->getParsedBody();
135
            $form->populate($post);
136
            if ($form->isValid()) {
137
                $data = $form->getValues();
138
                $orc = $this->service->updateFromArray($orc, $data);
139
                $this->service->saveOrc($orc);
140
                $msg = $this->alertBox(Icon::CHECK_CIRCLE . ' Orc details updated.', 'success');
141
            } else {
142
                $msg = $this->alertBox(Icon::REMOVE . ' There was a problem with the form.', 'danger');
143
            }
144
        }
145
146
        $form = $form->render();
147
        $body = $this->view->render('orc::edit', [
148
            'form' => $form,
149
            'msg' => $msg,
150
        ]);
151
152
        return new HtmlResponse($body);
153
    }
154
155
    /**
156
     * @param ServerRequestInterface $request
157
     * @return ResponseInterface $response
158
     * @throws \Exception
159
     */
160
    public function deleteAction(ServerRequestInterface $request, array $args): ResponseInterface
161
    {
162
        $id = $args['id'];
163
        $db = $this->service->getRepository();
164
        $form = new Form('deleteOrc');
165
        $submit = new Submit('submit');
166
        $submit->setValue('Delete');
167
        $submit->setClass('btn btn-danger');
168
        $form->addField($submit);
169
        /** @var Orc $orc */
170
        $orc = $db->find($id);
171
172
        if ($request->getMethod() === 'POST') {
173
            $this->service->deleteOrc($orc);
174
            $msg = $this->alertBox(Icon::CHECK_CIRCLE . ' Orc deleted.', 'warning');
175
            $form = '<a href="/orc" class="btn btn-default">Back</a>';
176
        } else {
177
            $form = $form->render();
178
            $msg = $this->alertBox(Icon::WARNING . ' Warning, please confirm your intention to delete.', 'danger');
179
            $msg .= '<p class="lead">Are you sure you want to delete ' . $orc->getName() . '?</p>';
180
        }
181
182
        $body = $this->view->render('orc::delete', [
183
            'orc' => $orc,
184
            'form' => $form,
185
            'msg' => $msg,
186
        ]);
187
188
        return new HtmlResponse($body);
189
    }
190
191
    /**
192
     * @param string $message
193
     * @param string $class
194
     * @return string
195
     */
196
    private function alertBox(string $message, string $class): string
197
    {
198
        return AlertBox::alertBox([
199
            'message' => $message,
200
            'class' => $class,
201
        ]);
202
    }
203
}
204