Applied fixes from StyleCI

This commit is contained in:
Casper Rasmussen 2016-07-28 05:56:24 -04:00 committed by StyleCI Bot
parent d225edabb2
commit f8d6534fcc
13 changed files with 79 additions and 95 deletions

View File

@ -1,4 +1,5 @@
<?php
namespace Nodes\CounterCache;
use Illuminate\Database\Eloquent\Model as IlluminateModel;
@ -11,20 +12,17 @@ use Nodes\CounterCache\Exceptions\NotCounterCacheableException;
use Nodes\CounterCache\Exceptions\RelationNotFoundException;
/**
* Class CounterCache
*
* @package Nodes\CounterCache
* Class CounterCache.
*/
class CounterCache
{
/**
* Perform counter caching on model
* Perform counter caching on model.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @param \Illuminate\Database\Eloquent\Model $model
* @return boolean
* @return bool
* @throws \Nodes\CounterCache\Exceptions\NoCounterCachesFound
* @throws \Nodes\CounterCache\Exceptions\NotCounterCacheableException
* @throws \Nodes\CounterCache\Exceptions\RelationNotFoundException
@ -33,7 +31,7 @@ class CounterCache
{
// If model does not implement the CounterCacheable
// interface, we'll jump ship and abort.
if (!$model instanceof CounterCacheable) {
if (! $model instanceof CounterCacheable) {
Log::error(sprintf('[%s] Model [%s] does not implement CounterCacheable.', __CLASS__, get_class($model)));
throw new NotCounterCacheableException(sprintf('Model [%s] does not implement CounterCacheable.', __CLASS__, get_class($model)));
}
@ -54,11 +52,11 @@ class CounterCache
foreach ((array) $relations as $relationName => $counterCacheConditions) {
// Sometimes our counter cache might require additional conditions
// which means, we need to support both scenarios
$relationName = !is_array($counterCacheConditions) ? $counterCacheConditions : $relationName;
$relationName = ! is_array($counterCacheConditions) ? $counterCacheConditions : $relationName;
// When we've figured out the name of our relation
// we'll just make a quick validation, that it actually exists
if (!method_exists($model, $relationName)) {
if (! method_exists($model, $relationName)) {
Log::error(sprintf('[%s] Relation [%s] was not found on model [%s]', __CLASS__, $relationName, get_class($model)));
throw new RelationNotFoundException(sprintf('Relation [%s] was not found on model [%s]', __CLASS__, $relationName, get_class($model)));
}
@ -71,7 +69,7 @@ class CounterCache
// If our model's foreign key has been updated,
// we need to update the counter cache for the previous value as well
if (!is_null($model->getOriginal($relation->getForeignKey())) && $model->getOriginal($relation->getForeignKey()) != $model->getAttribute($relation->getForeignKey())) {
if (! is_null($model->getOriginal($relation->getForeignKey())) && $model->getOriginal($relation->getForeignKey()) != $model->getAttribute($relation->getForeignKey())) {
// Retrieve original foreign key
$originalForeignKey = $model->getOriginal($relation->getForeignKey());
@ -88,13 +86,12 @@ class CounterCache
}
/**
* Perform counter caching on all entities of model
* Perform counter caching on all entities of model.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @param \Illuminate\Database\Eloquent\Model $model
* @return boolean
* @return bool
* @throws \Nodes\CounterCache\Exceptions\NoEntitiesFoundException
* @throws \Nodes\CounterCache\Exceptions\NoCounterCachesFound
* @throws \Nodes\CounterCache\Exceptions\NotCounterCacheableException
@ -107,7 +104,7 @@ class CounterCache
// If no entities found, we'll log the error,
// throw an exception and abort.
if (!$entities->isEmpty()) {
if (! $entities->isEmpty()) {
Log::error(sprintf('[%s] No entities found of model [%s]', __CLASS__, get_class($model)));
throw new NoEntitiesFoundException(sprintf('No entities found of model [%s]', get_class($model)));
}
@ -121,17 +118,16 @@ class CounterCache
}
/**
* Update counter cache column
* Update counter cache column.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access protected
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation
* @param array|null $counterCacheConditions
* @param string $foreignKey
* @param string $counterCacheColumnName
* @return boolean
* @return bool
*/
protected function updateCount(IlluminateModel $model, IlluminateRelation $relation, $counterCacheConditions, $foreignKey, $counterCacheColumnName)
{
@ -166,21 +162,20 @@ class CounterCache
// Fire the update query
// to update counter cache column
return (bool) $relation->getBaseQuery()->update([
sprintf('%s.%s', $relationTableName, $counterCacheColumnName) => DB::raw(sprintf('(%s)', vsprintf($countQuerySql, $countQuery->getBindings())))
sprintf('%s.%s', $relationTableName, $counterCacheColumnName) => DB::raw(sprintf('(%s)', vsprintf($countQuerySql, $countQuery->getBindings()))),
]);
}
/**
* Prepare value for SQL insertion
* Prepare value for SQL insertion.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @param string $value
* @return integer|string
* @return int|string
*/
private function prepareValue($value)
{
return is_numeric($value) ? $value : sprintf('"%s"', $value);
}
}
}

