Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Actions/Component/CreateComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cachet\Data\Requests\Component\CreateComponentRequestData;
use Cachet\Models\Component;
use Illuminate\Support\Facades\DB;

class CreateComponent
{
Expand All @@ -12,8 +13,10 @@ class CreateComponent
*/
public function handle(CreateComponentRequestData $component): Component
{
return tap(Component::create($component->except('meta')->toArray()), function (Component $model) use ($component) {
$model->syncMeta($component->meta ?? []);
return DB::transaction(function () use ($component): Component {
return tap(Component::create($component->except('meta')->toArray()), function (Component $model) use ($component) {
$model->syncMeta($component->meta ?? []);
});
});
}
}
5 changes: 3 additions & 2 deletions src/Actions/Component/DeleteComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ class DeleteComponent
{
/**
* Handle the action.
*
* The delete is soft, so the component keeps its subscriptions for a
* restore; the model purges them once it is hard deleted.
*/
public function handle(Component $component): void
{
$component->subscribers()->detach();

$component->delete();
}
}
9 changes: 5 additions & 4 deletions src/Actions/Component/UpdateComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cachet\Actions\Component;

use Cachet\Data\Requests\Component\UpdateComponentRequestData;
use Cachet\Enums\ComponentStatusEnum;
use Cachet\Enums\ComponentStatusSourceEnum;
use Cachet\Models\Component;
use Illuminate\Contracts\Auth\Authenticatable;
Expand All @@ -23,19 +24,19 @@ public function handle(Component $component, UpdateComponentRequestData $data, ?
DB::transaction(function () use ($component, $data, $user): void {
$attributes = $data->except('meta', 'status')->toArray();

if ($data->status === null) {
$component->update($attributes);
} else {
if ($data->status instanceof ComponentStatusEnum) {
$this->changeComponentStatus->handle(
$component,
$data->status,
ComponentStatusSourceEnum::Manual,
$user,
attributes: $attributes,
);
} else {
$component->update($attributes);
}

if ($data->meta !== null) {
if (is_array($data->meta)) {
$component->syncMeta($data->meta);
}
});
Expand Down
26 changes: 13 additions & 13 deletions src/Actions/ComponentGroup/CreateComponentGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
use Cachet\Data\Requests\ComponentGroup\CreateComponentGroupRequestData;
use Cachet\Models\Component;
use Cachet\Models\ComponentGroup;
use Illuminate\Support\Facades\DB;

class CreateComponentGroup
{
/**
* Handle the action.
*/
/**
* Handle the action.
*/
public function handle(CreateComponentGroupRequestData $data): ComponentGroup
{
return tap(ComponentGroup::create(
$data->except('components', 'meta')->toArray(),
), function (ComponentGroup $componentGroup) use ($data) {
$componentGroup->syncMeta($data->meta ?? []);
return DB::transaction(function () use ($data): ComponentGroup {
return tap(ComponentGroup::create(
$data->except('components', 'meta')->toArray(),
), function (ComponentGroup $componentGroup) use ($data) {
$componentGroup->syncMeta($data->meta ?? []);

if (! $data->components) {
return;
}
if (! $data->components) {
return;
}

Component::query()->whereIn('id', $data->components)->update([
'component_group_id' => $componentGroup->id,
]);
Component::query()->whereIn('id', $data->components)->update([
'component_group_id' => $componentGroup->id,
]);
});
});
}
}
7 changes: 5 additions & 2 deletions src/Actions/ComponentGroup/DeleteComponentGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cachet\Actions\ComponentGroup;

use Cachet\Models\ComponentGroup;
use Illuminate\Support\Facades\DB;

class DeleteComponentGroup
{
Expand All @@ -11,8 +12,10 @@ class DeleteComponentGroup
*/
public function handle(ComponentGroup $componentGroup): void
{
$componentGroup->components()->update(['component_group_id' => null]);
DB::transaction(function () use ($componentGroup): void {
$componentGroup->components()->update(['component_group_id' => null]);

$componentGroup->delete();
$componentGroup->delete();
});
}
}
21 changes: 12 additions & 9 deletions src/Actions/ComponentGroup/UpdateComponentGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cachet\Data\Requests\ComponentGroup\UpdateComponentGroupRequestData;
use Cachet\Models\Component;
use Cachet\Models\ComponentGroup;
use Illuminate\Support\Facades\DB;

class UpdateComponentGroup
{
Expand All @@ -13,17 +14,19 @@ class UpdateComponentGroup
*/
public function handle(ComponentGroup $componentGroup, UpdateComponentGroupRequestData $data): ComponentGroup
{
$componentGroup->update($data->except('components', 'meta')->toArray());
DB::transaction(function () use ($componentGroup, $data): void {
$componentGroup->update($data->except('components', 'meta')->toArray());

if ($data->meta !== null) {
$componentGroup->syncMeta($data->meta);
}
if (is_array($data->meta)) {
$componentGroup->syncMeta($data->meta);
}

if ($data->components) {
Component::query()->whereIn('id', $data->components)->update([
'component_group_id' => $componentGroup->id,
]);
}
if (is_array($data->components) && $data->components !== []) {
Component::query()->whereIn('id', $data->components)->update([
'component_group_id' => $componentGroup->id,
]);
}
});

return $componentGroup->fresh();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/Incident/CreateIncident.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function handle(CreateIncidentRequestData $data): Incident
if (isset($data->template)) {
$template = IncidentTemplate::query()
->where('slug', $data->template)
->first();
->firstOrFail();

$data = $data->withMessage($this->parseTemplate($template, $data));
}
Expand Down Expand Up @@ -65,7 +65,7 @@ private function parseTemplate(IncidentTemplate $template, CreateIncidentRequest
'name' => $data->name,
'status' => $data->status,
'message' => $data->message ?? null,
'visible' => $data->visible,
'visible' => $data->visible->value,
'notify' => $data->notifications,
'stickied' => $data->stickied,
'occurred_at' => $data->occurredAt ?? Carbon::now(),
Expand Down
6 changes: 4 additions & 2 deletions src/Actions/Incident/DeleteIncident.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class DeleteIncident
{
/**
* Handle the action.
*
* The delete is soft, so the incident keeps its updates and component
* attachments for a restore; the model purges them once it is hard
* deleted.
*/
public function handle(Incident $incident): void
{
$incident->updates()->delete();

$incident->delete();
}
}
11 changes: 7 additions & 4 deletions src/Actions/Incident/UpdateIncident.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cachet\Data\Requests\Incident\UpdateIncidentRequestData;
use Cachet\Models\Incident;
use Illuminate\Support\Facades\DB;

class UpdateIncident
{
Expand All @@ -12,11 +13,13 @@ class UpdateIncident
*/
public function handle(Incident $incident, UpdateIncidentRequestData $data): Incident
{
$incident->update($data->except('meta')->toArray());
DB::transaction(function () use ($incident, $data): void {
$incident->update($data->except('meta')->toArray());

if ($data->meta !== null) {
$incident->syncMeta($data->meta);
}
if (is_array($data->meta)) {
$incident->syncMeta($data->meta);
}
});

return $incident->fresh();
}
Expand Down
7 changes: 5 additions & 2 deletions src/Actions/Metric/DeleteMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cachet\Actions\Metric;

use Cachet\Models\Metric;
use Illuminate\Support\Facades\DB;

class DeleteMetric
{
Expand All @@ -11,7 +12,9 @@ class DeleteMetric
*/
public function handle(Metric $metric): void
{
$metric->metricPoints()->delete();
$metric->delete();
DB::transaction(function () use ($metric): void {
$metric->metricPoints()->delete();
$metric->delete();
});
}
}
33 changes: 18 additions & 15 deletions src/Actions/Schedule/UpdateSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cachet\Data\Requests\Schedule\ScheduleComponentRequestData;
use Cachet\Data\Requests\Schedule\UpdateScheduleRequestData;
use Cachet\Models\Schedule;
use Illuminate\Support\Facades\DB;

class UpdateSchedule
{
Expand All @@ -13,21 +14,23 @@ class UpdateSchedule
*/
public function handle(Schedule $schedule, UpdateScheduleRequestData $data): Schedule
{
$schedule->update($data->except('components', 'meta')->toArray());

if ($data->meta !== null) {
$schedule->syncMeta($data->meta);
}

if ($data->components) {
$components = collect($data->components)
->mapWithKeys(fn (ScheduleComponentRequestData $component) => [
$component->id => ['component_status' => $component->status],
])
->all();

$schedule->components()->sync($components);
}
DB::transaction(function () use ($schedule, $data): void {
$schedule->update($data->except('components', 'meta')->toArray());

if (is_array($data->meta)) {
$schedule->syncMeta($data->meta);
}

if (is_array($data->components) && $data->components !== []) {
$components = collect($data->components)
->mapWithKeys(fn (ScheduleComponentRequestData $component) => [
$component->id => ['component_status' => $component->status],
])
->all();

$schedule->components()->sync($components);
}
});

// @todo Dispatch notification that maintenance was updated.

Expand Down
25 changes: 14 additions & 11 deletions src/Actions/Subscriber/CreateSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cachet\Actions\Subscriber;

use Cachet\Models\Subscriber;
use Illuminate\Support\Facades\DB;

class CreateSubscriber
{
Expand All @@ -11,19 +12,21 @@ class CreateSubscriber
*/
public function handle(string $email, bool $global = true, array $components = [], bool $verified = false, ?array $meta = null): Subscriber
{
$subscriber = Subscriber::firstOrCreate([
'email' => $email,
], [
'global' => $global,
'email_verified_at' => $verified ? now() : null,
]);
return DB::transaction(function () use ($email, $global, $components, $verified, $meta): Subscriber {
$subscriber = Subscriber::firstOrCreate([
'email' => $email,
], [
'global' => $global,
'email_verified_at' => $verified ? now() : null,
]);

$subscriber->components()->attach($components);
$subscriber->components()->attach($components);

if ($meta !== null) {
$subscriber->syncMeta($meta);
}
if ($meta !== null) {
$subscriber->syncMeta($meta);
}

return $subscriber;
return $subscriber;
});
}
}
7 changes: 5 additions & 2 deletions src/Actions/Subscriber/UnsubscribeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cachet\Actions\Subscriber;

use Cachet\Models\Subscriber;
use Illuminate\Support\Facades\DB;

class UnsubscribeSubscriber
{
Expand All @@ -13,8 +14,10 @@ class UnsubscribeSubscriber
*/
public function handle(Subscriber $subscriber): void
{
$subscriber->components()->detach();
DB::transaction(function () use ($subscriber): void {
$subscriber->components()->detach();

$subscriber->delete();
$subscriber->delete();
});
}
}
Loading
Loading