with('employee') ->where('id', $id->value) ->first(); if ($customer === null || $customer->employee === null) { return null; } return $this->mapToShopper($customer); } #[\Override] public function persist(Shopper $entity): void { $customer = Customer::query() ->with('employee') ->where('id', $entity->id()->value) ->firstOrNew(); $customer->id = $entity->id()->value->toString(); $customer->email = $entity->email; if ($customer->employee === null) { throw new \DomainException( 'Customer must have an associated employee to persist a Shopper.' ); } $customer->employee->matricule = $entity->matricule; $customer->employee->nom = $entity->nom; $customer->employee->prenom = $entity->prenom; $customer->employee->start_date = Carbon::instance($entity->startDate); $customer->employee->end_date = optional( $entity->endDate, fn (?CarbonInterface $date) => $date ? Carbon::instance($date) : null ); $customer->employee->etablissement = $entity->etablissement; $customer->employee->site = $entity->site; $customer->employee->service = $entity->service; $customer->push(); } #[\Override] public function delete(Shopper $entity): void { Customer::query() ->where('id', $entity->id()->value) ->delete(); } public function getByIdOrFail(ShopperId $id): Shopper { $shopper = $this->find($id); if ($shopper === null) { throw new \DomainException("Shopper with id '{$id->value}' not found or is not linked to any employee."); } return $shopper; } private function mapToShopper(Customer $customer): Shopper { if (! $customer->employee) { throw new \DomainException( 'Customer must have an associated employee to be mapped to a Shopper.' ); } return new Shopper( id: $customer->getId(), email: $customer->email, matricule: clone $customer->employee->matricule, nom: $customer->employee->nom, prenom: $customer->employee->prenom, startDate: $customer->employee->start_date ? CarbonImmutable::instance($customer->employee->start_date) : CarbonImmutable::now(), endDate: $customer->employee->end_date ? CarbonImmutable::instance($customer->employee->end_date) : null, etablissement: $customer->employee->etablissement, site: $customer->employee->site, service: $customer->employee->service ); } }