Skip to content

Commit dc70329

Browse files
committed
[php][user_email-02_extract_method] Extract guard clauses
1 parent 9d679af commit dc70329

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

examples/php/php-user_email-02_extract_method/src/Controller/UserController.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,37 @@
1010
final class UserController
1111
{
1212
public function post(string $emailAddress): User
13+
{
14+
$this->ensureEmailIsNotEmpty($emailAddress);
15+
$this->ensureEmailIsFormattedCorrectly($emailAddress);
16+
$this->ensureEmailHasCommonProvider($emailAddress);
17+
18+
return new User($emailAddress);
19+
}
20+
21+
private function ensureEmailIsNotEmpty(string $emailAddress): void
1322
{
1423
if ('' === $emailAddress) {
1524
throw new InvalidArgumentException('The email address is empty');
1625
}
26+
}
1727

28+
private function ensureEmailIsFormattedCorrectly(string $emailAddress): void
29+
{
1830
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
1931
throw new InvalidArgumentException("The email address <$emailAddress> is not valid");
2032
}
33+
}
2134

22-
if (!(strpos($emailAddress, '@yahoo') || strpos($emailAddress, '@gmail') || strpos($emailAddress, '@outlook'))) {
35+
private function ensureEmailHasCommonProvider(string $emailAddress): void
36+
{
37+
if (!$this->emailHasCommonProvider($emailAddress)) {
2338
throw new InvalidArgumentException("The email address <$emailAddress> has not a common provider");
2439
}
40+
}
2541

26-
return new User($emailAddress);
42+
private function emailHasCommonProvider(string $emailAddress): bool
43+
{
44+
return strpos($emailAddress, '@yahoo') || strpos($emailAddress, '@gmail') || strpos($emailAddress, '@outlook');
2745
}
2846
}

0 commit comments

Comments
 (0)