From e1eedfff7517557d0ee2cd1b58d6c29dbf5921b3 Mon Sep 17 00:00:00 2001 From: bkrukowski Date: Sat, 24 Sep 2016 13:10:25 +0200 Subject: [PATCH] Value Object should be immutable --- src/Emails/EditableEmail.php | 63 +++++++++++++++++++ src/Emails/MutableEmail.php | 56 ----------------- src/Services/GmailCom.php | 4 +- src/Services/MultiDomain.php | 4 +- src/Services/OutlookCom.php | 4 +- src/Services/Www33MailCom.php | 4 +- src/Services/YahooCom.php | 4 +- ...bleEmailTest.php => EditableEmailTest.php} | 51 ++++++++------- 8 files changed, 101 insertions(+), 89 deletions(-) create mode 100644 src/Emails/EditableEmail.php delete mode 100644 src/Emails/MutableEmail.php rename tests/Emails/{MutableEmailTest.php => EditableEmailTest.php} (63%) diff --git a/src/Emails/EditableEmail.php b/src/Emails/EditableEmail.php new file mode 100644 index 0000000..cb223bd --- /dev/null +++ b/src/Emails/EditableEmail.php @@ -0,0 +1,63 @@ +localPart = $email->getLocalPart(); + $this->domain = $email->getDomain(); + } + + public function removeFromLocalPart(string $toRemove) : EditableEmail + { + $copy = clone $this; + $copy->localPart = str_replace($toRemove, '', $this->localPart); + + return $copy; + } + + public function removeSuffixAlias(string $delimiter) : EditableEmail + { + $copy = clone $this; + $copy->localPart = explode($delimiter, $this->localPart, 2)[0]; + + return $copy; + } + + public function setDomain(string $domain) : EditableEmail + { + $copy = clone $this; + $copy->domain = $domain; + + return $copy; + } + + public function setLocalPart(string $localPart) : EditableEmail + { + $copy = clone $this; + $copy->localPart = $localPart; + + return $copy; + } + + public function lowerCaseLocalPartIf(bool $condition) : EditableEmail + { + if ($condition) { + $copy = clone $this; + $copy->localPart = strtolower($this->localPart); + + return $copy; + } + + return $this; + } +} \ No newline at end of file diff --git a/src/Emails/MutableEmail.php b/src/Emails/MutableEmail.php deleted file mode 100644 index c6cda8e..0000000 --- a/src/Emails/MutableEmail.php +++ /dev/null @@ -1,56 +0,0 @@ -localPart = $email->getLocalPart(); - $this->domain = $email->getDomain(); - } - - public function removeFromLocalPart(string $toRemove) : MutableEmail - { - $this->localPart = str_replace($toRemove, '', $this->localPart); - - return $this; - } - - public function removeSuffixAlias(string $delimiter) : MutableEmail - { - $this->localPart = explode($delimiter, $this->localPart, 2)[0]; - - return $this; - } - - public function setDomain(string $domain) : MutableEmail - { - $this->domain = $domain; - - return $this; - } - - public function setLocalPart(string $localPart) : MutableEmail - { - $this->localPart = $localPart; - - return $this; - } - - public function lowerCaseLocalPartIf(bool $condition) : MutableEmail - { - if ($condition) { - $this->localPart = strtolower($this->localPart); - } - - return $this; - } -} \ No newline at end of file diff --git a/src/Services/GmailCom.php b/src/Services/GmailCom.php index 49cc461..b2113bd 100644 --- a/src/Services/GmailCom.php +++ b/src/Services/GmailCom.php @@ -4,14 +4,14 @@ declare(strict_types=1); namespace bkrukowski\TransparentEmail\Services; -use bkrukowski\TransparentEmail\Emails\MutableEmail; +use bkrukowski\TransparentEmail\Emails\EditableEmail; use bkrukowski\TransparentEmail\Emails\EmailInterface; class GmailCom implements ServiceInterface { public function getPrimaryEmail(EmailInterface $email) : EmailInterface { - return (new MutableEmail($email)) + return (new EditableEmail($email)) ->removeFromLocalPart('.') ->removeSuffixAlias('+') ->lowerCaseLocalPartIf(true) diff --git a/src/Services/MultiDomain.php b/src/Services/MultiDomain.php index fe33d70..0b2a122 100644 --- a/src/Services/MultiDomain.php +++ b/src/Services/MultiDomain.php @@ -4,14 +4,14 @@ declare(strict_types=1); namespace bkrukowski\TransparentEmail\Services; -use bkrukowski\TransparentEmail\Emails\MutableEmail; +use bkrukowski\TransparentEmail\Emails\EditableEmail; use bkrukowski\TransparentEmail\Emails\EmailInterface; abstract class MultiDomain implements ServiceInterface { public function getPrimaryEmail(EmailInterface $email) : EmailInterface { - return (new MutableEmail($email)) + return (new EditableEmail($email)) ->setDomain($this->getPrimaryDomain()) ->lowerCaseLocalPartIf(!$this->isCaseSensitive()); } diff --git a/src/Services/OutlookCom.php b/src/Services/OutlookCom.php index 60bfdcf..32e6ea2 100644 --- a/src/Services/OutlookCom.php +++ b/src/Services/OutlookCom.php @@ -4,14 +4,14 @@ declare(strict_types=1); namespace bkrukowski\TransparentEmail\Services; -use bkrukowski\TransparentEmail\Emails\MutableEmail; +use bkrukowski\TransparentEmail\Emails\EditableEmail; use bkrukowski\TransparentEmail\Emails\EmailInterface; class OutlookCom implements ServiceInterface { public function getPrimaryEmail(EmailInterface $email) : EmailInterface { - return (new MutableEmail($email)) + return (new EditableEmail($email)) ->removeSuffixAlias('+') ->lowerCaseLocalPartIf(true); } diff --git a/src/Services/Www33MailCom.php b/src/Services/Www33MailCom.php index 996eab2..60c7261 100644 --- a/src/Services/Www33MailCom.php +++ b/src/Services/Www33MailCom.php @@ -4,14 +4,14 @@ declare(strict_types=1); namespace bkrukowski\TransparentEmail\Services; -use bkrukowski\TransparentEmail\Emails\MutableEmail; +use bkrukowski\TransparentEmail\Emails\EditableEmail; use bkrukowski\TransparentEmail\Emails\EmailInterface; class Www33MailCom implements ServiceInterface { public function getPrimaryEmail(EmailInterface $email) : EmailInterface { - return (new MutableEmail($email)) + return (new EditableEmail($email)) ->setLocalPart(preg_replace('/\\.33mail\\.com$/', '', $email->getDomain())); } diff --git a/src/Services/YahooCom.php b/src/Services/YahooCom.php index 6ff47ef..505fe26 100644 --- a/src/Services/YahooCom.php +++ b/src/Services/YahooCom.php @@ -4,14 +4,14 @@ declare(strict_types=1); namespace bkrukowski\TransparentEmail\Services; -use bkrukowski\TransparentEmail\Emails\MutableEmail; +use bkrukowski\TransparentEmail\Emails\EditableEmail; use bkrukowski\TransparentEmail\Emails\EmailInterface; class YahooCom implements ServiceInterface { public function getPrimaryEmail(EmailInterface $email) : EmailInterface { - return (new MutableEmail($email)) + return (new EditableEmail($email)) ->removeSuffixAlias('-') ->lowerCaseLocalPartIf(true); } diff --git a/tests/Emails/MutableEmailTest.php b/tests/Emails/EditableEmailTest.php similarity index 63% rename from tests/Emails/MutableEmailTest.php rename to tests/Emails/EditableEmailTest.php index 05f973d..78e2cf0 100644 --- a/tests/Emails/MutableEmailTest.php +++ b/tests/Emails/EditableEmailTest.php @@ -2,11 +2,11 @@ namespace bkrukowski\TransparentEmail\Tests\Emails; -use bkrukowski\TransparentEmail\Emails\MutableEmail; +use bkrukowski\TransparentEmail\Emails\EditableEmail; use bkrukowski\TransparentEmail\Emails\Email; use bkrukowski\TransparentEmail\Emails\EmailInterface; -class MutableEmailTest extends \PHPUnit_Framework_TestCase +class EditableEmailTest extends \PHPUnit_Framework_TestCase { /** * @dataProvider providerRemoveFromLocalPart @@ -16,10 +16,11 @@ class MutableEmailTest extends \PHPUnit_Framework_TestCase */ public function testRemoveFromLocalPart(EmailInterface $email, string $toRemove) { - $editable = new MutableEmail($email); - $this->assertSame($editable, $editable->removeFromLocalPart($toRemove)); - $this->assertNotContains((string) $editable, $toRemove); - $this->assertSame($email->getDomain(), $editable->getDomain()); + $editable = new EditableEmail($email); + $new = $editable->removeFromLocalPart($toRemove); + $this->assertNotSame($editable, $new); + $this->assertNotContains((string) $new, $toRemove); + $this->assertSame($email->getDomain(), $new->getDomain()); } public function providerRemoveFromLocalPart() @@ -39,9 +40,10 @@ class MutableEmailTest extends \PHPUnit_Framework_TestCase */ public function testRemoveSuffixAlias(EmailInterface $email, string $delimiter, string $expected) { - $editable = new MutableEmail($email); - $this->assertSame($editable, $editable->removeSuffixAlias($delimiter)); - $this->assertEquals($expected, $editable); + $editable = new EditableEmail($email); + $new = $editable->removeSuffixAlias($delimiter); + $this->assertNotSame($editable, $new); + $this->assertEquals($expected, $new); $this->assertSame($email->getDomain(), $editable->getDomain()); } @@ -62,10 +64,11 @@ class MutableEmailTest extends \PHPUnit_Framework_TestCase */ public function testSetDomain(EmailInterface $email, string $domain) { - $editable = new MutableEmail($email); - $this->assertSame($editable, $editable->setDomain($domain)); - $this->assertSame($domain, $editable->getDomain()); - $this->assertSame($email->getLocalPart(), $editable->getLocalPart()); + $editable = new EditableEmail($email); + $new = $editable->setDomain($domain); + $this->assertNotSame($editable, $new); + $this->assertSame($domain, $new->getDomain()); + $this->assertSame($email->getLocalPart(), $new->getLocalPart()); } public function providerSetDomain() @@ -85,10 +88,11 @@ class MutableEmailTest extends \PHPUnit_Framework_TestCase */ public function testLocalPart(EmailInterface $email, string $localPart) { - $editable = new MutableEmail($email); - $this->assertSame($editable, $editable->setLocalPart($localPart)); - $this->assertSame($localPart, $editable->getLocalPart()); - $this->assertSame($email->getDomain(), $editable->getDomain()); + $editable = new EditableEmail($email); + $new = $editable->setLocalPart($localPart); + $this->assertNotSame($editable, $new); + $this->assertSame($localPart, $new->getLocalPart()); + $this->assertSame($email->getDomain(), $new->getDomain()); } public function providerSetLocalPart() @@ -105,14 +109,15 @@ class MutableEmailTest extends \PHPUnit_Framework_TestCase * * @param EmailInterface $email * @param bool $condition - * @param bool $expected + * @param bool $isLowerCase */ - public function testLowerCaseLocalPartIf(EmailInterface $email, bool $condition, bool $expected) + public function testLowerCaseLocalPartIf(EmailInterface $email, bool $condition, bool $isLowerCase) { - $editable = new MutableEmail($email); - $this->assertSame($editable, $editable->lowerCaseLocalPartIf($condition)); - $this->assertSame($expected, strtolower($editable->getLocalPart()) === $editable->getLocalPart()); - $this->assertSame($email->getDomain(), $editable->getDomain()); + $editable = new EditableEmail($email); + $new = $editable->lowerCaseLocalPartIf($condition); + $this->assertSame(!$condition, $editable === $new); + $this->assertSame($isLowerCase, strtolower($editable->getLocalPart()) === $new->getLocalPart()); + $this->assertSame($email->getDomain(), $new->getDomain()); } public function providerLowerCaseLocalPartIf()