View File

@ -1,21 +1,20 @@
<?php
namespace Nodes\CounterCache;
/**
* Interface CounterCacheable
* Interface CounterCacheable.
*
* @interface
* @package Nodes\CounterCache
*/
interface CounterCacheable
{
/**
* Retrieve array of counter caches
* Retrieve array of counter caches.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @return array
*/
public function counterCaches();
}
}

View File

@ -1,29 +1,27 @@
<?php
namespace Nodes\CounterCache\Exceptions;
use Nodes\Exceptions\Exception as NodesException;
/**
* Class CounterCacheException
*
* @package Nodes\CounterCache\Exceptions
* Class CounterCacheException.
*/
class CounterCacheException extends NodesException
{
/**
* CounterCacheException constructor
* CounterCacheException constructor.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @param string $message
* @param integer $code
* @param int $code
* @param array $headers
* @param boolean $report
* @param bool $report
* @param string $severity
*/
public function __construct($message = 'Counter cache failed', $code = 500, array $headers = [], $report = true, $severity = 'error')
{
parent::__construct($message, $code, $headers, $report, $severity);
}
}
}

View File

@ -1,27 +1,25 @@
<?php
namespace Nodes\CounterCache\Exceptions;
/**
* Class NoCounterCachesFound
*
* @package Nodes\CounterCache\Exceptions
* Class NoCounterCachesFound.
*/
class NoCounterCachesFound extends CounterCacheException
class NoCounterCachesFoundException extends CounterCacheException
{
/**
* NoCounterCachesFound constructor
* NoCounterCachesFound constructor.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @param string $message
* @param integer $code
* @param int $code
* @param array $headers
* @param boolean $report
* @param bool $report
* @param string $severity
*/
public function __construct($message = 'No counter caches found on model', $code = 500, array $headers = [], $report = true, $severity = 'error')
{
parent::__construct($message, $code, $headers, $report, $severity);
}
}
}

View File

@ -1,27 +1,25 @@
<?php
namespace Nodes\CounterCache\Exceptions;
/**
* Class NoEntitiesFoundException
*
* @package Nodes\CounterCache\Exceptions
* Class NoEntitiesFoundException.
*/
class NoEntitiesFoundException extends CounterCacheException
{
/**
* NoEntitiesFoundException constructor
* NoEntitiesFoundException constructor.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @param string $message
* @param integer $code
* @param int $code
* @param array $headers
* @param boolean $report
* @param bool $report
* @param string $severity
*/
public function __construct($message = 'No entities found', $code = 500, array $headers = [], $report = true, $severity = 'error')
{
parent::__construct($message, $code, $headers, $report, $severity);
}
}
}

View File

@ -1,27 +1,25 @@
<?php
namespace Nodes\CounterCache\Exceptions;
/**
* Class NotCounterCacheableException
*
* @package Nodes\CounterCache\Exceptions
* Class NotCounterCacheableException.
*/
class NotCounterCacheableException extends CounterCacheException
{
/**
* NotCounterCacheableException constructor
* NotCounterCacheableException constructor.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @param string $message
* @param integer $code
* @param int $code
* @param array $headers
* @param boolean $report
* @param bool $report
* @param string $severity
*/
public function __construct($message = 'Model does not implement CounterCacheable', $code = 500, array $headers = [], $report = true, $severity = 'error')
{
parent::__construct($message, $code, $headers, $report, $severity);
}
}
}

View File

