Completed
Push — master ( d6bb8e...21b718 )
by Oleg
05:17
created

PostController::filters()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 32
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
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\Mvc\Models\Query;
9
10
/**
11
 * Class PostController
12
 * @package App\Modules\Blog\Controllers
13
 */
14
class PostController extends Controller
15
{
16
    public function filters()
17
    {
18
        return [
19
            [
20
                'class' => '\Micro\Filter\AccessFilter',
21
                'actions' => ['index', 'view', 'create', 'update', 'delete'],
22
                'rules' => [
23
                    [
24
                        'allow' => false,
25
                        'actions' => ['create', 'update', 'delete'],
26
                        'users' => ['?'],
27
                        'message' => 'Only for authorized!'
28
                    ],
29
                    [
30
                        'allow' => true,
31
                        'actions' => ['index', 'view'],
32
                        'users' => ['*'],
33
                        'message' => 'View for all'
34
                    ]
35
                ]
36
            ],
37
            [
38
                'class' => '\Micro\Filter\CsrfFilter',
39
                'actions' => ['login']
40
            ],
41
            [
42
                'class' => '\Micro\Filter\XssFilter',
43
                'actions' => ['index', 'login', 'logout'],
44
                'clean' => '*'
45
            ]
46
        ];
47
    }
48
49
    public function actionIndex()
50
    {
51
        $crt = new Query($this->container->db);
0 ignored issues
show
Bug introduced by
Accessing db on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
52
        $crt->table = Blog::tableName();
53
        $crt->order = 'id DESC';
54
55
        $v = new View($this->container);
56
        $v->addParameter('blogs', $crt);
57
        $v->addParameter('page', $this->container->request->query('page') ?: 0);
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
58
59
        return $v;
60
    }
61
62 View Code Duplication
    public function actionView()
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...
63
    {
64
        $blog = Blog::findByPk($this->container->request->query('id'), $this->container);
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
65
        $v = new View($this->container);
66
        $v->addParameter('model', $blog);
67
68
        return $v;
69
    }
70
71
    public function actionCreate()
72
    {
73
        $blog = new Blog($this->container);
74
75
        /** @var array $blogData */
76
        if ($blogData = $this->container->request->post('Blog')) {
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
77
            $blog->name = $blogData['name'];
78
            $blog->content = $blogData['content'];
79
80
            if ($blog->save()) {
81
                $this->redirect('/blog/post/' . $blog->id);
82
            }
83
        }
84
85
        $v = new View($this->container);
86
        $v->addParameter('model', $blog);
87
88
        return $v;
89
    }
90
91 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...
92
    {
93
        $blog = Blog::findByPk($this->container->request->query('id'), $this->container);
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
94
95
        $blog->name = 'setup-er';
96
97
        return $blog->save();
98
    }
99
100
    public function actionDelete()
101
    {
102
        $blog = Blog::findByPk(
103
            $this->container->request->query('id'),
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
104
            $this->container
105
        );
106
107
        return $blog->delete();
108
    }
109
}
110