Skip to content

Commit 00f9829

Browse files
authored
Merge pull request #179 from dereuromark/improvements
Use entity interface and more specific exception.
2 parents 6d3bbce + 363b8bd commit 00f9829

File tree

7 files changed

+59
-54
lines changed

7 files changed

+59
-54
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"test": "php phpunit.phar",
4646
"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit.phar",
4747
"test-coverage": "php phpunit.phar --log-junit webroot/coverage/unitreport.xml --coverage-html webroot/coverage --coverage-clover webroot/coverage/coverage.xml",
48-
"cs-check": "vendor/bin/phpcs -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/cakephp-tools/vendor/,/tmp/,/logs/,/tests/test_files/ --extensions=php ./",
48+
"cs-check": "phpcs -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/cakephp-tools/vendor/,/tmp/,/logs/,/tests/test_files/ --extensions=php ./",
4949
"cs-fix": "phpcbf -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/cakephp-tools/vendor/,/tmp/,/logs/,/tests/test_files --extensions=php ./"
5050
}
5151
}

src/Model/Behavior/BitmaskedBehavior.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Tools\Model\Behavior;
44

55
use ArrayObject;
6+
use Cake\Datasource\EntityInterface;
67
use Cake\Event\Event;
78
use Cake\ORM\Behavior;
8-
use Cake\ORM\Entity;
99
use Cake\ORM\Query;
1010
use Cake\Utility\Inflector;
11-
use Exception;
11+
use RuntimeException;
1212
use Tools\Utility\Text;
1313

