businessId === null ? null : new BusinessId($data->businessId); $personalIdentityCode = $data->personalIdentityCode === null ? null : new PersonalIdentityCode($data->personalIdentityCode); $customer = Customer::register(new CustomerName($data->name), $data->type, $businessId, $personalIdentityCode); return $this->transactions->run(function () use ($customer, $businessId, $personalIdentityCode): int { if ($businessId !== null && $this->customers->isBusinessIdTaken($businessId)) { throw new BusinessIdAlreadyInUse($businessId); } if ($personalIdentityCode !== null && $this->customers->isPersonalIdentityCodeTaken($personalIdentityCode)) { throw new PersonalIdentityCodeAlreadyInUse; } $customerId = (int) $this->customers->save($customer)->id(); $this->events->dispatch(new CustomerRegistered($customerId)); return $customerId; }); } }