Skip to content

Commit

Permalink
Add rules to requests and make naming explicit (#169)
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Butcher <[email protected]>
Co-authored-by: James Brooks <[email protected]>
  • Loading branch information
joelbutcher and jbrooksuk authored Jan 8, 2025
1 parent b3a5388 commit 88960af
Show file tree
Hide file tree
Showing 70 changed files with 514 additions and 334 deletions.
4 changes: 2 additions & 2 deletions src/Actions/Component/CreateComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Cachet\Actions\Component;

use Cachet\Data\Component\CreateComponentData;
use Cachet\Data\Requests\Component\CreateComponentRequestData;
use Cachet\Models\Component;

class CreateComponent
{
/**
* Handle the action.
*/
public function handle(CreateComponentData $component): Component
public function handle(CreateComponentRequestData $component): Component
{
return Component::create($component->toArray());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/Component/UpdateComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cachet\Actions\Component;

use Cachet\Data\Component\UpdateComponentData;
use Cachet\Data\Requests\Component\UpdateComponentRequestData;
use Cachet\Events\Components\ComponentStatusWasChanged;
use Cachet\Models\Component;

Expand All @@ -11,7 +11,7 @@ class UpdateComponent
/**
* Handle the action.
*/
public function handle(Component $component, UpdateComponentData $data): Component
public function handle(Component $component, UpdateComponentRequestData $data): Component
{
$oldStatus = $component->status;

Expand Down
4 changes: 2 additions & 2 deletions src/Actions/ComponentGroup/CreateComponentGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cachet\Actions\ComponentGroup;

use Cachet\Data\ComponentGroup\CreateComponentGroupData;
use Cachet\Data\Requests\ComponentGroup\CreateComponentGroupRequestData;
use Cachet\Models\Component;
use Cachet\Models\ComponentGroup;

Expand All @@ -14,7 +14,7 @@ class CreateComponentGroup
/**
* Handle the action.
*/
public function handle(CreateComponentGroupData $data): ComponentGroup
public function handle(CreateComponentGroupRequestData $data): ComponentGroup
{
return tap(ComponentGroup::create(
$data->except('components')->toArray(),
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/ComponentGroup/UpdateComponentGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cachet\Actions\ComponentGroup;

use Cachet\Data\ComponentGroup\UpdateComponentGroupData;
use Cachet\Data\Requests\ComponentGroup\UpdateComponentGroupRequestData;
use Cachet\Models\Component;
use Cachet\Models\ComponentGroup;

Expand All @@ -11,7 +11,7 @@ class UpdateComponentGroup
/**
* Handle the action.
*/
public function handle(ComponentGroup $componentGroup, UpdateComponentGroupData $data): ComponentGroup
public function handle(ComponentGroup $componentGroup, UpdateComponentGroupRequestData $data): ComponentGroup
{
$componentGroup->update($data->except('components')->toArray());

Expand Down
6 changes: 3 additions & 3 deletions src/Actions/Incident/CreateIncident.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cachet\Actions\Incident;

use Cachet\Data\Incident\CreateIncidentData;
use Cachet\Data\Requests\Incident\CreateIncidentRequestData;
use Cachet\Models\Component;
use Cachet\Models\Incident;
use Cachet\Models\IncidentTemplate;
Expand All @@ -14,7 +14,7 @@ class CreateIncident
/**
* Handle the action.
*/
public function handle(CreateIncidentData $data): Incident
public function handle(CreateIncidentRequestData $data): Incident
{
if (isset($data->template)) {
$template = IncidentTemplate::query()
Expand All @@ -35,7 +35,7 @@ public function handle(CreateIncidentData $data): Incident
/**
* Render the incident template with the given data.
*/
private function parseTemplate(IncidentTemplate $template, CreateIncidentData $data): string
private function parseTemplate(IncidentTemplate $template, CreateIncidentRequestData $data): string
{
$vars = array_merge($data->templateVars, [
'incident' => [
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/Incident/UpdateIncident.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cachet\Actions\Incident;

use Cachet\Data\Incident\UpdateIncidentData;
use Cachet\Data\Requests\Incident\UpdateIncidentRequestData;
use Cachet\Events\Incidents\IncidentUpdated;
use Cachet\Models\Incident;

Expand All @@ -11,7 +11,7 @@ class UpdateIncident
/**
* Handle the action.
*/
public function handle(Incident $incident, UpdateIncidentData $data): Incident
public function handle(Incident $incident, UpdateIncidentRequestData $data): Incident
{
$incident->update($data->toArray());

Expand Down
4 changes: 2 additions & 2 deletions src/Actions/IncidentTemplate/CreateIncidentTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Cachet\Actions\IncidentTemplate;

use Cachet\Data\IncidentTemplate\CreateIncidentTemplateData;
use Cachet\Data\Requests\IncidentTemplate\CreateIncidentTemplateRequestData;
use Cachet\Models\IncidentTemplate;

class CreateIncidentTemplate
{
/**
* Handle the action.
*/
public function handle(CreateIncidentTemplateData $data): IncidentTemplate
public function handle(CreateIncidentTemplateRequestData $data): IncidentTemplate
{
return IncidentTemplate::create($data->toArray());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/IncidentTemplate/UpdateIncidentTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Cachet\Actions\IncidentTemplate;

use Cachet\Data\IncidentTemplate\UpdateIncidentTemplateData;
use Cachet\Data\Requests\IncidentTemplate\UpdateIncidentTemplateRequestData;
use Cachet\Models\IncidentTemplate;

class UpdateIncidentTemplate
{
/**
* Handle the action.
*/
public function handle(IncidentTemplate $incidentTemplate, UpdateIncidentTemplateData $data): IncidentTemplate
public function handle(IncidentTemplate $incidentTemplate, UpdateIncidentTemplateRequestData $data): IncidentTemplate
{
$incidentTemplate->update($data->toArray());

Expand Down
4 changes: 2 additions & 2 deletions src/Actions/Metric/CreateMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Cachet\Actions\Metric;

use Cachet\Data\Metric\CreateMetricData;
use Cachet\Data\Requests\Metric\CreateMetricRequestData;
use Cachet\Models\Metric;

class CreateMetric
{
/**
* Handle the action.
*/
public function handle(CreateMetricData $data): Metric
public function handle(CreateMetricRequestData $data): Metric
{
return Metric::create($data->toArray());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/Metric/CreateMetricPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cachet\Actions\Metric;

use Cachet\Data\Metric\CreateMetricPointData;
use Cachet\Data\Requests\Metric\CreateMetricPointRequestData;
use Cachet\Models\Metric;
use Cachet\Models\MetricPoint;

Expand All @@ -11,7 +11,7 @@ class CreateMetricPoint
/**
* Handle the action.
*/
public function handle(Metric $metric, ?CreateMetricPointData $data = null): MetricPoint
public function handle(Metric $metric, ?CreateMetricPointRequestData $data = null): MetricPoint
{
$lastPoint = $metric->metricPoints()->latest()->first();

Expand Down
4 changes: 2 additions & 2 deletions src/Actions/Metric/UpdateMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Cachet\Actions\Metric;

use Cachet\Data\Metric\UpdateMetricData;
use Cachet\Data\Requests\Metric\UpdateMetricRequestData;
use Cachet\Models\Metric;

class UpdateMetric
{
/**
* Handle the action.
*/
public function handle(Metric $metric, UpdateMetricData $data): Metric
public function handle(Metric $metric, UpdateMetricRequestData $data): Metric
{
$metric->update($data->toArray());

Expand Down
8 changes: 4 additions & 4 deletions src/Actions/Schedule/CreateSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace Cachet\Actions\Schedule;

use Cachet\Data\Schedule\CreateScheduleData;
use Cachet\Data\Schedule\ScheduleComponentData;
use Cachet\Data\Requests\Schedule\CreateScheduleRequestData;
use Cachet\Data\Requests\Schedule\ScheduleComponentRequestData;
use Cachet\Models\Schedule;

class CreateSchedule
{
/**
* Handle the action.
*/
public function handle(CreateScheduleData $data): Schedule
public function handle(CreateScheduleRequestData $data): Schedule
{
return tap(Schedule::create($data->except('components')->toArray()), function (Schedule $schedule) use ($data) {
if (! $data->components) {
return;
}

$components = collect($data->components)->map(fn (ScheduleComponentData $component) => [
$components = collect($data->components)->map(fn (ScheduleComponentRequestData $component) => [
'component_id' => $component->id,
'component_status' => $component->status,
])->all();
Expand Down
8 changes: 4 additions & 4 deletions src/Actions/Schedule/UpdateSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

namespace Cachet\Actions\Schedule;

use Cachet\Data\Schedule\ScheduleComponentData;
use Cachet\Data\Schedule\UpdateScheduleData;
use Cachet\Data\Requests\Schedule\ScheduleComponentRequestData;
use Cachet\Data\Requests\Schedule\UpdateScheduleRequestData;
use Cachet\Models\Schedule;

class UpdateSchedule
{
/**
* Handle the action.
*/
public function handle(Schedule $schedule, UpdateScheduleData $data): Schedule
public function handle(Schedule $schedule, UpdateScheduleRequestData $data): Schedule
{
$schedule->update($data->except('components')->toArray());

if ($data->components) {
$components = collect($data->components)->map(fn (ScheduleComponentData $component) => [
$components = collect($data->components)->map(fn (ScheduleComponentRequestData $component) => [
'component_id' => $component->id,
'component_status' => $component->status,
])->all();
Expand Down
6 changes: 3 additions & 3 deletions src/Actions/Update/CreateUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Cachet\Actions\Update;

use Cachet\Data\IncidentUpdate\CreateIncidentUpdateData;
use Cachet\Data\ScheduleUpdate\CreateScheduleUpdateData;
use Cachet\Data\Requests\IncidentUpdate\CreateIncidentUpdateRequestData;
use Cachet\Data\Requests\ScheduleUpdate\CreateScheduleUpdateRequestData;
use Cachet\Models\Incident;
use Cachet\Models\Schedule;
use Cachet\Models\Update;
Expand All @@ -13,7 +13,7 @@ class CreateUpdate
/**
* Handle the action.
*/
public function handle(Incident|Schedule $resource, CreateIncidentUpdateData|CreateScheduleUpdateData $data): Update
public function handle(Incident|Schedule $resource, CreateIncidentUpdateRequestData|CreateScheduleUpdateRequestData $data): Update
{
$update = new Update(array_merge(['user_id' => auth()->id()], $data->toArray()));

Expand Down
6 changes: 3 additions & 3 deletions src/Actions/Update/EditUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Cachet\Actions\Update;

use Cachet\Data\IncidentUpdate\EditIncidentUpdateData;
use Cachet\Data\ScheduleUpdate\EditScheduleUpdateData;
use Cachet\Data\Requests\IncidentUpdate\EditIncidentUpdateRequestData;
use Cachet\Data\Requests\ScheduleUpdate\EditScheduleUpdateRequestData;
use Cachet\Models\Update;

class EditUpdate
{
/**
* Handle the action.
*/
public function handle(Update $update, EditIncidentUpdateData|EditScheduleUpdateData $data): Update
public function handle(Update $update, EditIncidentUpdateRequestData|EditScheduleUpdateRequestData $data): Update
{
return tap($update, function (Update $update) use ($data) {
$update->update($data->toArray());
Expand Down
9 changes: 9 additions & 0 deletions src/Data/BaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

use Spatie\LaravelData\Data;

/**
* @template TKey of array-key
* @template TValue
*/
abstract class BaseData extends Data
{
/**
* Get the instance as an array.
*
* @return array<TKey, TValue>
*/
public function toArray(): array
{
return array_filter(
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Cachet/ThemeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Spatie\LaravelData\Attributes\Computed;
use Spatie\LaravelData\Attributes\Validation\Required;

class ThemeData extends BaseData
final class ThemeData extends BaseData
{
public const GRAYS = ['slate', 'gray', 'zinc', 'neutral', 'stone'];

Expand Down
18 changes: 0 additions & 18 deletions src/Data/IncidentUpdate/CreateIncidentUpdateData.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Data/IncidentUpdate/EditIncidentUpdateData.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Data/Metric/CreateMetricPointData.php

This file was deleted.

Loading

0 comments on commit 88960af

Please sign in to comment.