AppComponent   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 11
dl 0
loc 136
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 7 1
A redirect() 0 32 3
A toggleField() 0 22 2
A checkIsAjax() 0 6 2
A _checkToggleData() 0 6 3
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 Core\ORM\Table;
19
use JBZoo\Utils\Arr;
20
use JBZoo\Utils\Str;
21
use Cake\Utility\Hash;
22
use Cake\Http\ServerRequest;
23
use Cake\Controller\Component;
24
use Cake\Controller\Controller;
25
use Cake\Http\Exception\BadRequestException;
26
27
/**
28
 * Class AppComponent
29
 *
30
 * @package Core\Controller\Component
31
 */
32
class AppComponent extends Component
33
{
34
35
    /**
36
     * Controller object.
37
     *
38
     * @var     Controller
39
     */
40
    protected $_controller;
41
42
    /**
43
     * Hold controller request (Server request).
44
     *
45
     * @var     ServerRequest
46
     */
47
    protected $_request;
48
49
    /**
50
     * Constructor hook method.
51
     *
52
     * @param   array $config
53
     * @return  void
54
     */
55
    public function initialize(array $config)
56
    {
57
        $this->_controller = $this->_registry->getController();
58
        $this->_request    = $this->_controller->request;
59
60
        parent::initialize($config);
61
    }
62
63
    /**
64
     * Redirect by request data.
65
     *
66
     * @param   array $options
67
     * @return  \Cake\Http\Response|null
68
     */
69
    public function redirect(array $options = [])
70
    {
71
        $plugin     = $this->_request->getParam('plugin');
72
        $controller = $this->_request->getParam('controller');
73
74
        $_options = [
75
            'apply'   => [],
76
            'savenew' => [
77
                'plugin'     => $plugin,
78
                'controller' => $controller,
79
                'action'     => 'add'
80
            ],
81
            'save' => [
82
                'plugin'     => $plugin,
83
                'controller' => $controller,
84
                'action'     => 'index',
85
            ]
86
        ];
87
88
        $options = Hash::merge($_options, $options);
89
90
        $url = $options['save'];
91
        if ($rAction = $this->_request->getData('action')) {
92
            list(, $action) = pluginSplit($rAction);
0 ignored issues
show
Bug introduced by
It seems like $rAction can also be of type array; however, pluginSplit() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
93
            $action = Str::low($action);
94
            if (Arr::key($action, $options)) {
95
                $url = $options[$action];
96
            }
97
        }
98
99
        return $this->_controller->redirect($url);
100
    }
101
102
    /**
103
     * Toggle table field value.
104
     *
105
     * @param   Table $table
106
     * @param   int $id
107
     * @param   string|int $value
108
     * @param   string $field
109
     *
110
     * @throws  \RuntimeException
111
     * @throws  \Aura\Intl\Exception
112
     */
113
    public function toggleField(Table $table, $id, $value, $field = 'status')
114
    {
115
        $this->checkIsAjax();
116
        $this->_checkToggleData($id, $value);
117
118
        $this->_controller->viewBuilder()
119
            ->setLayout('ajax')
120
            ->setTemplate('toggle')
121
            ->setTemplatePath('Common');
122
123
        $entity   = $table->get($id);
124
        $newValue = ! (int) $value;
125
126
        $entity->set($field, (int) $newValue);
127
128
        if ($result = $table->save($entity)) {
129
            $this->_controller->set('entity', $result);
130
            $this->_controller->render('toggle');
131
        } else {
132
            throw new \RuntimeException(__d('core', 'Failed toggling field {0} to {1}', $field, $entity->get($field)));
133
        }
134
    }
135
136
    /**
137
     * Check is ajax request.
138
     *
139
     * @return  void
140
     *
141
     * @throws  \RuntimeException
142
     * @throws  \Aura\Intl\Exception
143
     */
144
    public function checkIsAjax()
145
    {
146
        if (!$this->_request->is('ajax')) {
147
            throw new \RuntimeException(__d('core', 'Bad request'));
148
        }
149
    }
150
151
    /**
152
     * Check toggle data.
153
     *
154
     * @param   int $id
155
     * @param   string|int $value
156
     * @return  void
157
     *
158
     * @throws  \RuntimeException
159
     * @throws  \Aura\Intl\Exception
160
     */
161
    protected function _checkToggleData($id, $value)
162
    {
163
        if (empty($id) || $value === null) {
164
            throw new BadRequestException(__d('core', 'Invalid content'));
165
        }
166
    }
167
}
168