Merge pull request #1110 from cachethq/filter

Fixed up array_filter stuff
This commit is contained in:
Graham Campbell
2015-11-21 22:03:51 +00:00
4 changed files with 66 additions and 31 deletions

View File

@@ -26,21 +26,34 @@ class AddComponentCommandHandler
*/
public function handle(AddComponentCommand $command)
{
$componentData = array_filter([
'name' => $command->name,
'description' => $command->description,
'link' => $command->link,
'status' => $command->status,
'order' => $command->order,
'group_id' => $command->group_id,
]);
$componentData['enabled'] = $command->enabled;
$component = Component::create($componentData);
$component = Component::create($this->filter($command));
event(new ComponentWasAddedEvent($component));
return $component;
}
/**
* Filter the command data.
*
* @param \CachetHQ\Cachet\Commands\Incident\AddComponentCommand $command
*
* @return array
*/
protected function filter(AddComponentCommand $command)
{
$params = [
'name' => $command->name,
'description' => $command->description,
'link' => $command->link,
'status' => $command->status,
'enabled' => $command->enabled,
'order' => $command->order,
'group_id' => $command->group_id,
];
return array_filter($params, function ($val) {
return $val !== null;
});
}
}

View File

@@ -27,21 +27,35 @@ class UpdateComponentCommandHandler
public function handle(UpdateComponentCommand $command)
{
$component = $command->component;
$componentData = array_filter([
'name' => $command->name,
'description' => $command->description,
'link' => $command->link,
'status' => $command->status,
'order' => $command->order,
'group_id' => $command->group_id,
]);
$componentData['enabled'] = $command->enabled;
$component->update($componentData);
$component->update($this->filter($command));
event(new ComponentWasUpdatedEvent($component));
return $component;
}
/**
* Filter the command data.
*
* @param \CachetHQ\Cachet\Commands\Incident\UpdateComponentCommand $command
*
* @return array
*/
protected function filter(UpdateComponentCommand $command)
{
$params = [
'name' => $command->name,
'description' => $command->description,
'link' => $command->link,
'status' => $command->status,
'enabled' => $command->enabled,
'order' => $command->order,
'group_id' => $command->group_id,
];
return array_filter($params, function ($val) {
return $val !== null;
});
}
}

View File

@@ -55,7 +55,7 @@ class UpdateIncidentCommandHandler
}
$incident = $command->incident;
$incident->update($this->filterIncidentData($command));
$incident->update($this->filter($command));
// The incident occurred at a different time.
if ($command->incident_date) {
@@ -86,9 +86,9 @@ class UpdateIncidentCommandHandler
*
* @return array
*/
protected function filterIncidentData($command)
protected function filter(UpdateIncidentCommand $command)
{
return array_filter([
$params = [
'name' => $command->name,
'status' => $command->status,
'message' => $command->message,
@@ -96,7 +96,11 @@ class UpdateIncidentCommandHandler
'component_id' => $command->component_id,
'component_status' => $command->component_status,
'notify' => $command->notify,
]);
];
return array_filter($params, function ($val) {
return $val !== null;
});
}
/**

View File

@@ -28,7 +28,7 @@ class UpdateMetricCommandHandler
{
$metric = $command->metric;
$metric->update($this->filterMetricData($command));
$metric->update($this->filter($command));
event(new MetricWasUpdatedEvent($metric));
@@ -42,9 +42,9 @@ class UpdateMetricCommandHandler
*
* @return array
*/
protected function filterMetricData($command)
protected function filter(UpdateMetricCommand $command)
{
return array_filter([
$params = [
'name' => $command->name,
'suffix' => $command->suffix,
'description' => $command->description,
@@ -52,6 +52,10 @@ class UpdateMetricCommandHandler
'calc_type' => $command->calc_type,
'display_chart' => $command->display_chart,
'places' => $command->places,
]);
];
return array_filter($params, function ($val) {
return $val !== null;
});
}
}