services = $services; } /** * @param string $email * @return string * @throws InvalidEmailException */ public function getPrimaryEmail(string $email) { $this->validateEmailAddress($email); $email = strtolower($email); list(, $domain) = explode('@', $email); $result = $email; foreach ($this->services as $serviceClass) { /** @var ServiceInterface $service */ $service = new $serviceClass(); if ($service->isDomainSupported($domain)) { $result = $service->getPrimaryEmail($result); } } return $result; } /** * @param string $email * @throws InvalidEmailException */ private function validateEmailAddress(string $email) { if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { throw new InvalidEmailException("Invalid email '{$email}'!"); } } }