Files
crewli/api/app/Services/Artist/PerformanceService.php
bert.hausmans f7ed03237c feat(timetable): seven artist-domain services + supporting exceptions
GenreService, ArtistService, ArtistEngagementService (state machine),
StageService, StageDayService, PerformanceService, LaneCascadeService
under app/Services/Artist/. Plain final classes with constructor
injection — matches FormSubmissionService convention.

ArtistEngagementService implements the RFC §10.1 booking_status state
machine: terminal Cancelled/Rejected/Declined, Option requires future
option_expires_at, Contracted requires fee_amount > 0. transitionStatus
is the focused entry point; update() routes through it whenever the
payload mutates booking_status. cancel() composes transitionStatus +
soft delete in one transaction so the existing
ArtistEngagementObserver cascade fires.

LaneCascadeService is the D18 transactional move algorithm. Locks the
dragged Performance row FOR UPDATE, validates client version against
the persisted version (D14), then either parks (stage_id=null, no
cascade) or places onto (stage, event, lane) with single-level
cascade-bump of any time-overlapping rows on the target lane. Returns
a MoveResult value object carrying the moved + cascaded performances
so the controller maps them to API resources without a second query.

StageDayService implements the §10.5 atomic matrix replace. Detects
non-cancelled performances on event_ids about to be removed; throws
StageDaysOrphanedPerformancesException unless force_orphan=true. The
orphans are not deleted — they persist with the same stage_id so they
re-appear when the day re-activates (D5/D27 retention).

ArtistService.create raises DuplicateArtistException carrying the
existing master so the controller can offer a "use existing" choice
instead of forcing the booker to abandon their dialog. ArtistEngagement
defaults buma_handled_by based on artist.agent_company.handles_buma
per RFC D26.

GenreService.delete is hard-blocked (GenreInUseException) when artists
still reference the genre via default_genre_id; the frontend rebinds
those artists first.

StageService.delete cascade-parks performances (stage_id → null, lane
preserved) and returns the parked count for the activity-log entry
the controller writes in Step 9.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 20:49:18 +02:00

110 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Artist;
use App\Models\ArtistEngagement;
use App\Models\Performance;
use App\Models\Stage;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\DB;
/**
* Performance CRUD + park/unpark. Placement edits (move-by-drag,
* cascade) route through LaneCascadeService to keep the row-locking
* and version-bump in one place; this service only handles non-
* placement state (notes) plus the binary park/unpark transitions.
*/
final class PerformanceService
{
public function __construct(
private readonly LaneCascadeService $laneCascade,
) {}
/**
* @param array<string, mixed> $attributes
*/
public function create(ArtistEngagement $engagement, array $attributes): Performance
{
return DB::transaction(function () use ($engagement, $attributes): Performance {
$perf = new Performance($attributes);
$perf->engagement_id = $engagement->id;
$perf->event_id = $attributes['event_id'] ?? $engagement->event_id;
$perf->version = 1;
$perf->save();
return $perf->refresh();
});
}
/**
* Non-placement update only. start_at/end_at/stage_id/lane changes
* MUST go through LaneCascadeService::move so the cascade-bump and
* optimistic-lock contract are honoured.
*
* @param array<string, mixed> $attributes
*/
public function update(Performance $performance, array $attributes): Performance
{
unset(
$attributes['stage_id'],
$attributes['start_at'],
$attributes['end_at'],
$attributes['lane'],
$attributes['version'],
$attributes['event_id'],
$attributes['engagement_id'],
);
$performance->fill($attributes);
$performance->save();
return $performance->refresh();
}
public function delete(Performance $performance): void
{
$performance->delete();
}
/**
* Move into the wachtrij. lane preserved so the user can drag back
* to roughly the same visual position (D12 — wachtrij is just
* stage_id=null, no separate table).
*/
public function park(Performance $performance, int $clientVersion): Performance
{
$result = $this->laneCascade->move(
performance: $performance,
targetStage: null,
start: null,
end: null,
targetLane: null,
clientVersion: $clientVersion,
);
return $result->moved;
}
public function unpark(
Performance $performance,
Stage $targetStage,
CarbonImmutable $start,
CarbonImmutable $end,
int $targetLane,
int $clientVersion,
): Performance {
$result = $this->laneCascade->move(
performance: $performance,
targetStage: $targetStage,
start: $start,
end: $end,
targetLane: $targetLane,
clientVersion: $clientVersion,
);
return $result->moved;
}
}