Added ServiceCollectorInterface

This commit is contained in:
bkrukowski 2016-09-23 21:29:25 +02:00
parent 5557e8d955
commit 53d5f0852f
7 changed files with 150 additions and 33 deletions

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace bkrukowski\TransparentEmail;
use bkrukowski\TransparentEmail\Services\AppsGoogleCom;
use bkrukowski\TransparentEmail\Services\GmailCom;
use bkrukowski\TransparentEmail\Services\OutlookCom;
use bkrukowski\TransparentEmail\Services\TlenPl;
use bkrukowski\TransparentEmail\Services\Www33MailCom;
use bkrukowski\TransparentEmail\Services\YahooCom;
class DefaultServiceCollector extends ServiceCollector
{
public function __construct()
{
foreach ($this->getAllServicesClasses() as $class) {
$this->addService(new $class());
}
}
private function getAllServicesClasses() : array
{
return [
GmailCom::class,
OutlookCom::class,
YahooCom::class,
Www33MailCom::class,
TlenPl::class,
AppsGoogleCom::class,
];
}
}

27
src/ServiceCollector.php Normal file
View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace bkrukowski\TransparentEmail;
use bkrukowski\TransparentEmail\Services\ServiceInterface;
class ServiceCollector implements ServiceCollectorInterface
{
private $services = [];
public function addService(ServiceInterface $service)
{
$this->services[] = $service;
}
/**
* @inheritdoc
*/
public function getIterator()
{
foreach ($this->services as $service) {
yield $service;
}
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace bkrukowski\TransparentEmail;
use bkrukowski\TransparentEmail\Services\ServiceInterface;
interface ServiceCollectorInterface extends \IteratorAggregate
{
/**
* @return ServiceInterface[]
*/
public function getIterator();
}

View File

@ -2,42 +2,23 @@
namespace bkrukowski\TransparentEmail; namespace bkrukowski\TransparentEmail;
use bkrukowski\TransparentEmail\Services\AppsGoogleCom;
use bkrukowski\TransparentEmail\Services\GmailCom;
use bkrukowski\TransparentEmail\Services\OutlookCom;
use bkrukowski\TransparentEmail\Services\ServiceInterface; use bkrukowski\TransparentEmail\Services\ServiceInterface;
use bkrukowski\TransparentEmail\Services\TlenPl;
use bkrukowski\TransparentEmail\Services\Www33MailCom;
use bkrukowski\TransparentEmail\Services\YahooCom;
class TransparentEmail class TransparentEmail
{ {
const SERVICE_GMAIL_COM = GmailCom::class;
const SERVICE_TLEN_PL = TlenPl::class;
const SERVICE_WWW_33MAIL_COM = Www33MailCom::class;
const SERVICE_OUTLOOK_COM = OutlookCom::class;
const SERVICE_YAHOO_COM = YahooCom::class;
const SERVICE_APPS_GOOGLE_COM = AppsGoogleCom::class;
/** /**
* Constant ALL_SERVICES can contain different values depends on API version * @var ServiceCollectorInterface|ServiceInterface[]
*/ */
const ALL_SERVICES = [
self::SERVICE_GMAIL_COM,
self::SERVICE_TLEN_PL,
self::SERVICE_WWW_33MAIL_COM,
self::SERVICE_OUTLOOK_COM,
self::SERVICE_YAHOO_COM,
self::SERVICE_APPS_GOOGLE_COM,
];
private $services; private $services;
/**
* @var bool
*/
private $caseSensitiveLocalPart; private $caseSensitiveLocalPart;
public function __construct(array $services = self::ALL_SERVICES, bool $caseSensitiveLocalPart = false) public function __construct(ServiceCollectorInterface $services = null, bool $caseSensitiveLocalPart = false)
{ {
$this->services = $services; $this->services = $services ?: new DefaultServiceCollector();
$this->caseSensitiveLocalPart = $caseSensitiveLocalPart; $this->caseSensitiveLocalPart = $caseSensitiveLocalPart;
} }
@ -55,12 +36,9 @@ class TransparentEmail
$domain = strtolower(explode('@', $email)[1]); $domain = strtolower(explode('@', $email)[1]);
$result = $email; $result = $email;
foreach ($this->services as $serviceClass) { foreach ($this->services as $service) {
/** @var ServiceInterface $service */
$service = new $serviceClass();
if ($service->isDomainSupported($domain)) { if ($service->isDomainSupported($domain)) {
$result = $service->getPrimaryEmail($result); $result = $service->getPrimaryEmail($result);
break;
} }
} }

View File

@ -0,0 +1,21 @@
<?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,28 @@
<?php
declare(strict_types=1);
namespace bkrukowski\TransparentEmail\Tests;
use bkrukowski\TransparentEmail\ServiceCollector;
use bkrukowski\TransparentEmail\Services\ServiceInterface;
class ServiceCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testAddService()
{
$service = $this->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);
}
}

View File

@ -2,6 +2,9 @@
namespace bkrukowski\TransparentEmail\Tests; namespace bkrukowski\TransparentEmail\Tests;
use bkrukowski\TransparentEmail\ServiceCollector;
use bkrukowski\TransparentEmail\ServiceCollectorInterface;
use bkrukowski\TransparentEmail\Services\TlenPl;
use bkrukowski\TransparentEmail\TransparentEmail; use bkrukowski\TransparentEmail\TransparentEmail;
use bkrukowski\TransparentEmail\InvalidEmailException; use bkrukowski\TransparentEmail\InvalidEmailException;
@ -43,18 +46,31 @@ class TransparentEmailTest extends \PHPUnit_Framework_TestCase
public function providerGetPrimaryEmail() public function providerGetPrimaryEmail()
{ {
$emptyServiceCollector = $this->createServiceCollector();
$tlenServiceCollector = $this->createServiceCollector([TlenPl::class]);
return [ return [
[ [
new TransparentEmail([TransparentEmail::SERVICE_TLEN_PL]), new TransparentEmail($tlenServiceCollector),
'john.doe+alias@gmail.com', 'john.doe+alias@gmail.com',
'john.doe+alias@gmail.com' 'john.doe+alias@gmail.com'
], ],
[new TransparentEmail(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'], [new TransparentEmail(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'],
[new TransparentEmail([]), 'John.Doe@example.com', 'john.doe@example.com'], [new TransparentEmail($emptyServiceCollector), 'John.Doe@example.com', 'john.doe@example.com'],
[new TransparentEmail([], true), 'John.Doe@example.com', 'John.Doe@example.com'], [new TransparentEmail($emptyServiceCollector, true), 'John.Doe@example.com', 'John.Doe@example.com'],
[new TransparentEmail(TransparentEmail::ALL_SERVICES, true), 'John.Doe@gmail.com', 'johndoe@gmail.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@hotmail.com', 'jane.doe@hotmail.com'],
[new TransparentEmail(), 'Jane.Doe-receipts@yahoo.com', 'jane.doe@yahoo.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;
}
} }