ProcessBehavior   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 19 4
A processDelete() 0 6 1
A processPublish() 0 4 1
A processUnPublish() 0 4 1
A _toggleField() 0 8 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\ORM\Behavior;
17
18
use JBZoo\Utils\Arr;
19
use Cake\ORM\Behavior;
20
21
/**
22
 * Class ProcessBehavior
23
 *
24
 * @package Core\ORM\Behavior
25
 */
26
class ProcessBehavior extends Behavior
27
{
28
    /**
29
     * Default config.
30
     *
31
     * @var array
32
     */
33
    protected $_defaultConfig = [
34
        'field'   => 'status',
35
        'actions' => [
36
            'delete'    => 'processDelete',
37
            'publish'   => 'processPublish',
38
            'unpublish' => 'processUnPublish'
39
        ]
40
    ];
41
42
    /**
43
     * Process table method.
44
     *
45
     * @param string $name
46
     * @param array $ids
47
     * @return mixed
48
     */
49
    public function process($name, array $ids = [])
50
    {
51
        $allowActions = $this->getConfig('actions');
52
53
        if (!Arr::key($name, $allowActions)) {
54
            throw new \InvalidArgumentException(__d('core', 'Invalid action to perform'));
55
        }
56
57
        $action = $allowActions[$name];
58
        if ($action === false) {
59
            throw new \InvalidArgumentException(__d('core', 'Action "{0}" is disabled', $name));
60
        }
61
62
        if (Arr::in($action, get_class_methods($this->_table))) {
63
            return $this->_table->{$action}($ids);
64
        }
65
66
        return $this->{$action}($ids);
67
    }
68
69
    /**
70
     * Process delete method.
71
     *
72
     * @param array $ids
73
     * @return int
74
     */
75
    public function processDelete(array $ids)
76
    {
77
        return $this->_table->deleteAll([
78
            $this->_table->getPrimaryKey() . ' IN (' . implode(',', $ids) . ')'
79
        ]);
80
    }
81
82
    /**
83
     * Process publish method.
84
     *
85
     * @param array $ids
86
     * @return int
87
     */
88
    public function processPublish(array $ids)
89
    {
90
        return $this->_toggleField($ids, STATUS_PUBLISH);
91
    }
92
93
    /**
94
     * Process un publish method.
95
     *
96
     * @param array $ids
97
     * @return int
98
     */
99
    public function processUnPublish(array $ids)
100
    {
101
        return $this->_toggleField($ids, STATUS_UN_PUBLISH);
102
    }
103
104
    /**
105
     * Toggle table field.
106
     *
107
     * @param array $ids
108
     * @param int $value
109
     * @return int
110
     */
111
    protected function _toggleField(array $ids, $value = STATUS_UN_PUBLISH)
112
    {
113
        return $this->_table->updateAll([
114
            $this->_configRead('field') => $value,
115
        ], [
116
            $this->_table->getPrimaryKey() . ' IN (' . implode(',', $ids) . ')'
117
        ]);
118
    }
119
}
120