|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Lib\Model\Table; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Core\Configure; |
|
6
|
|
|
use Cake\Core\InstanceConfigTrait; |
|
7
|
|
|
use Cake\Database\Expression\QueryExpression; |
|
8
|
|
|
use Cake\Event\Event; |
|
9
|
|
|
use Cake\Event\EventManager; |
|
10
|
|
|
use Cake\ORM\Entity; |
|
11
|
|
|
use Cake\ORM\Table; |
|
12
|
|
|
use Saito\Event\SaitoEventManager; |
|
13
|
|
|
|
|
14
|
|
|
class AppTable extends Table |
|
15
|
|
|
{ |
|
16
|
|
|
use InstanceConfigTrait { |
|
17
|
|
|
getConfig as private traitGetConfig; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** @var array default config for InstanceConfigTrait */ |
|
21
|
|
|
protected $_defaultConfig = []; |
|
22
|
|
|
|
|
23
|
|
|
public $SharedObjects; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var SaitoEventManager |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $_SEM; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Toggle bool field value. |
|
32
|
|
|
* |
|
33
|
|
|
* @param int $recordId The record-ID. |
|
34
|
|
|
* @param string $field The name of the field to toggle. |
|
35
|
|
|
* @return int new field The new value after the toggle. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function toggle($recordId, $field) |
|
38
|
|
|
{ |
|
39
|
|
|
$entity = $this->query() |
|
40
|
|
|
->select(['id', $field]) |
|
41
|
|
|
->where(['id' => $recordId]) |
|
42
|
|
|
->first(); |
|
43
|
|
|
$new = ($entity->get($field) == 0) ? 1 : 0; |
|
44
|
|
|
$this->patchEntity($entity, [$field => $new]); |
|
|
|
|
|
|
45
|
|
|
$this->save($entity); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
return $new; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Check that a record exists. |
|
52
|
|
|
* |
|
53
|
|
|
* @param int|array|\ArrayAccess $conditions Record-ID or query conditions. |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function exists($conditions) |
|
57
|
|
|
{ |
|
58
|
|
|
if (is_int($conditions)) { |
|
59
|
|
|
$conditions = ['id' => $conditions]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return parent::exists($conditions); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Increments value of a field |
|
67
|
|
|
* |
|
68
|
|
|
* @param int|array $where entry-id or array with conditions |
|
69
|
|
|
* @param string $field fielt to increment |
|
70
|
|
|
* @param int $amount Increment size. |
|
71
|
|
|
* @return void |
|
72
|
|
|
* @throws \InvalidArgumentException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function increment($where, $field, $amount = 1) |
|
75
|
|
|
{ |
|
76
|
|
|
if (!is_int($amount)) { |
|
77
|
|
|
throw new \InvalidArgumentException; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (is_numeric($where)) { |
|
81
|
|
|
$where = ['id' => $where]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$operator = '+'; |
|
85
|
|
|
if ($amount < 0) { |
|
86
|
|
|
$operator = '-'; |
|
87
|
|
|
$amount *= -1; |
|
88
|
|
|
} |
|
89
|
|
|
$expression = new QueryExpression("$field = $field $operator $amount"); |
|
90
|
|
|
$this->updateAll($expression, $where); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Dispatches an event |
|
95
|
|
|
* |
|
96
|
|
|
* - Always passes the issuing model class as subject |
|
97
|
|
|
* - Wrapper for CakeEvent boilerplate code |
|
98
|
|
|
* - Easier to test |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $event event identifier `Model.<modelname>.<event>` |
|
101
|
|
|
* @param array $data additional event data |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function _dispatchEvent($event, $data = []) |
|
105
|
|
|
{ |
|
106
|
|
|
EventManager::instance()->dispatch(new Event($event, $this, $data)); |
|
107
|
|
|
// propagate event on Saito's event bus |
|
108
|
|
|
$this->dispatchSaitoEvent($event, $data); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Dispatch Saito Event |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $event event |
|
115
|
|
|
* @param array $data data |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
|
|
public function dispatchSaitoEvent($event, $data) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!$this->_SEM) { |
|
121
|
|
|
$this->_SEM = SaitoEventManager::getInstance(); |
|
122
|
|
|
} |
|
123
|
|
|
$this->_SEM->dispatch($event, $data + ['Model' => $this]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getConfig($key = null, $default = null) |
|
130
|
|
|
{ |
|
131
|
|
|
if (is_string($key)) { |
|
132
|
|
|
$setting = Configure::read('Saito.Settings.' . $key); |
|
133
|
|
|
if ($setting !== null) { |
|
134
|
|
|
return $setting; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $this->traitGetConfig($key, $default); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.