Skip to content

Commit

Permalink
Merge pull request #179 from dereuromark/improvements
Browse files Browse the repository at this point in the history
Use entity interface and more specific exception.
  • Loading branch information
dereuromark authored Mar 18, 2017
2 parents 6d3bbce + 363b8bd commit 00f9829
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"test": "php phpunit.phar",
"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit.phar",
"test-coverage": "php phpunit.phar --log-junit webroot/coverage/unitreport.xml --coverage-html webroot/coverage --coverage-clover webroot/coverage/coverage.xml",
"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 ./",
"cs-check": "phpcs -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/cakephp-tools/vendor/,/tmp/,/logs/,/tests/test_files/ --extensions=php ./",
"cs-fix": "phpcbf -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/cakephp-tools/vendor/,/tmp/,/logs/,/tests/test_files --extensions=php ./"
}
}
18 changes: 9 additions & 9 deletions src/Model/Behavior/BitmaskedBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Tools\Model\Behavior;

use ArrayObject;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\Utility\Inflector;
use Exception;
use RuntimeException;
use Tools\Utility\Text;

/**
Expand Down Expand Up @@ -68,7 +68,7 @@ public function initialize(array $config = []) {
$config['bits'] = false;
}
if (empty($config['bits'])) {
throw new Exception('Bits not found');
throw new RuntimeException('Bits not found');
}
ksort($config['bits'], SORT_NUMERIC);

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

/**
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @param \ArrayObject $options
* @return void
*/
public function beforeRules(Event $event, Entity $entity, ArrayObject $options) {
public function beforeRules(Event $event, EntityInterface $entity, ArrayObject $options) {
if ($this->_config['on'] !== 'beforeRules' || !$options['checkRules']) {
return;
}
Expand All @@ -110,11 +110,11 @@ public function beforeRules(Event $event, Entity $entity, ArrayObject $options)

/**
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @param \ArrayObject $options
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
if ($this->_config['on'] !== 'beforeSave') {
return;
}
Expand Down Expand Up @@ -190,10 +190,10 @@ public function encodeBitmaskConditions(Query $query) {
}

/**
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @return void
*/
public function encodeBitmaskData(Entity $entity) {
public function encodeBitmaskData(EntityInterface $entity) {
$field = $this->_config['field'];
if (!($mappedField = $this->_config['mappedField'])) {
$mappedField = $field;
Expand Down
17 changes: 9 additions & 8 deletions src/Model/Behavior/JsonableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use ArrayObject;
use Cake\Database\Type;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Exception;
use RuntimeException;
use Tools\Database\Type\ArrayType;
use Tools\Utility\Text;

Expand Down Expand Up @@ -71,12 +72,12 @@ class JsonableBehavior extends Behavior {

/**
* @param array $config
* @throws \Exception
* @throws \RuntimeException
* @return void
*/
public function initialize(array $config = []) {
if (empty($this->_config['fields'])) {
throw new Exception('Fields are required');
throw new RuntimeException('Fields are required');
}
if (!is_array($this->_config['fields'])) {
$this->_config['fields'] = (array)$this->_config['fields'];
Expand All @@ -85,7 +86,7 @@ public function initialize(array $config = []) {
$this->_config['map'] = (array)$this->_config['map'];
}
if (!empty($this->_config['map']) && count($this->_config['fields']) !== count($this->_config['map'])) {
throw new Exception('Fields and Map need to be of the same length if map is specified.');
throw new RuntimeException('Fields and Map need to be of the same length if map is specified.');
}
foreach ($this->_config['fields'] as $field) {
$this->_table->schema()->columnType($field, 'array');
Expand Down Expand Up @@ -121,10 +122,10 @@ public function beforeFind(Event $event, Query $query) {
/**
* Decodes the fields of an array/entity (if the value itself was encoded)
*
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @return void
*/
public function decodeItems(Entity $entity) {
public function decodeItems(EntityInterface $entity) {
$fields = $this->_getMappedFields();

foreach ($fields as $map => $field) {
Expand All @@ -139,11 +140,11 @@ public function decodeItems(Entity $entity) {
* Saves all fields that do not belong to the current Model into 'with' helper model.
*
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @param \ArrayObject $options
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
$fields = $this->_getMappedFields();

foreach ($fields as $map => $field) {
Expand Down
19 changes: 10 additions & 9 deletions src/Model/Behavior/PasswordableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use ArrayObject;
use Cake\Auth\PasswordHasherFactory;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Exception;
use RuntimeException;

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

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

$rules = $this->_validationRules;
Expand Down Expand Up @@ -276,12 +276,12 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti
* Preparing the data
*
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @param \ArrayObject $options
* @param string $operation
* @return void
*/
public function beforeRules(Event $event, Entity $entity, ArrayObject $options, $operation) {
public function beforeRules(Event $event, EntityInterface $entity, ArrayObject $options, $operation) {
$formField = $this->_config['formField'];
$formFieldRepeat = $this->_config['formFieldRepeat'];
$formFieldCurrent = $this->_config['formFieldCurrent'];
Expand Down Expand Up @@ -313,11 +313,12 @@ public function beforeRules(Event $event, Entity $entity, ArrayObject $options,
* Hashing the password and whitelisting
*
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Entity $entity
* @throws \Exception
* @param \Cake\Datasource\EntityInterface $entity
* @param \ArrayObject $options
* @throws \RuntimeException
* @return void
*/
public function beforeSave(Event $event, Entity $entity) {
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
$formField = $this->_config['formField'];
$field = $this->_config['field'];

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

if (!$entity->get($field)) {
throw new Exception('Empty field');
throw new RuntimeException('Empty field');
}

$entity->unsetProperty($formField);
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Behavior/ResetBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Cake\Core\Configure;
use Cake\ORM\Behavior;
use Cake\ORM\Table;
use Exception;
use RuntimeException;

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

$res = $this->_table->save($record, compact('validate', 'fieldList'));
if (!$res) {
throw new Exception(print_r($this->_table->errors(), true));
throw new RuntimeException(print_r($this->_table->errors(), true));
}
$modified++;
}
Expand Down
41 changes: 22 additions & 19 deletions src/Model/Behavior/SluggedBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Tools\Model\Behavior;

use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use Exception;
use InvalidArgumentException;
use RuntimeException;

/**
* SluggedBehavior
Expand Down Expand Up @@ -131,11 +131,11 @@ public function initialize(array $config) {
if (strpos($field, '.')) {
list($alias, $field) = explode('.', $field);
if (!$this->_table->$alias->hasField($field)) {
throw new Exception('(SluggedBehavior::setup) model ' . $this->_table->$alias->name . ' is missing the field ' . $field .
throw new RuntimeException('(SluggedBehavior::setup) model ' . $this->_table->$alias->name . ' is missing the field ' . $field .
' (specified in the setup for model ' . $this->_table->name . ') ');
}
} elseif (!$this->_table->hasField($field) && !method_exists($this->_table->entityClass(), '_get' . Inflector::classify($field))) {
throw new Exception('(SluggedBehavior::setup) model ' . $this->_table->name . ' is missing the field ' . $field . ' specified in the setup.');
throw new RuntimeException('(SluggedBehavior::setup) model ' . $this->_table->name . ' is missing the field ' . $field . ' specified in the setup.');
}
}
}
Expand All @@ -161,10 +161,10 @@ public function findSlugged(Query $query, array $options) {
* SluggedBehavior::beforeRules()
*
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @return void
*/
public function beforeRules(Event $event, Entity $entity) {
public function beforeRules(Event $event, EntityInterface $entity) {
if ($this->_config['on'] === 'beforeRules') {
$this->slug($entity);
}
Expand All @@ -174,10 +174,10 @@ public function beforeRules(Event $event, Entity $entity) {
* SluggedBehavior::beforeSave()
*
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @return void
*/
public function beforeSave(Event $event, Entity $entity) {
public function beforeSave(Event $event, EntityInterface $entity) {
if ($this->_config['on'] === 'beforeSave') {
$this->slug($entity);
}
Expand All @@ -186,11 +186,11 @@ public function beforeSave(Event $event, Entity $entity) {
/**
* SluggedBehavior::slug()
*
* @param \Cake\ORM\Entity $entity Entity
* @param \Cake\Datasource\EntityInterface $entity Entity
* @param array $options Options
* @return void
*/
public function slug(Entity $entity, array $options = []) {
public function slug(EntityInterface $entity, array $options = []) {
$overwrite = isset($options['overwrite']) ? $options['overwrite'] : $this->_config['overwrite'];
if (!$overwrite && $entity->get($this->_config['overwriteField'])) {
$overwrite = true;
Expand All @@ -216,11 +216,11 @@ public function slug(Entity $entity, array $options = []) {
* of maybe some not in sync slugs anymore (saving the same title again,
* but the slug is completely different, for example).
*
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @param bool $deep If true it will generate a new slug and compare it to the currently stored one.
* @return bool
*/
public function needsSlugUpdate($entity, $deep = false) {
public function needsSlugUpdate(EntityInterface $entity, $deep = false) {
foreach ((array)$this->_config['label'] as $label) {
if ($entity->dirty($label)) {
return true;
Expand All @@ -245,10 +245,11 @@ public function needsSlugUpdate($entity, $deep = false) {
* until a unique slug is found
*
* @param string $value
* @param \Cake\ORM\Entity|null $entity
* @param \Cake\Datasource\EntityInterface|null $entity
* @return string A slug
* @throws \RuntimeException
*/
public function generateSlug($value, Entity $entity = null) {
public function generateSlug($value, EntityInterface $entity = null) {
$separator = $this->_config['separator'];

$string = str_replace(["\r\n", "\r", "\n"], ' ', $value);
Expand Down Expand Up @@ -300,7 +301,7 @@ public function generateSlug($value, Entity $entity = null) {
}
if ($this->_config['unique']) {
if (!$entity) {
throw new Exception('Needs an Entity to work on');
throw new RuntimeException('Needs an Entity to work on');
}
$field = $this->_table->alias() . '.' . $this->_config['field'];
$conditions = [$field => $slug];
Expand Down Expand Up @@ -339,10 +340,11 @@ public function generateSlug($value, Entity $entity = null) {
*
* @param array $params
* @return bool Success
* @throws \RuntimeException
*/
public function resetSlugs($params = []) {
if (!$this->_table->hasField($this->_config['field'])) {
throw new Exception('Table does not have field ' . $this->_config['field']);
throw new RuntimeException('Table does not have field ' . $this->_config['field']);
}
$defaults = [
'page' => 1,
Expand All @@ -361,14 +363,15 @@ public function resetSlugs($params = []) {

$this->_table->behaviors()->Slugged->config($params, null, false);
while (($records = $this->_table->find('all', $params)->toArray())) {
/** @var \Cake\ORM\Entity $record */
foreach ($records as $record) {
$record->isNew(true);
$options = [
'validate' => true,
'fieldList' => array_merge([$this->_table->primaryKey(), $this->_config['field']], $this->_config['label'])
];
if (!$this->_table->save($record, $options)) {
throw new Exception(print_r($this->_table->errors(), true));
throw new RuntimeException(print_r($record->errors(), true));
}
}
$params['page']++;
Expand All @@ -384,10 +387,10 @@ public function resetSlugs($params = []) {
*
* //FIXME
*
* @param \Cake\ORM\Entity $entity
* @param \Cake\Datasource\EntityInterface $entity
* @return void
*/
protected function _multiSlug(Entity $entity) {
protected function _multiSlug(EntityInterface $entity) {
$label = $this->config('label');
$field = current($label);
$fields = (array)$entity->get($field);
Expand Down
Loading

0 comments on commit 00f9829

Please sign in to comment.