Completed
Push — master ( eb5cf7...2b1758 )
by Cheren
08:53
created

MoveComponent   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 4 1
A _move() 0 21 3
A down() 0 4 1
A setConfig() 0 12 1
A _redirect() 0 10 1
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Controller\Component;
17
18
use Cake\ORM\Table;
19
use JBZoo\Utils\Arr;
20
use Cake\Controller\Component;
21
use Cake\ORM\Behavior\TreeBehavior;
22
23
/**
24
 * Class MoveComponent
25
 *
26
 * @package Core\Controller\Component
27
 * @property FlashComponent $Flash
28
 */
29
class MoveComponent extends AppComponent
30
{
31
32
    const TYPE_UP   = 'moveUp';
33
    const TYPE_DOWN = 'moveDown';
34
35
    /**
36
     * Other Components this component uses.
37
     *
38
     * @var array
39
     */
40
    public $components = [
41
        'Core.Flash'
42
    ];
43
44
    /**
45
     * Move down record in tree.
46
     *
47
     * @param Table $table
48
     * @param int $id
49
     * @param int $step
50
     * @return \Cake\Http\Response|null
51
     */
52
    public function down(Table $table, $id, $step = 1)
53
    {
54
        return $this->_move($table, $id, $step, self::TYPE_DOWN);
55
    }
56
57
    /**
58
     * Sets the config.
59
     *
60
     * @param array|string $key
61
     * @param null|mixed $value
62
     * @param bool $merge
63
     * @return mixed
64
     * @throws \Cake\Core\Exception\Exception When trying to set a key that is invalid.
65
     */
66
    public function setConfig($key, $value = null, $merge = true)
67
    {
68
        $this->_defaultConfig = [
69
            'messages' => [
70
                'success' => __d('core', 'Object has been moved'),
71
                'error'   => __d('core', 'Object could not been moved')
72
            ],
73
            'action' => 'index',
74
        ];
75
76
        return parent::setConfig($key, $value, $merge);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::setConfig($key, $value, $merge); (Core\Controller\Component\MoveComponent) is incompatible with the return type of the parent method Cake\Controller\Component::setConfig of type Cake\Core\InstanceConfigTrait.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
77
    }
78
79
    /**
80
     * Move up record in tree.
81
     *
82
     * @param Table $table
83
     * @param int $id
84
     * @param int $step
85
     * @return \Cake\Http\Response|null
86
     * @SuppressWarnings(PHPMD.ShortMethodName)
87
     */
88
    public function up(Table $table, $id, $step = 1)
89
    {
90
        return $this->_move($table, $id, $step);
91
    }
92
93
    /**
94
     * Move object in tree table.
95
     *
96
     * @param Table $table
97
     * @param string $type
98
     * @param int $id
99
     * @param int $step
100
     * @return \Cake\Http\Response|null
101
     */
102
    protected function _move(Table $table, $id, $step = 1, $type = self::TYPE_UP)
103
    {
104
        $behaviors = $table->behaviors();
105
        if (!Arr::in('Tree', $behaviors->loaded())) {
106
            $behaviors->load('Tree');
107
        }
108
109
        $entity = $table->get($id);
110
111
        /** @var TreeBehavior $treeBehavior */
112
        $treeBehavior = $behaviors->get('Tree');
113
        $treeBehavior->setConfig('scope', $entity->get('id'));
114
115
        if ($table->{$type}($entity, $step)) {
116
            $this->Flash->success($this->_configRead('messages.success'));
117
        } else {
118
            $this->Flash->error($this->_configRead('messages.error'));
119
        }
120
121
        return $this->_redirect();
122
    }
123
124
    /**
125
     * Process redirect.
126
     *
127
     * @return \Cake\Http\Response|null
128
     */
129
    protected function _redirect()
130
    {
131
        $request = $this->_controller->request;
132
        return $this->_controller->redirect([
133
            'prefix'     => $request->getParam('prefix'),
134
            'plugin'     => $request->getParam('plugin'),
135
            'controller' => $request->getParam('controller'),
136
            'action'     => $this->getConfig('action'),
137
        ]);
138
    }
139
}
140