diff --git a/src/DefaultServiceCollector.php b/src/DefaultServiceCollector.php new file mode 100644 index 0000000..239e1ed --- /dev/null +++ b/src/DefaultServiceCollector.php @@ -0,0 +1,34 @@ +getAllServicesClasses() as $class) { + $this->addService(new $class()); + } + } + + private function getAllServicesClasses() : array + { + return [ + GmailCom::class, + OutlookCom::class, + YahooCom::class, + Www33MailCom::class, + TlenPl::class, + AppsGoogleCom::class, + ]; + } +} \ No newline at end of file diff --git a/src/ServiceCollector.php b/src/ServiceCollector.php new file mode 100644 index 0000000..8258942 --- /dev/null +++ b/src/ServiceCollector.php @@ -0,0 +1,27 @@ +services[] = $service; + } + + /** + * @inheritdoc + */ + public function getIterator() + { + foreach ($this->services as $service) { + yield $service; + } + } +} \ No newline at end of file diff --git a/src/ServiceCollectorInterface.php b/src/ServiceCollectorInterface.php new file mode 100644 index 0000000..9d98605 --- /dev/null +++ b/src/ServiceCollectorInterface.php @@ -0,0 +1,13 @@ +services = $services; + $this->services = $services ?: new DefaultServiceCollector(); $this->caseSensitiveLocalPart = $caseSensitiveLocalPart; } @@ -55,12 +36,9 @@ class TransparentEmail $domain = strtolower(explode('@', $email)[1]); $result = $email; - foreach ($this->services as $serviceClass) { - /** @var ServiceInterface $service */ - $service = new $serviceClass(); + foreach ($this->services as $service) { if ($service->isDomainSupported($domain)) { $result = $service->getPrimaryEmail($result); - break; } } diff --git a/tests/DefaultServiceCollectorTest.php b/tests/DefaultServiceCollectorTest.php new file mode 100644 index 0000000..c5dab4c --- /dev/null +++ b/tests/DefaultServiceCollectorTest.php @@ -0,0 +1,21 @@ +assertInstanceOf(ServiceInterface::class, $service); + } + $this->assertTrue($atLeastOne); + } +} \ No newline at end of file diff --git a/tests/ServiceCollectorTest.php b/tests/ServiceCollectorTest.php new file mode 100644 index 0000000..38f77f5 --- /dev/null +++ b/tests/ServiceCollectorTest.php @@ -0,0 +1,28 @@ +createMock(ServiceInterface::class); + $collector = new ServiceCollector(); + $collector->addService($service); + + $contains = false; + foreach ($collector->getIterator() as $currentService) { + if ($currentService === $service) { + $contains = true; + break; + } + } + + $this->assertTrue($contains); + } +} \ No newline at end of file diff --git a/tests/TransparentEmailTest.php b/tests/TransparentEmailTest.php index 95dbd4e..efa42f1 100644 --- a/tests/TransparentEmailTest.php +++ b/tests/TransparentEmailTest.php @@ -2,6 +2,9 @@ namespace bkrukowski\TransparentEmail\Tests; +use bkrukowski\TransparentEmail\ServiceCollector; +use bkrukowski\TransparentEmail\ServiceCollectorInterface; +use bkrukowski\TransparentEmail\Services\TlenPl; use bkrukowski\TransparentEmail\TransparentEmail; use bkrukowski\TransparentEmail\InvalidEmailException; @@ -43,18 +46,31 @@ class TransparentEmailTest extends \PHPUnit_Framework_TestCase public function providerGetPrimaryEmail() { + $emptyServiceCollector = $this->createServiceCollector(); + $tlenServiceCollector = $this->createServiceCollector([TlenPl::class]); + return [ [ - new TransparentEmail([TransparentEmail::SERVICE_TLEN_PL]), + new TransparentEmail($tlenServiceCollector), 'john.doe+alias@gmail.com', 'john.doe+alias@gmail.com' ], [new TransparentEmail(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'], - [new TransparentEmail([]), 'John.Doe@example.com', 'john.doe@example.com'], - [new TransparentEmail([], true), 'John.Doe@example.com', 'John.Doe@example.com'], - [new TransparentEmail(TransparentEmail::ALL_SERVICES, true), 'John.Doe@gmail.com', 'johndoe@gmail.com'], + [new TransparentEmail($emptyServiceCollector), 'John.Doe@example.com', 'john.doe@example.com'], + [new TransparentEmail($emptyServiceCollector, true), 'John.Doe@example.com', 'John.Doe@example.com'], + [new TransparentEmail(null, true), 'John.Doe@gmail.com', 'johndoe@gmail.com'], [new TransparentEmail(), 'Jane.Doe+receipts@hotmail.com', 'jane.doe@hotmail.com'], [new TransparentEmail(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'], ]; } + + private function createServiceCollector(array $classes = []) : ServiceCollectorInterface + { + $collector = new ServiceCollector(); + foreach ($classes as $class) { + $collector->addService(new $class()); + } + + return $collector; + } } \ No newline at end of file