> $newRoles The new roles by user id. */ public function __construct( public int $jobId, public string $jobNumber, public string $jobName, public array $newRoles, public int $actorUserId, ) {} /** * The roles the saved job gives that the details before did not (a new * job has none before); null when nobody got a new role. */ public static function between(?JobDetails $before, CustomerJob $after, int $actorUserId): ?self { $old = $before === null ? [] : self::rolesOf($before); $newRoles = []; foreach (self::rolesOf($after->details()) as $userId => $roles) { $added = array_values(array_diff($roles, $old[$userId] ?? [])); if ($added !== []) { $newRoles[$userId] = $added; } } if ($newRoles === []) { return null; } return new self((int) $after->id(), $after->number()->format(), $after->details()->name->value, $newRoles, $actorUserId); } /** * @return array> */ private static function rolesOf(JobDetails $details): array { $roles = []; $roles[$details->responsibleUserId][] = self::RESPONSIBLE; if ($details->contactUserId !== null) { $roles[$details->contactUserId][] = self::CONTACT; } if ($details->billerUserId !== null) { $roles[$details->billerUserId][] = self::BILLER; } foreach ($details->memberUserIds as $memberId) { $roles[$memberId][] = self::MEMBER; } return $roles; } }