@ -1,27 +1,25 @@
<?php
namespace Nodes\CounterCache\Exceptions;
/**
* Class RelationNotFoundException
*
* @package Nodes\CounterCache\Exceptions
* Class RelationNotFoundException.
*/
class RelationNotFoundException extends CounterCacheException
{
/**
* RelationNotFoundException constructor
* RelationNotFoundException constructor.
*
* @author Morten Rugaard <moru@nodes.dk>
*
* @access public
* @param string $message
* @param integer $code
* @param int $code
* @param array $headers
* @param boolean $report
* @param bool $report
* @param string $severity
*/
public function __construct($message = 'Relation not found on model', $code = 500, array $headers = [], $report = true, $severity = 'error')
{
parent::__construct($message, $code, $headers, $report, $severity);
}
}
}

View File

@ -1,15 +1,15 @@
<?php
namespace Nodes\CounterCache\Traits;
/**
* Trait CounterCache
* Trait CounterCache.
*
* @trait
* @package Nodes\CounterCache\Traits
*/
trait CounterCache
{
use CounterCacheSaved,
CounterCacheDeleted,
CounterCacheRestored;
}
}

View File

@ -1,16 +1,16 @@
<?php
namespace Nodes\CounterCache\Traits;
/**
* Trait CounterCacheCreated
* Trait CounterCacheCreated.
*
* @trait
* @package Nodes\CounterCache\Traits
*/
trait CounterCacheCreated
{
/**
* The "booting" of trait
* The "booting" of trait.
*
* @author Morten Rugaard <moru@nodes.dk>
*
@ -19,8 +19,8 @@ trait CounterCacheCreated
*/
public static function bootCounterCacheCreated()
{
static::created(function($model) {
static::created(function ($model) {
app('Nodes\CounterCache\CounterCache')->count($model);
});
}
}
}

View File

@ -1,16 +1,16 @@
<?php
namespace Nodes\CounterCache\Traits;
/**
* Trait CounterCacheDeleted
* Trait CounterCacheDeleted.
*
* @trait
* @package Nodes\CounterCache\Traits
*/
trait CounterCacheDeleted
{
/**
* The "booting" of trait
* The "booting" of trait.
*
* @author Morten Rugaard <moru@nodes.dk>
*
@ -19,8 +19,8 @@ trait CounterCacheDeleted
*/
public static function bootCounterCacheDeleted()
{
static::deleted(function($model) {
static::deleted(function ($model) {
app('Nodes\CounterCache\CounterCache')->count($model);
});
}
}
}

View File

@ -1,16 +1,16 @@
<?php
namespace Nodes\CounterCache\Traits;
/**
* Trait CounterCacheRestored
* Trait CounterCacheRestored.
*
* @trait
* @package Nodes\CounterCache\Traits
*/
trait CounterCacheRestored
{
/**
* The "booting" of trait
* The "booting" of trait.
*
* @author Morten Rugaard <moru@nodes.dk>
*
@ -25,4 +25,4 @@ trait CounterCacheRestored
});
}
}
}
}

View File

@ -1,16 +1,16 @@
<?php
namespace Nodes\CounterCache\Traits;
/**
* Trait CounterCacheSaved
* Trait CounterCacheSaved.
*
* @trait
* @package Nodes\CounterCache\Traits
*/
trait CounterCacheSaved
{
/**
* The "booting" of trait
* The "booting" of trait.
*
* @author Morten Rugaard <moru@nodes.dk>
*
@ -19,8 +19,8 @@ trait CounterCacheSaved
*/
public static function bootCounterCacheSaved()
{
static::saved(function($model) {
static::saved(function ($model) {
app('Nodes\CounterCache\CounterCache')->count($model);
});
}
}
}

View File

@ -1,16 +1,16 @@
<?php
namespace Nodes\CounterCache\Traits;
/**
* Trait CounterCacheUpdated
* Trait CounterCacheUpdated.
*
* @trait
* @package Nodes\CounterCache\Traits
*/
trait CounterCacheUpdated
{
/**
* The "booting" of trait
* The "booting" of trait.
*
* @author Morten Rugaard <moru@nodes.dk>
*
@ -19,8 +19,8 @@ trait CounterCacheUpdated
*/
public static function bootCounterCacheUpdated()
{
static::updated(function($model) {
static::updated(function ($model) {
app('Nodes\CounterCache\CounterCache')->count($model);
});
}
}
}