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