Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53404b1

Browse files
[php][user_email-04_email_address_vo] Move logic to EmailAddress
1 parent b3fe068 commit 53404b1

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

‎examples/php/php-user_email-04_email_address_vo/src/Controller/NewsletterController.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace CodelyTv\Controller;
66

7+
use CodelyTv\Model\EmailAddress;
78
use CodelyTv\Model\Newsletter;
89
use CodelyTv\Model\User;
910

1011
final class NewsletterController
1112
{
1213
public function post(string $emailAddress): Newsletter
1314
{
14-
User::ensureEmailIsValid($emailAddress);
15+
User::ensureEmailIsValid(newEmailAddress($emailAddress));
1516

1617
return new Newsletter($emailAddress);
1718
}

‎examples/php/php-user_email-04_email_address_vo/src/Controller/UserController.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace CodelyTv\Controller;
66

7+
use CodelyTv\Model\EmailAddress;
78
use CodelyTv\Model\User;
89

910
final class UserController
1011
{
1112
public function post(string $emailAddress): User
1213
{
13-
return new User($emailAddress);
14+
return new User(newEmailAddress($emailAddress));
1415
}
1516
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\Model;
6+
7+
use InvalidArgumentException;
8+
9+
final class EmailAddress
10+
{
11+
private string $value;
12+
13+
public function __construct(string $value)
14+
{
15+
$this->ensureEmailIsNotEmpty($value);
16+
$this->ensureEmailIsFormattedCorrectly($value);
17+
18+
$this->value = $value;
19+
}
20+
21+
public function emailHasCommonProvider(): bool
22+
{
23+
return strpos($this->value(), '@yahoo')
24+
|| strpos($this->value(), '@gmail')
25+
|| strpos($this->value(), '@outlook');
26+
}
27+
28+
public function value(): string
29+
{
30+
return $this->value;
31+
}
32+
33+
private function ensureEmailIsNotEmpty(string $emailAddress): void
34+
{
35+
if ('' === $emailAddress) {
36+
throw new InvalidArgumentException('The email address is empty');
37+
}
38+
}
39+
40+
private function ensureEmailIsFormattedCorrectly(string $emailAddress): void
41+
{
42+
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
43+
throw new InvalidArgumentException("The email address <$emailAddress> is not valid");
44+
}
45+
}
46+
}

‎examples/php/php-user_email-04_email_address_vo/src/Model/User.php‎

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,25 @@
88

99
final class User
1010
{
11-
private string $emailAddress;
11+
private EmailAddress $emailAddress;
1212

13-
public function __construct(string $emailAddress)
13+
public function __construct(EmailAddress $emailAddress)
1414
{
1515
self::ensureEmailIsValid($emailAddress);
1616

1717
$this->emailAddress = $emailAddress;
1818
}
1919

20-
public static function ensureEmailIsValid(string $emailAddress): void
20+
public static function ensureEmailIsValid(EmailAddress $emailAddress): void
2121
{
22-
self::ensureEmailIsNotEmpty($emailAddress);
23-
self::ensureEmailIsFormattedCorrectly($emailAddress);
24-
self::ensureEmailHasCommonProvider($emailAddress);
25-
}
26-
27-
private static function ensureEmailIsNotEmpty(string $emailAddress): void
28-
{
29-
if ('' === $emailAddress) {
30-
throw new InvalidArgumentException('The email address is empty');
31-
}
32-
}
33-
34-
private static function ensureEmailIsFormattedCorrectly(string $emailAddress): void
35-
{
36-
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
37-
throw new InvalidArgumentException("The email address <$emailAddress> is not valid");
38-
}
39-
}
40-
41-
private static function emailHasCommonProvider(string $emailAddress): bool
42-
{
43-
return strpos($emailAddress, '@yahoo') || strpos($emailAddress, '@gmail') || strpos($emailAddress, '@outlook');
44-
}
45-
46-
private static function ensureEmailHasCommonProvider(string $emailAddress): void
47-
{
48-
if (!self::emailHasCommonProvider($emailAddress)) {
49-
throw new InvalidArgumentException("The email address <$emailAddress> has not a common provider");
22+
if (!$emailAddress->emailHasCommonProvider()) {
23+
throw new InvalidArgumentException(
24+
"The email address <{$emailAddress->value()}> has not a common provider"
25+
);
5026
}
5127
}
5228

53-
public function emailAddress(): string
29+
public function emailAddress(): EmailAddress
5430
{
5531
return $this->emailAddress;
5632
}

‎examples/php/php-user_email-04_email_address_vo/tests/Controller/UserControllerTest.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CodelyTv\Tests\Controller;
66

77
use CodelyTv\Controller\UserController;
8+
use CodelyTv\Model\EmailAddress;
89
use CodelyTv\Model\User;
910
use InvalidArgumentException;
1011
use PHPUnit\Framework\TestCase;
@@ -18,7 +19,7 @@ public function it_should_create_a_valid_user(): void
1819

1920
$emailAddress = 'codely@gmail.com';
2021

21-
self::assertEquals(new User('codely@gmail.com'), $controller->post($emailAddress));
22+
self::assertEquals(new User(newEmailAddress('codely@gmail.com')), $controller->post($emailAddress));
2223
}
2324

2425
/** @test */

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /