Skip to content

Commit

Permalink
Attempt to decrease complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
hparadiz committed May 27, 2018
1 parent fae027c commit 8f7878d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
3 changes: 1 addition & 2 deletions src/Controllers/RecordsRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,7 @@ public static function handleMultiDestroyRequest()
foreach ($_REQUEST['data'] as $datum) {
try {
$results[] = static::processDatumDestroy($datum);
}
catch(Exception $e) {
} catch (Exception $e) {
$failed[] = [
'record' => $datum,
'errors' => $e->getMessage(),
Expand Down
55 changes: 32 additions & 23 deletions src/Models/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class ActiveRecord
protected static $_relationshipsDefined = [];
protected static $_eventsDefined = [];

/**
/**
* @var array $_record Raw array data for this model.
*/
protected $_record;
Expand Down Expand Up @@ -186,11 +186,11 @@ class ActiveRecord

/**
* Instantiates a Model and returns.
*
*
* @param array $record Raw array data to start off the model.
* @param bool $isDirty Whether or not to treat this object as if it was modified from the start.
* @param bool $isPhantom Whether or not to treat this object as a brand new record not yet in the database.
*
*
* @return ActiveRecord Instance of the value of $this->Class
*/
public function __construct($record = [], $isDirty = false, $isPhantom = null)
Expand All @@ -216,7 +216,7 @@ public function __construct($record = [], $isDirty = false, $isPhantom = null)

/**
* Passthru to getValue($name)
*
*
* @return mixed The return of $this->getValue($name)
*/
public function __get($name)
Expand All @@ -226,7 +226,7 @@ public function __get($name)

/**
* Passthru to setValue($name,$value)
*
*
* @return mixed The return of $this->setValue($name,$value)
*/
public function __set($name, $value)
Expand All @@ -237,7 +237,7 @@ public function __set($name, $value)

/**
* Is set magic method
*
*
* @return bool Returns true if a value was returned by $this->getValue($name), false otherwise.
*/
public function __isset($name)
Expand All @@ -248,7 +248,7 @@ public function __isset($name)

/**
* Gets the primary key field for his model.
*
*
* @return string ID by default or static::$primaryKey if it's set.
*/
public function getPrimaryKey()
Expand All @@ -258,7 +258,7 @@ public function getPrimaryKey()

/**
* Gets the primary key value for his model.
*
*
* @return int The primary key value for this object.
*/
public function getPrimaryKeyValue()
Expand Down Expand Up @@ -298,7 +298,7 @@ public static function init()

/**
* @param string $name The name of the field you want to get.
*
*
* @return mixed Value of the field you wanted if it exists or null otherwise.
*/
public function getValue($name)
Expand Down Expand Up @@ -430,7 +430,7 @@ public function setField($field, $value)

/**
* Gets normalized object data.
*
*
* @return array The model's data as a normal array with any validation errors included.
*/
public function getData()
Expand Down Expand Up @@ -742,6 +742,26 @@ public static function getAllByWhere($conditions = [], $options = [])
return static::instantiateRecords(static::getAllRecordsByWhere($conditions, $options));
}

public static function buildExtraColumns($columns)
{
if (!empty($columns)) {
if (is_array($columns)) {
foreach ($columns as $key => $value) {
return ', '.$value.' AS '.$key;
}
} else {
return ', ' . $columns;
}
}
}

public static function buildHaving($having)
{
if (!empty($having)) {
return ' HAVING (' . (is_array($having) ? join(') AND (', static::_mapConditions($having)) : $having) . ')';
}
}

public static function getAllRecordsByWhere($conditions = [], $options = [])
{
$className = get_called_class();
Expand All @@ -767,22 +787,11 @@ public static function getAllRecordsByWhere($conditions = [], $options = [])

// build query
$query = 'SELECT %1$s `%3$s`.*';

if (!empty($options['extraColumns'])) {
if (is_array($options['extraColumns'])) {
foreach ($options['extraColumns'] as $key => $value) {
$query .= ', '.$value.' AS '.$key;
}
} else {
$query .= ', ' . $options['extraColumns'];
}
}
$query .= static::buildExtraColumns($options['extraColumns']);
$query .= ' FROM `%2$s` AS `%3$s`';
$query .= ' WHERE (%4$s)';

if (!empty($options['having'])) {
$query .= ' HAVING (' . (is_array($options['having']) ? join(') AND (', static::_mapConditions($options['having'])) : $options['having']) . ')';
}
$query .= static::buildHaving($options['having']);

$params = [
$options['calcFoundRows'] ? 'SQL_CALC_FOUND_ROWS' : '',
Expand Down
20 changes: 10 additions & 10 deletions tests/Divergence/Models/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,15 +1086,15 @@ public function testBeforeSave()
{
$x = relationalCanary::getByID(1);

$myBeforeSave = function($model) {
if(is_a($this, 'Divergence\Tests\Models\ActiveRecordTest')) {
$this->assertEquals(1,$model->ID);
$myBeforeSave = function ($model) {
if (is_a($this, 'Divergence\Tests\Models\ActiveRecordTest')) {
$this->assertEquals(1, $model->ID);
}
};
$scopedBeforeSave = Closure::bind($myBeforeSave,$this);
$scopedBeforeSave = Closure::bind($myBeforeSave, $this);

relationalCanary::setBeforeEvents([
relationalCanary::class => $scopedBeforeSave
relationalCanary::class => $scopedBeforeSave,
]);

$x->beforeSave();
Expand All @@ -1108,15 +1108,15 @@ public function testAfterSave()
{
$x = relationalCanary::getByID(1);

$myAfterSave = function($model) {
if(is_a($this, 'Divergence\Tests\Models\ActiveRecordTest')) {
$this->assertEquals(1,$model->ID);
$myAfterSave = function ($model) {
if (is_a($this, 'Divergence\Tests\Models\ActiveRecordTest')) {
$this->assertEquals(1, $model->ID);
}
};
$scopedAfterSave = Closure::bind($myAfterSave,$this);
$scopedAfterSave = Closure::bind($myAfterSave, $this);

relationalCanary::setAfterEvents([
relationalCanary::class => $scopedAfterSave
relationalCanary::class => $scopedAfterSave,
]);

$x->afterSave();
Expand Down
4 changes: 2 additions & 2 deletions tests/MockSite/Models/Canary.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Canary extends \Divergence\Models\Model
'notnull' => false,
'precision' => '5',
'scale' => '2',
]
],
];


Expand Down Expand Up @@ -182,7 +182,7 @@ public static function avis()
'HighestRecordedAltitude' => mt_rand(70, 2000),
'ObservationCount' => 1,
'DateOfBirth' => mt_rand(time()-315360000, time()),
'Weight' => (mt_rand (10, 200) / 10)
'Weight' => (mt_rand(10, 200) / 10),
];

$output['Handle'] = static::getUniqueHandle($output['Name']);
Expand Down

0 comments on commit 8f7878d

Please sign in to comment.