Initial public release

This commit is contained in:
bkrukowski 2016-09-10 00:41:21 +02:00
commit 6fb1eae38f
17 changed files with 486 additions and 0 deletions

3
.coveralls.yml Normal file
View File

@ -0,0 +1,3 @@
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
service_name: travis-ci

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/.idea/
composer.lock
/vendor/
/build/

21
.travis.yml Normal file
View File

@ -0,0 +1,21 @@
language: php
install:
- php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified' . PHP_EOL; } else { throw new \RuntimeException('Installer corrupt'); }"
- php composer-setup.php
- php -r "unlink('composer-setup.php');"
- travis_wait php composer.phar install --profile --no-interaction
- mkdir -p build/logs
php:
- 7.0
- hhvm
- nightly
script:
- ./vendor/bin/phpunit
after_script:
- travis_retry php vendor/bin/coveralls -v
- travis_retry php vendor/bin/codacycoverage clover ./build/logs/clover.xml

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright (c) 2016 Bartłomiej Krukowski
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

30
composer.json Normal file
View File

@ -0,0 +1,30 @@
{
"name": "bkrukowski/gordianus",
"description": "Remove aliases from email and get primary email account",
"type": "library",
"require": {
"php": "^7.0.0"
},
"require-dev": {
"phpunit/phpunit": "^5.5.4",
"satooshi/php-coveralls": "^1.0.1",
"codacy/coverage": "^1.0.3"
},
"license": "MIT",
"authors": [
{
"name": "Bartłomiej Krukowski",
"email": "bartlomiej@krukowski.me"
}
],
"autoload": {
"psr-4": {
"bkrukowski\\Gordianus\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"bkrukowski\\Gordianus\\Tests\\": "tests/"
}
}
}

25
phpunit.xml.dist Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.5/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="auto"
>
<testsuites>
<testsuite name="Gordianus Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>

65
src/Gordianus.php Normal file
View File

@ -0,0 +1,65 @@
<?php
namespace bkrukowski\Gordianus;
use bkrukowski\Gordianus\Services\GmailCOM;
use bkrukowski\Gordianus\Services\ServiceInterface;
use bkrukowski\Gordianus\Services\TlenPL;
use bkrukowski\Gordianus\Services\WWW33MailCOM;
class Gordianus
{
const SERVICE_GMAIL_COM = GmailCOM::class;
const SERVICE_TLEN_PL = TlenPL::class;
const SERVICE_WWW_33MAIL_COM = WWW33MailCOM::class;
/**
* Constant ALL_SERVICES can contain different values depends on API version
*/
const ALL_SERVICES = [
self::SERVICE_GMAIL_COM,
self::SERVICE_TLEN_PL,
self::SERVICE_WWW_33MAIL_COM,
];
private $services;
public function __construct(array $services = self::ALL_SERVICES)
{
$this->services = $services;
}
/**
* @param string $email
* @return string
* @throws InvalidEmailException
*/
public function getPrimaryEmail(string $email)
{
$this->validateEmailAddress($email);
$email = strtolower($email);
list(, $domain) = explode('@', $email);
$result = $email;
foreach ($this->services as $serviceClass) {
/** @var ServiceInterface $service */
$service = new $serviceClass();
if ($service->isDomainSupported($domain)) {
$result = $service->getPrimaryEmail($result);
}
}
return $result;
}
/**
* @param string $email
* @throws InvalidEmailException
*/
private function validateEmailAddress(string $email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
throw new InvalidEmailException("Invalid email '{$email}'!");
}
}
}

View File

@ -0,0 +1,7 @@
<?php
namespace bkrukowski\Gordianus;
class InvalidEmailException extends \Exception
{
}

