Completed
Push — master ( b5b0aa...3a12ca )
by Oscar
02:24
created

Entity   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 181
Duplicated Lines 10.5 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 20
c 6
b 1
f 1
lcom 0
cbo 3
dl 19
loc 181
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
D listItems() 0 74 13
B createItem() 11 32 3
B editItem() 8 43 3
A deleteItem() 0 10 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
2
3
namespace Folk\Controllers;
4
5
use Psr7Middlewares\Middleware\FormatNegotiator;
6
use Zend\Diactoros\Response\RedirectResponse;
7
use FormManager\Builder as F;
8
use Folk\SearchQuery;
9
10
class Entity
11
{
12
    protected $entity;
13
14
    /**
15
     * GET: List/search items.
16
     */
17
    public function listItems($request, $response, $app)
0 ignored issues
show
Unused Code introduced by
The parameter $response 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...
18
    {
19
        $entity = $app->getEntity($request->getAttribute('entity'));
20
        $search = new SearchQuery($request->getQueryParams());
21
22
        if ($search->getPage() === null) {
23
            $search->setPage(1);
24
        }
25
26
        //Check whether the query is an id
27
        if (($id = $search->getId())) {
28
            if ($items = $entity->read($id)) {
29
                $items = [$id => $items];
30
            } else {
31
                $items = [];
32
            }
33
        } else {
34
            $items = $entity->search($search);
35
        }
36
37
        //Return as json (for ajax calls)
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
        if (FormatNegotiator::getFormat($request) === 'json') {
39
            $json = [];
40
41
            foreach ($items as $id => $item) {
42
                $json[$id] = $entity->getLabel($id, $item);
43
            }
44
45
            return json_encode($json);
46
        }
47
48
        //Redirect to edit element if it's only one result
49
        if (!empty($search->getQuery()) && count($items) === 1) {
50
            return new RedirectResponse($app->getRouteUrl('edit', [
51
                'entity' => $entity->getName(),
52
                'id' => key($items),
53
            ]));
54
        }
55
56
        //Load the values
57
        $row = $entity->getScheme($app['builder']);
58
59
        //Remove non-listable elements
60
        $removedKeys = [];
61
62
        foreach ($row as $key => $value) {
63
            if ($value->get('list') === false) {
64
                $removedKeys[] = $key;
65
            }
66
        }
67
68
        foreach ($removedKeys as $key) {
69
            unset($row[$key]);
70
        }
71
72
        //populate the data
73
        $rows = [];
74
75
        foreach ($items as $id => &$item) {
76
            $rows[$id] = clone $row;
77
            $rows[$id]->val($item);
78
        }
79
80
        if (count($items) === 50) {
81
            $search->setPage($search->getPage() + 1);
82
        }
83
84
        //List all results
85
        return $app['templates']->render('pages/list', [
86
            'rows' => $rows,
87
            'entity' => $entity,
88
            'search' => $search,
89
        ]);
90
    }
91
92
    /**
93
     * POST: Create a new element
94
     * GET:  Display the form.
95
     */
96
    public function createItem($request, $response, $app)
0 ignored issues
show
Unused Code introduced by
The parameter $response 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...
97
    {
98
        $entity = $app->getEntity($request->getAttribute('entity'));
99
100
        //Create form
101
        $form = F::form()
102
            ->method('post')
103
            ->enctype('multipart/form-data');
104
105
        $form->add([
106
            'entity' => F::hidden()->val($entity->getName())->class('field-data-entity'),
107
            'data' => $entity->getScheme($app['builder']),
108
        ]);
109
110
        //Save
111 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...
112
            //Load data
113
            $form->loadFromPsr7($request);
114
115
            if ($form->isValid()) {
116
                return new RedirectResponse($app->getRouteUrl('edit', [
117
                    'entity' => $entity->getName(),
118
                    'id' => $entity->create($form['data']->val()),
119
                ]));
120
            }
121
        }
122
123
        return $app['templates']->render('pages/create', [
124
            'entity' => $entity,
125
            'form' => $form,
126
        ]);
127
    }
128
129
    /**
130
     * POST: Edit an element
131
     * GET:  Display the form.
132
     */
133
    public function editItem($request, $response, $app)
0 ignored issues
show
Unused Code introduced by
The parameter $response 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...
134
    {
135
        $entity = $app->getEntity($request->getAttribute('entity'));
136
        $id = $request->getAttribute('id');
137
138
        //Create form
139
        $form = F::form()
140
            ->method('post')
141
            ->enctype('multipart/form-data');
142
143
        $form->add([
144
            'id' => F::hidden()->val($id)->class('field-data-id'),
145
            'entity' => F::hidden()->val($entity->getName())->class('field-data-entity'),
146
            'data' => $entity->getScheme($app['builder']),
147
        ]);
148
149
        //Save
150
        if ($request->getMethod() === 'POST') {
151
            //Load data
152
            $form->loadFromPsr7($request);
153
154 View Code Duplication
            if ($form->isValid()) {
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...
155
                $entity->update($id, $form['data']->val());
156
157
                return new RedirectResponse($app->getRouteUrl('edit', [
158
                    'entity' => $entity->getName(),
159
                    'id' => $id,
160
                ]));
161
            }
162
        } else {
163
            //View
164
            $data = $entity->read($id);
165
166
            $form['data']->val($data);
167
        }
168
169
        //Render template
170
        return $app['templates']->render('pages/edit', [
171
            'entity' => $entity,
172
            'form' => $form,
173
            'id' => $id,
174
        ]);
175
    }
176
177
    /**
178
     * POST: Delete the element.
179
     */
180
    public function deleteItem($request, $response, $app)
0 ignored issues
show
Unused Code introduced by
The parameter $response 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...
181
    {
182
        $entity = $app->getEntity($request->getAttribute('entity'));
183
184
        $entity->delete($request->getAttribute('id'));
185
186
        return new RedirectResponse($app->getRouteUrl('list', [
187
            'entity' => $entity->getName(),
188
        ]));
189
    }
190
}
191