Skip to content

Commit 5186e41

Browse files
committed
Changes after code review
- replace EmailRecipient and SmsRecipient with corresponding traits - loosen restrictions for custructing Recipients - remove email adress and phone number validation - adjust unit tests accordingly
1 parent df233a4 commit 5186e41

File tree

8 files changed

+47
-212
lines changed

8 files changed

+47
-212
lines changed

src/Symfony/Component/Notifier/Recipient/EmailRecipient.php renamed to src/Symfony/Component/Notifier/Recipient/EmailRecipientTrait.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@
1616
*
1717
* @experimental in 5.1
1818
*/
19-
class EmailRecipient implements EmailRecipientInterface
19+
trait EmailRecipientTrait
2020
{
21-
use EmailValidationTrait;
22-
23-
private $email;
24-
25-
public function __construct(string $email)
26-
{
27-
self::ensureEmailIsValid($email);
28-
29-
$this->email = $email;
30-
}
21+
protected $email;
3122

3223
public function getEmail(): string
3324
{

src/Symfony/Component/Notifier/Recipient/EmailValidationTrait.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Symfony/Component/Notifier/Recipient/PhoneValidationTrait.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Symfony/Component/Notifier/Recipient/Recipient.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Notifier\Recipient;
1313

14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
1416
/**
1517
* @author Fabien Potencier <fabien@symfony.com>
1618
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
@@ -19,28 +21,36 @@
1921
*/
2022
class Recipient implements EmailRecipientInterface, SmsRecipientInterface
2123
{
22-
use EmailValidationTrait;
23-
use PhoneValidationTrait;
24-
25-
private $email;
26-
private $phone;
24+
use EmailRecipientTrait;
25+
use SmsRecipientTrait;
2726

28-
public function __construct(string $email, string $phone)
27+
public function __construct(string $email = '', string $phone = '')
2928
{
30-
self::ensureEmailIsValid($email);
31-
self::ensurePhoneIsValid($phone);
29+
if (empty(trim($email)) && empty(trim($phone))) {
30+
throw new InvalidArgumentException(sprintf('"%s" needs an email or a phone but both cannot be empty.', static::class));
31+
}
3232

3333
$this->email = $email;
3434
$this->phone = $phone;
3535
}
3636

37-
public function getEmail(): string
37+
/**
38+
* @return $this
39+
*/
40+
public function email(string $email): self
3841
{
39-
return $this->email;
42+
$this->email = $email;
43+
44+
return $this;
4045
}
4146

42-
public function getPhone(): string
47+
/**
48+
* @return $this
49+
*/
50+
public function phone(string $phone): self
4351
{
44-
return $this->phone;
52+
$this->phone = $phone;
53+
54+
return $this;
4555
}
4656
}

src/Symfony/Component/Notifier/Recipient/SmsRecipient.php renamed to src/Symfony/Component/Notifier/Recipient/SmsRecipientTrait.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@
1616
*
1717
* @experimental in 5.1
1818
*/
19-
class SmsRecipient implements SmsRecipientInterface
19+
trait SmsRecipientTrait
2020
{
21-
use PhoneValidationTrait;
22-
23-
private $phone;
24-
25-
public function __construct(string $phone)
26-
{
27-
self::ensurePhoneIsValid($phone);
28-
29-
$this->phone = $phone;
30-
}
21+
protected $phone;
3122

3223
public function getPhone(): string
3324
{

src/Symfony/Component/Notifier/Tests/Recipient/EmailRecipientTest.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Symfony/Component/Notifier/Tests/Recipient/RecipientTest.php

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,42 @@
2020
*/
2121
class RecipientTest extends TestCase
2222
{
23-
public function testCanBeConstructed()
23+
public function testCannotBeConstructedWithoutEmailAndWithoutPhone()
2424
{
25-
$recipient = new Recipient('test@test.de', '+0815');
25+
$this->expectException(InvalidArgumentException::class);
2626

27-
$this->assertSame('test@test.de', $recipient->getEmail());
28-
$this->assertSame('+0815', $recipient->getPhone());
27+
new Recipient('', '');
2928
}
3029

3130
/**
32-
* @dataProvider provideInvalidEmailData
31+
* @dataProvider provideValidEmailAndPhone
3332
*/
34-
public function testCannotBeConstructedWithInvalidEmail(string $email)
33+
public function testCanBeConstructed(string $email, string $phone)
3534
{
36-
$this->expectException(InvalidArgumentException::class);
35+
$recipient = new Recipient($email, $phone);
3736

38-
new Recipient($email, '+0815');
37+
$this->assertSame($email, $recipient->getEmail());
38+
$this->assertSame($phone, $recipient->getPhone());
3939
}
4040

41-
public function provideInvalidEmailData(): \Generator
41+
public function provideValidEmailAndPhone()
4242
{
43-
yield [''];
44-
yield ['test'];
45-
yield ['test@'];
43+
yield ['test@test.de', '+0815'];
44+
yield ['test@test.de', ''];
45+
yield ['', '+0815'];
4646
}
4747

48-
/**
49-
* @dataProvider provideInvalidPhoneData
50-
*/
51-
public function testCannotBeConstructedWithInvalidPhone(string $phone)
48+
public function testEmailAndPhoneAreNotImmutable()
5249
{
53-
$this->expectException(InvalidArgumentException::class);
50+
$recipient = new Recipient('test@test.de', '+0815');
5451

55-
new Recipient('test@test.de', $phone);
56-
}
52+
$this->assertSame('test@test.de', $recipient->getEmail());
53+
$this->assertSame('+0815', $recipient->getPhone());
5754

58-
public function provideInvalidPhoneData(): \Generator
59-
{
60-
yield [''];
61-
yield ['0815'];
62-
yield ['qwert'];
55+
$recipient->email('test@test.com');
56+
$recipient->phone('+49815');
57+
58+
$this->assertSame('test@test.com', $recipient->getEmail());
59+
$this->assertSame('+49815', $recipient->getPhone());
6360
}
6461
}

src/Symfony/Component/Notifier/Tests/Recipient/SmsRecipientTest.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)