21
src/Services/GmailCOM.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace bkrukowski\Gordianus\Services;
/**
* @internal
*/
class GmailCOM implements ServiceInterface
{
public function getPrimaryEmail(string $email) : string
{
list($name, $domain) = explode('@', $email);
return explode('+', str_replace('.', '', $name))[0] . '@' . $domain;
}
public function isDomainSupported(string $domain) : bool
{
return $domain === 'gmail.com';
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace bkrukowski\Gordianus\Services;
/**
* @internal
*/
abstract class MultiDomain implements ServiceInterface
{
public function getPrimaryEmail(string $email) : string
{
return explode('@', $email)[0] . '@' . $this->getPrimaryDomain();
}
public function isDomainSupported(string $domain) : bool
{
return in_array($domain, $this->getDomainList());
}
abstract protected function getPrimaryDomain() : string;
abstract protected function getDomainList() : array;
}

View File

@ -0,0 +1,13 @@
<?php
namespace bkrukowski\Gordianus\Services;
/**
* @internal
*/
interface ServiceInterface
{
public function isDomainSupported(string $domain) : bool;
public function getPrimaryEmail(string $email) : string;
}

25
src/Services/TlenPL.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace bkrukowski\Gordianus\Services;
/**
* @codeCoverageIgnore
* @internal
*/
class TlenPL extends MultiDomain
{
protected function getDomainList() : array
{
return [
'o2.pl',
'tlen.pl',
'go2.pl',
'prokonto.pl',
];
}
protected function getPrimaryDomain() : string
{
return 'o2.pl';
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace bkrukowski\Gordianus\Services;
/**
* @internal
*/
class WWW33MailCOM implements ServiceInterface
{
public function getPrimaryEmail(string $email) : string
{
list(, $domain) = explode('@', $email);
return preg_replace('/\\.33mail\\.com$/', '', $domain) . '@' . $domain;
}
public function isDomainSupported(string $domain) : bool
{
return (bool) preg_match('/\\.33mail\\.com$/', $domain);
}
}

51
tests/GordianusTest.php Normal file
View File

@ -0,0 +1,51 @@
<?php
namespace bkrukowski\Gordianus\Tests;
use bkrukowski\Gordianus\Gordianus;
use bkrukowski\Gordianus\InvalidEmailException;
class GordianusTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerValidator
*
* @param string $email
* @param bool $validEmail
*/
public function testValidator(string $email, bool $validEmail)
{
if (!$validEmail) {
$this->expectException(InvalidEmailException::class);
}
(new Gordianus())->getPrimaryEmail($email);
}
public function providerValidator()
{
return [
['john.doe@gmail.comm', true],
['john doe@gmail.com', false],
];
}
/**
* @dataProvider providerGetPrimaryEmail
*
* @param Gordianus $gordianus
* @param string $email
* @param string $expectedEmail
*/
public function testGetPrimaryEmail(Gordianus $gordianus, string $email, string $expectedEmail)
{
$this->assertSame($expectedEmail, $gordianus->getPrimaryEmail($email));
}
public function providerGetPrimaryEmail()
{
return [
[new Gordianus([Gordianus::SERVICE_TLEN_PL]), 'john.doe+alias@gmail.com', 'john.doe+alias@gmail.com'],
[new Gordianus(), 'john.doe+alias@gmail.com', 'johndoe@gmail.com'],
];
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace bkrukowski\Gordianus\Tests\Services;
use bkrukowski\Gordianus\Services\GmailCOM;
class GmailCOMTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerGetPrimaryEmail
*
* @param string $inputEmail
* @param string $outputEmail
*/
public function testGetPrimaryEmail(string $inputEmail, string $outputEmail)
{
$this->assertSame($outputEmail, (new GmailCOM())->getPrimaryEmail($inputEmail));
}
public function providerGetPrimaryEmail()
{
return [
['foo.bar@gmail.com', 'foobar@gmail.com'],
['foobar+alias@gmail.com', 'foobar@gmail.com'],
['fo.ob.ar+alias@gmail.com', 'foobar@gmail.com'],
];
}
/**
* @dataProvider providerIsDomainSupported
*
* @param string $domain
* @param bool $result
*/
public function testIsDomainSupported(string $domain, bool $result)
{
$this->assertSame($result, (new GmailCOM())->isDomainSupported($domain));
}
public function providerIsDomainSupported()
{
return [
['gmail.com', true],
['google.com', false],
['gmailcom', false],
['g.mail.com', false]
];
}
}

View File

@ -0,0 +1,73 @@
<?php
namespace bkrukowski\Gordianus\Tests\Services;
use bkrukowski\Gordianus\Services\MultiDomain;
class MultiDomainTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerIsDomainSupported
*
* @param MultiDomain $mock
* @param string $domain
* @param bool $expected
*/
public function testIsDomainSupported(MultiDomain $mock, string $domain, bool $expected)
{
$this->assertSame($expected, $mock->isDomainSupported($domain));
}
public function providerIsDomainSupported()
{
return [
[$this->getMultiDomainMock('foo.bar', ['foo.bar']), 'gmail.com', false],
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'gmail.com']), 'gmail.com', true],
];
}
/**
* @dataProvider providerGetPrimaryDomain
*
* @param MultiDomain $mock
* @param string $email
* @param string $expectedEmail
*/
public function testGetPrimaryDomain(MultiDomain $mock, string $email, string $expectedEmail)
{
$this->assertSame($expectedEmail, $mock->getPrimaryEmail($email));
}
public function providerGetPrimaryDomain()
{
return [
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2']), 'name@foo.bar', 'name@foo.bar'],
[$this->getMultiDomainMock('foo.bar', ['foo.bar', 'foo.bar2']), 'name@foo.bar2', 'name@foo.bar'],
];
}
private function getMultiDomainMock(string $primaryDomain, array $supportedDomains) : MultiDomain
{
return new class ($primaryDomain, $supportedDomains) extends MultiDomain {
private $primaryDomain;
private $supportedDomains;
public function __construct(string $primaryDomain, array $supportedDomains)
{
$this->primaryDomain = $primaryDomain;
$this->supportedDomains = $supportedDomains;
}
protected function getDomainList() : array
{
return $this->supportedDomains;
}
protected function getPrimaryDomain() : string
{
return $this->primaryDomain;
}
};
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace bkrukowski\Gordianus\Tests\Services;
use bkrukowski\Gordianus\Services\WWW33MailCOM;
class WWW33MailCOMTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerIsDomainSupported
*
* @param string $domain
* @param bool $isSupported
*/
public function testIsDomainSupported(string $domain, bool $isSupported)
{
$this->assertSame($isSupported, (new WWW33MailCOM())->isDomainSupported($domain));
}
public function providerIsDomainSupported()
{
return [
['foo.33mail.com', true],
['bar.33mail.com', true],
['foo.34mail.com', false],
['foo33mail.com', false],
];
}
/**
* @dataProvider providerGetPrimaryEmail
*
* @param string $inputEmail
* @param string $expectedEmail
*/
public function testGetPrimaryEmail(string $inputEmail, string $expectedEmail)
{
$this->assertSame($expectedEmail, (new WWW33MailCOM())->getPrimaryEmail($inputEmail));
}
public function providerGetPrimaryEmail()
{
return [
['qwerty@name.33mail.com', 'name@name.33mail.com'],
['lorem@ipsum.33mail.com', 'ipsum@ipsum.33mail.com'],
];
}
}