mirror of
https://github.com/retailcrm/transparent-email.git
synced 2024-11-28 08:16:03 +03:00
Added ServiceCollectorInterface
This commit is contained in:
parent
5557e8d955
commit
53d5f0852f
34
src/DefaultServiceCollector.php
Normal file
34
src/DefaultServiceCollector.php
Normal 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
27
src/ServiceCollector.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
13
src/ServiceCollectorInterface.php
Normal file
13
src/ServiceCollectorInterface.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace bkrukowski\TransparentEmail;
|
||||
|
||||
use bkrukowski\TransparentEmail\Services\ServiceInterface;
|
||||
|
||||
interface ServiceCollectorInterface extends \IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* @return ServiceInterface[]
|
||||
*/
|
||||
public function getIterator();
|
||||
}
|
@ -2,42 +2,23 @@
|
||||
|
||||
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\TlenPl;
|
||||
use bkrukowski\TransparentEmail\Services\Www33MailCom;
|
||||
use bkrukowski\TransparentEmail\Services\YahooCom;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
21
tests/DefaultServiceCollectorTest.php
Normal file
21
tests/DefaultServiceCollectorTest.php
Normal 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);
|
||||
}
|
||||
}
|
28
tests/ServiceCollectorTest.php
Normal file
28
tests/ServiceCollectorTest.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user