1414
/**
@@ -68,7 +68,7 @@ public function initialize(array $config = []) {
6868
$config['bits'] = false;
6969
}
7070
if (empty($config['bits'])) {
71-
throw new Exception('Bits not found');
71+
throw new RuntimeException('Bits not found');
7272
}
7373
ksort($config['bits'], SORT_NUMERIC);
7474

@@ -97,11 +97,11 @@ public function beforeFind(Event $event, Query $query) {
9797

9898
/**
9999
* @param \Cake\Event\Event $event
100-
* @param \Cake\ORM\Entity $entity
100+
* @param \Cake\Datasource\EntityInterface $entity
101101
* @param \ArrayObject $options
102102
* @return void
103103
*/
104-
public function beforeRules(Event $event, Entity $entity, ArrayObject $options) {
104+
public function beforeRules(Event $event, EntityInterface $entity, ArrayObject $options) {
105105
if ($this->_config['on'] !== 'beforeRules' || !$options['checkRules']) {
106106
return;
107107
}
@@ -110,11 +110,11 @@ public function beforeRules(Event $event, Entity $entity, ArrayObject $options)
110110

111111
/**
112112
* @param \Cake\Event\Event $event
113-
* @param \Cake\ORM\Entity $entity
113+
* @param \Cake\Datasource\EntityInterface $entity
114114
* @param \ArrayObject $options
115115
* @return void
116116
*/
117-
public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
117+
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
118118
if ($this->_config['on'] !== 'beforeSave') {
119119
return;
120120
}
@@ -190,10 +190,10 @@ public function encodeBitmaskConditions(Query $query) {
190190
}
191191

192192
/**
193-
* @param \Cake\ORM\Entity $entity
193+
* @param \Cake\Datasource\EntityInterface $entity
194194
* @return void
195195
*/
196-
public function encodeBitmaskData(Entity $entity) {
196+
public function encodeBitmaskData(EntityInterface $entity) {
197197
$field = $this->_config['field'];
198198
if (!($mappedField = $this->_config['mappedField'])) {
199199
$mappedField = $field;

src/Model/Behavior/JsonableBehavior.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use ArrayObject;
66
use Cake\Database\Type;
7+
use Cake\Datasource\EntityInterface;
78
use Cake\Event\Event;
89
use Cake\ORM\Behavior;
910
use Cake\ORM\Entity;
1011
use Cake\ORM\Query;
11-
use Exception;
12+
use RuntimeException;
1213
use Tools\Database\Type\ArrayType;
1314
use Tools\Utility\Text;
1415

@@ -71,12 +72,12 @@ class JsonableBehavior extends Behavior {
7172

7273
/**
7374
* @param array $config
74-
* @throws \Exception
75+
* @throws \RuntimeException
7576
* @return void
7677
*/
7778
public function initialize(array $config = []) {
7879
if (empty($this->_config['fields'])) {
79-
throw new Exception('Fields are required');
80+
throw new RuntimeException('Fields are required');
8081
}
8182
if (!is_array($this->_config['fields'])) {
8283
$this->_config['fields'] = (array)$this->_config['fields'];
@@ -85,7 +86,7 @@ public function initialize(array $config = []) {
8586
$this->_config['map'] = (array)$this->_config['map'];
8687
}
8788
if (!empty($this->_config['map']) && count($this->_config['fields']) !== count($this->_config['map'])) {
88-
throw new Exception('Fields and Map need to be of the same length if map is specified.');
89+
throw new RuntimeException('Fields and Map need to be of the same length if map is specified.');
8990
}
9091
foreach ($this->_config['fields'] as $field) {
9192
$this->_table->schema()->columnType($field, 'array');
@@ -121,10 +122,10 @@ public function beforeFind(Event $event, Query $query) {
121122
/**
122123
* Decodes the fields of an array/entity (if the value itself was encoded)
123124
*
124-
* @param \Cake\ORM\Entity $entity
125+
* @param \Cake\Datasource\EntityInterface $entity
125126
* @return void
126127
*/
127-
public function decodeItems(Entity $entity) {
128+
public function decodeItems(EntityInterface $entity) {
128129
$fields = $this->_getMappedFields();
129130

130131
foreach ($fields as $map => $field) {
@@ -139,11 +140,11 @@ public function decodeItems(Entity $entity) {
139140
* Saves all fields that do not belong to the current Model into 'with' helper model.
140141
*
141142
* @param \Cake\Event\Event $event
142-
* @param \Cake\ORM\Entity $entity
143+
* @param \Cake\Datasource\EntityInterface $entity
143144
* @param \ArrayObject $options
144145
* @return void
145146
*/
146-
public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
147+
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
147148
$fields = $this->_getMappedFields();
148149

149150
foreach ($fields as $map => $field) {

src/Model/Behavior/PasswordableBehavior.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use ArrayObject;
66
use Cake\Auth\PasswordHasherFactory;
77
use Cake\Core\Configure;
8+
use Cake\Datasource\EntityInterface;
89
use Cake\Event\Event;
910
use Cake\ORM\Behavior;
10-
use Cake\ORM\Entity;
1111
use Cake\ORM\Table;
12-
use Exception;
12+
use RuntimeException;
1313

1414
if (!defined('PWD_MIN_LENGTH')) {
1515
define('PWD_MIN_LENGTH', 6);
@@ -131,7 +131,7 @@ public function initialize(array $config) {
131131
$formFieldCurrent = $this->_config['formFieldCurrent'];
132132

133133
if ($formField === $this->_config['field']) {
134-
throw new Exception('Invalid setup - the form field must to be different from the model field (' . $this->_config['field'] . ').');
134+
throw new RuntimeException('Invalid setup - the form field must to be different from the model field (' . $this->_config['field'] . ').');
135135
}
136136

137137
$rules = $this->_validationRules;
@@ -276,12 +276,12 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti
276276
* Preparing the data
277277
*
278278
* @param \Cake\Event\Event $event
279-
* @param \Cake\ORM\Entity $entity
279+
* @param \Cake\Datasource\EntityInterface $entity
280280
* @param \ArrayObject $options
281281
* @param string $operation
282282
* @return void
283283
*/
284-
public function beforeRules(Event $event, Entity $entity, ArrayObject $options, $operation) {
284+
public function beforeRules(Event $event, EntityInterface $entity, ArrayObject $options, $operation) {
285285
$formField = $this->_config['formField'];
286286
$formFieldRepeat = $this->_config['formFieldRepeat'];
287287
$formFieldCurrent = $this->_config['formFieldCurrent'];
@@ -313,11 +313,12 @@ public function beforeRules(Event $event, Entity $entity, ArrayObject $options,
313313
* Hashing the password and whitelisting
314314
*
315315
* @param \Cake\Event\Event $event
316-
* @param \Cake\ORM\Entity $entity
317-
* @throws \Exception
316+
* @param \Cake\Datasource\EntityInterface $entity
317+
* @param \ArrayObject $options
318+
* @throws \RuntimeException
318319
* @return void
319320
*/
320-
public function beforeSave(Event $event, Entity $entity) {
321+
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
321322
$formField = $this->_config['formField'];
322323
$field = $this->_config['field'];
323324

@@ -329,7 +330,7 @@ public function beforeSave(Event $event, Entity $entity) {
329330
$entity->set($field, $PasswordHasher->hash($entity->get($formField)));
330331

331332
if (!$entity->get($field)) {
332-
throw new Exception('Empty field');
333+
throw new RuntimeException('Empty field');
333334
}
334335

335336
$entity->unsetProperty($formField);

src/Model/Behavior/ResetBehavior.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Cake\Core\Configure;
66
use Cake\ORM\Behavior;
77
use Cake\ORM\Table;
8-
use Exception;
8+
use RuntimeException;
99

1010
/**
1111
* Allows the model to reset all records as batch command.
@@ -93,7 +93,7 @@ public function resetRecords(array $params = []) {
9393
if (!empty($this->_config['fields'])) {
9494
foreach ((array)$this->_config['fields'] as $field) {
9595
if (!$this->_table->hasField($field)) {
96-
throw new Exception('Table does not have field ' . $field);
96+
throw new RuntimeException('Table does not have field ' . $field);
9797
}
9898
}
9999
$defaults['fields'] = array_merge([$this->_table->alias() . '.' . $this->_table->primaryKey()], $this->_config['fields']);
@@ -150,7 +150,7 @@ public function resetRecords(array $params = []) {
150150

151151
$res = $this->_table->save($record, compact('validate', 'fieldList'));
152152
if (!$res) {
153-
throw new Exception(print_r($this->_table->errors(), true));
153+
throw new RuntimeException(print_r($this->_table->errors(), true));
154154
}
155155
$modified++;
156156
}

src/Model/Behavior/SluggedBehavior.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace Tools\Model\Behavior;
44

55
use Cake\Core\Configure;
6+
use Cake\Datasource\EntityInterface;
67
use Cake\Event\Event;
78
use Cake\ORM\Behavior;
8-
use Cake\ORM\Entity;
99
use Cake\ORM\Query;
1010
use Cake\ORM\Table;
1111
use Cake\Utility\Inflector;
12-
use Exception;
1312
use InvalidArgumentException;
13+
use RuntimeException;
1414

1515
/**
1616
* SluggedBehavior
@@ -131,11 +131,11 @@ public function initialize(array $config) {
131131
if (strpos($field, '.')) {
132132
list($alias, $field) = explode('.', $field);
133133
if (!$this->_table->$alias->hasField($field)) {
134-
throw new Exception('(SluggedBehavior::setup) model ' . $this->_table->$alias->name . ' is missing the field ' . $field .
134+
throw new RuntimeException('(SluggedBehavior::setup) model ' . $this->_table->$alias->name . ' is missing the field ' . $field .
135135
' (specified in the setup for model ' . $this->_table->name . ') ');
136136
}
137137
} elseif (!$this->_table->hasField($field) && !method_exists($this->_table->entityClass(), '_get' . Inflector::classify($field))) {
138-
throw new Exception('(SluggedBehavior::setup) model ' . $this->_table->name . ' is missing the field ' . $field . ' specified in the setup.');
138+
throw new RuntimeException('(SluggedBehavior::setup) model ' . $this->_table->name . ' is missing the field ' . $field . ' specified in the setup.');
139139
}
140140
}
141141
}
@@ -161,10 +161,10 @@ public function findSlugged(Query $query, array $options) {
161161
* SluggedBehavior::beforeRules()
162162
*
163163
* @param \Cake\Event\Event $event
164-
* @param \Cake\ORM\Entity $entity
164+
* @param \Cake\Datasource\EntityInterface $entity
165165
* @return void
166166
*/
167-
public function beforeRules(Event $event, Entity $entity) {
167+
public function beforeRules(Event $event, EntityInterface $entity) {
168168
if ($this->_config['on'] === 'beforeRules') {
169169
$this->slug($entity);
170170
}
@@ -174,10 +174,10 @@ public function beforeRules(Event $event, Entity $entity) {
174174
* SluggedBehavior::beforeSave()
175175
*
176176
* @param \Cake\Event\Event $event
177-
* @param \Cake\ORM\Entity $entity
177+
* @param \Cake\Datasource\EntityInterface $entity
178178
* @return void
179179
*/
180-
public function beforeSave(Event $event, Entity $entity) {
180+
public function beforeSave(Event $event, EntityInterface $entity) {
181181
if ($this->_config['on'] === 'beforeSave') {
182182
$this->slug($entity);
183183
}
@@ -186,11 +186,11 @@ public function beforeSave(Event $event, Entity $entity) {
186186
/**
187187
* SluggedBehavior::slug()
188188
*
189-
* @param \Cake\ORM\Entity $entity Entity
189+
* @param \Cake\Datasource\EntityInterface $entity Entity
190190
* @param array $options Options
191191
* @return void
192192
*/
193-
public function slug(Entity $entity, array $options = []) {
193+
public function slug(EntityInterface $entity, array $options = []) {
194194
$overwrite = isset($options['overwrite']) ? $options['overwrite'] : $this->_config['overwrite'];
195195
if (!$overwrite && $entity->get($this->_config['overwriteField'])) {
196196
$overwrite = true;
@@ -216,11 +216,11 @@ public function slug(Entity $entity, array $options = []) {
216216
* of maybe some not in sync slugs anymore (saving the same title again,
217217
* but the slug is completely different, for example).
218218
*
219-
* @param \Cake\ORM\Entity $entity
219+
* @param \Cake\Datasource\EntityInterface $entity
220220
* @param bool $deep If true it will generate a new slug and compare it to the currently stored one.
221221
* @return bool
222222
*/
223-
public function needsSlugUpdate($entity, $deep = false) {
223+
public function needsSlugUpdate(EntityInterface $entity, $deep = false) {
224224
foreach ((array)$this->_config['label'] as $label) {
225225
if ($entity->dirty($label)) {
226226
return true;
@@ -245,10 +245,11 @@ public function needsSlugUpdate($entity, $deep = false) {
245245
* until a unique slug is found
246246
*
247247
* @param string $value
248-
* @param \Cake\ORM\Entity|null $entity
248+
* @param \Cake\Datasource\EntityInterface|null $entity
249249
* @return string A slug
250+
* @throws \RuntimeException
250251
*/
251-
public function generateSlug($value, Entity $entity = null) {
252+
public function generateSlug($value, EntityInterface $entity = null) {
252253
$separator = $this->_config['separator'];
253254

254255
$string = str_replace(["\r\n", "\r", "\n"], ' ', $value);
@@ -300,7 +301,7 @@ public function generateSlug($value, Entity $entity = null) {
300301
}
301302
if ($this->_config['unique']) {
302303
if (!$entity) {
303-
throw new Exception('Needs an Entity to work on');
304+
throw new RuntimeException('Needs an Entity to work on');
304305
}
305306
$field = $this->_table->alias() . '.' . $this->_config['field'];
306307
$conditions = [$field => $slug];
@@ -339,10 +340,11 @@ public function generateSlug($value, Entity $entity = null) {
339340
*
340341
* @param array $params
341342
* @return bool Success
343+
* @throws \RuntimeException
342344
*/
343345
public function resetSlugs($params = []) {
344346
if (!$this->_table->hasField($this->_config['field'])) {
345-
throw new Exception('Table does not have field ' . $this->_config['field']);
347+
throw new RuntimeException('Table does not have field ' . $this->_config['field']);
346348
}
347349
$defaults = [
348350
'page' => 1,
@@ -361,14 +363,15 @@ public function resetSlugs($params = []) {
361363

362364
$this->_table->behaviors()->Slugged->config($params, null, false);
363365
while (($records = $this->_table->find('all', $params)->toArray())) {
366+
/** @var \Cake\ORM\Entity $record */
364367
foreach ($records as $record) {
365368
$record->isNew(true);
366369
$options = [
367370
'validate' => true,
368371
'fieldList' => array_merge([$this->_table->primaryKey(), $this->_config['field']], $this->_config['label'])
369372
];
370373
if (!$this->_table->save($record, $options)) {
371-
throw new Exception(print_r($this->_table->errors(), true));
374+
throw new RuntimeException(print_r($record->errors(), true));
372375
}
373376
}
374377
$params['page']++;
@@ -384,10 +387,10 @@ public function resetSlugs($params = []) {
384387
*
385388
* //FIXME
386389
*
387-
* @param \Cake\ORM\Entity $entity
390+
* @param \Cake\Datasource\EntityInterface $entity
388391
* @return void
389392
*/
390-
protected function _multiSlug(Entity $entity) {
393+
protected function _multiSlug(EntityInterface $entity) {
391394
$label = $this->config('label');
392395
$field = current($label);
393396
$fields = (array)$entity->get($field);

0 commit comments

Comments
 (0)