PostController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 16.33 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 7
dl 16
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B filters() 0 32 1
A actionIndex() 0 13 2
A actionView() 0 9 1
A actionCreate() 0 20 3
A actionUpdate() 9 9 1
A actionDelete() 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
2
3
namespace App\Modules\Blog\Controllers;
4
5
use App\Components\Controller;
6
use App\Components\View;
7
use App\Modules\Blog\Models\Blog;
8
use Micro\Db\Injector;
9
use Micro\Mvc\Models\Query;
10
use Micro\Web\RequestInjector;
11
12
/**
13
 * Class PostController
14
 * @package App\Modules\Blog\Controllers
15
 */
16
class PostController extends Controller
17
{
18
    public function filters()
19
    {
20
        return [
21
            [
22
                'class' => '\Micro\Filter\AccessFilter',
23
                'actions' => ['index', 'view', 'create', 'update', 'delete'],
24
                'rules' => [
25
                    [
26
                        'allow' => false,
27
                        'actions' => ['create', 'update', 'delete'],
28
                        'users' => ['?'],
29
                        'message' => 'Only for authorized!'
30
                    ],
31
                    [
32
                        'allow' => true,
33
                        'actions' => ['index', 'view'],
34
                        'users' => ['*'],
35
                        'message' => 'View for all'
36
                    ]
37
                ]
38
            ],
39
            [
40
                'class' => '\Micro\Filter\CsrfFilter',
41
                'actions' => ['login']
42
            ],
43
            [
44
                'class' => '\Micro\Filter\XssFilter',
45
                'actions' => ['index', 'login', 'logout'],
46
                'clean' => '*'
47
            ]
48
        ];
49
    }
50
51
    public function actionIndex()
52
    {
53
        $crt = new Query((new Injector)->getDriver());
54
        $crt->table = Blog::tableName();
55
        $crt->order = 'id DESC';
56
57
        $v = new View();
58
        $v->addParameter('blogs', $crt);
59
        $query = (new RequestInjector)->build()->getQueryParams();
60
        $v->addParameter('page', $query['page'] ?: 0);
61
62
        return $v;
63
    }
64
65
    public function actionView()
66
    {
67
        $query = (new RequestInjector)->build()->getQueryParams();
68
        $blog = Blog::findByPk($query['id']);
69
        $v = new View();
70
        $v->addParameter('model', $blog);
71
72
        return $v;
73
    }
74
75
    public function actionCreate()
76
    {
77
        $blog = new Blog();
78
79
        $body = (new RequestInjector)->build()->getParsedBody();
80
        /** @var array $blogData */
81
        if ($blogData = $body['Blog']) {
82
            $blog->name = $blogData['name'];
83
            $blog->content = $blogData['content'];
84
85
            if ($blog->save()) {
86
                return $this->redirect('/blog/post/' . $blog->id);
87
            }
88
        }
89
90
        $v = new View();
91
        $v->addParameter('model', $blog);
92
93
        return $v;
94
    }
95
96 View Code Duplication
    public function actionUpdate()
0 ignored issues
show
Duplication introduced by
This method 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...
97
    {
98
        $query = (new RequestInjector)->build()->getQueryParams();
99
        $blog = Blog::findByPk($query['id']);
100
101
        $blog->name = 'setup-er';
102
103
        return $blog->save();
104
    }
105
106 View Code Duplication
    public function actionDelete()
0 ignored issues
show
Duplication introduced by
This method 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...
107
    {
108
        $query = (new RequestInjector)->build()->getQueryParams();
109
        $blog = Blog::findByPk($query['id']);
110
111
        return $blog->delete();
112
    }
113
}
114