From beb514e0b339c251e688493cce9fafd1507e4b1d Mon Sep 17 00:00:00 2001 From: bkrukowski Date: Fri, 14 Oct 2016 20:19:38 +0200 Subject: [PATCH] Magic in constructor is replaced by factory --- README.md | 4 +-- src/TransparentEmail.php | 6 ++-- ...lector.php => TransparentEmailFactory.php} | 21 +++++++---- src/TransparentEmailInterface.php | 10 ++++++ tests/DefaultServiceCollectorTest.php | 21 ----------- tests/TransparentEmailFactoryTest.php | 35 +++++++++++++++++++ tests/TransparentEmailTest.php | 11 +++--- 7 files changed, 71 insertions(+), 37 deletions(-) rename src/{DefaultServiceCollector.php => TransparentEmailFactory.php} (54%) create mode 100644 src/TransparentEmailInterface.php delete mode 100644 tests/DefaultServiceCollectorTest.php create mode 100644 tests/TransparentEmailFactoryTest.php diff --git a/README.md b/README.md index b11a350..5f849bd 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,12 @@ To detect multi-accounts on your website. ## Usage ```php -use bkrukowski\TransparentEmail\TransparentEmail; +use bkrukowski\TransparentEmail\TransparentEmailFactory; use bkrukowski\TransparentEmail\Emails\Email; use bkrukowski\TransparentEmail\Emails\InvalidEmailException; try { - $cleaner = new TransparentEmail(); + $cleaner = TransparentEmailFactory::createDefault(); $transformedEmail = $cleaner->getPrimaryEmail(new Email('John.Doe+alias@gmail.com')); } catch (InvalidEmailException $exception) { // do something diff --git a/src/TransparentEmail.php b/src/TransparentEmail.php index 5758b1d..45ca0a1 100644 --- a/src/TransparentEmail.php +++ b/src/TransparentEmail.php @@ -7,16 +7,16 @@ namespace bkrukowski\TransparentEmail; use bkrukowski\TransparentEmail\Emails\EmailInterface; use bkrukowski\TransparentEmail\Services\ServiceInterface; -class TransparentEmail +class TransparentEmail implements TransparentEmailInterface { /** * @var ServiceCollectorInterface|ServiceInterface[] */ private $services; - public function __construct(ServiceCollectorInterface $services = null) + public function __construct(ServiceCollectorInterface $services) { - $this->services = $services ?: new DefaultServiceCollector(); + $this->services = $services; } public function getPrimaryEmail(EmailInterface $email) : EmailInterface diff --git a/src/DefaultServiceCollector.php b/src/TransparentEmailFactory.php similarity index 54% rename from src/DefaultServiceCollector.php rename to src/TransparentEmailFactory.php index 239e1ed..8765924 100644 --- a/src/DefaultServiceCollector.php +++ b/src/TransparentEmailFactory.php @@ -11,16 +11,25 @@ use bkrukowski\TransparentEmail\Services\TlenPl; use bkrukowski\TransparentEmail\Services\Www33MailCom; use bkrukowski\TransparentEmail\Services\YahooCom; -class DefaultServiceCollector extends ServiceCollector +class TransparentEmailFactory { - public function __construct() + public static function createDefault() : TransparentEmailInterface { - foreach ($this->getAllServicesClasses() as $class) { - $this->addService(new $class()); - } + return new TransparentEmail(self::createServiceCollector()); } - private function getAllServicesClasses() : array + private static function createServiceCollector() : ServiceCollectorInterface + { + $collector = new ServiceCollector(); + + foreach (self::getAllServicesClasses() as $servicesClass) { + $collector->addService(new $servicesClass()); + } + + return $collector; + } + + private static function getAllServicesClasses() : array { return [ GmailCom::class, diff --git a/src/TransparentEmailInterface.php b/src/TransparentEmailInterface.php new file mode 100644 index 0000000..75e5600 --- /dev/null +++ b/src/TransparentEmailInterface.php @@ -0,0 +1,10 @@ +assertInstanceOf(ServiceInterface::class, $service); - } - $this->assertTrue($atLeastOne); - } -} \ No newline at end of file diff --git a/tests/TransparentEmailFactoryTest.php b/tests/TransparentEmailFactoryTest.php new file mode 100644 index 0000000..b54366d --- /dev/null +++ b/tests/TransparentEmailFactoryTest.php @@ -0,0 +1,35 @@ +getPrimaryEmail($inputEmail); + $this->assertSame($expectedEmail, (string) $outputEmail); + } + + public function providerExpectedEmail() : array + { + return [ + [new Email('John.Doe+spam@gmail.com', true), 'johndoe@gmail.com'], + [new Email('Jane.Doe+spam@outlook.com', true), 'jane.doe@outlook.com'], + [new Email('John.Doe@tlen.pl', true), 'john.doe@o2.pl'], + [new Email('ALIAS@janedoe.33mail.com', true), 'janedoe@janedoe.33mail.com'], + [new Email('John.Doe-facebook@yahoo.com', true), 'john.doe@yahoo.com'], + ]; + } +} \ No newline at end of file diff --git a/tests/TransparentEmailTest.php b/tests/TransparentEmailTest.php index ae4e760..7a78650 100644 --- a/tests/TransparentEmailTest.php +++ b/tests/TransparentEmailTest.php @@ -10,6 +10,7 @@ use bkrukowski\TransparentEmail\ServiceCollector; use bkrukowski\TransparentEmail\ServiceCollectorInterface; use bkrukowski\TransparentEmail\Services\TlenPl; use bkrukowski\TransparentEmail\TransparentEmail; +use bkrukowski\TransparentEmail\TransparentEmailFactory; class TransparentEmailTest extends \PHPUnit_Framework_TestCase { @@ -41,12 +42,12 @@ class TransparentEmailTest extends \PHPUnit_Framework_TestCase 'john.doe+alias@gmail.com', 'john.doe+alias@gmail.com' ], - [new TransparentEmail(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'], + [TransparentEmailFactory::createDefault(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'], [new TransparentEmail($emptyServiceCollector), 'John.Doe@example.com', 'john.doe@example.com'], [new TransparentEmail($emptyServiceCollector), 'John.Doe@example.com', 'John.Doe@example.com', true], - [new TransparentEmail(), 'John.Doe@gmail.com', 'johndoe@gmail.com', true], - [new TransparentEmail(), 'Jane.Doe+receipts@hotmail.com', 'jane.doe@hotmail.com'], - [new TransparentEmail(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'], + [TransparentEmailFactory::createDefault(), 'John.Doe@gmail.com', 'johndoe@gmail.com', true], + [TransparentEmailFactory::createDefault(), 'Jane.Doe+receipts@hotmail.com', 'jane.doe@hotmail.com'], + [TransparentEmailFactory::createDefault(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.com'], ]; } @@ -58,7 +59,7 @@ class TransparentEmailTest extends \PHPUnit_Framework_TestCase */ public function testDefault(EmailInterface $inputEmail, string $expectedEmail) { - $this->assertEquals($expectedEmail, (new TransparentEmail())->getPrimaryEmail($inputEmail)); + $this->assertEquals($expectedEmail, (TransparentEmailFactory::createDefault())->getPrimaryEmail($inputEmail)); } public function providerDefault()