Magic in constructor is replaced by factory

This commit is contained in:
bkrukowski 2016-10-14 20:19:38 +02:00
parent 545bdd4541
commit beb514e0b3
7 changed files with 71 additions and 37 deletions

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -0,0 +1,10 @@
<?php
namespace bkrukowski\TransparentEmail;
use bkrukowski\TransparentEmail\Emails\EmailInterface;
interface TransparentEmailInterface
{
public function getPrimaryEmail(EmailInterface $email) : EmailInterface;
}

View File

@ -1,21 +0,0 @@
<?php
declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests;
use bkrukowski\TransparentEmail\DefaultServiceCollector;
use bkrukowski\TransparentEmail\Services\ServiceInterface;
class DefaultServiceCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testIterator()
{
$atLeastOne = false;
foreach (new DefaultServiceCollector() as $service) {
$atLeastOne = true;
$this->assertInstanceOf(ServiceInterface::class, $service);
}
$this->assertTrue($atLeastOne);
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests;
use bkrukowski\TransparentEmail\Emails\Email;
use bkrukowski\TransparentEmail\Emails\EmailInterface;
use bkrukowski\TransparentEmail\TransparentEmailFactory;
class TransparentEmailFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerExpectedEmail
*
* @param EmailInterface $inputEmail
* @param string $expectedEmail
*/
public function testExpectedEmail(EmailInterface $inputEmail, string $expectedEmail)
{
$outputEmail = TransparentEmailFactory::createDefault()->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'],
];
}
}

View File

@ -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()