Skip to content

Commit fbff336

Browse files
committed
add observer pattern and related test
1 parent 8df8acf commit fbff336

File tree

8 files changed

+142
-1
lines changed

8 files changed

+142
-1
lines changed

app/Controller/BehavioralController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public function command()
2424

2525
public function iterator()
2626
{
27-
// (new \App\Factory\Behavioral\Iterator\UserRoles\Application)->run();
2827
(new \App\Factory\Behavioral\Iterator\HotelSuppliers\Application)->run();
2928
}
29+
30+
public function observer()
31+
{
32+
(new \App\Factory\Behavioral\Observer\NewUserSignedup\Application)->run();
33+
}
3034
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace App\Factory\Behavioral\Observer\NewUserSignedup;
2+
3+
class Application
4+
{
5+
public function run()
6+
{
7+
$observer1 = new ConfirmWhatsapp();
8+
$observer2 = new SendConfirmationEmail();
9+
10+
$observable = new NewUserSignedup(new User('[email protected]', '123123123'));
11+
$observable->attach($observer1);
12+
$observable->attach($observer2);
13+
$observable->notify();
14+
echo '<pre>Whatsapp observer detached';
15+
$observable->detach($observer1);
16+
17+
$observable->setUser(new User('[email protected]', '123123123'));
18+
$observable->notify();
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php namespace App\Factory\Behavioral\Observer\NewUserSignedup;
2+
3+
class ConfirmWhatsapp implements \SplObserver
4+
{
5+
public function update(\SplSubject $subject)
6+
{
7+
echo '<pre>Sending text to: ' . $subject->getUser()->getPhoneNo();
8+
}
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php namespace App\Factory\Behavioral\Observer\NewUserSignedup;
2+
3+
class NewUserSignedup implements \SplSubject
4+
{
5+
private $observers = [];
6+
private $_user;
7+
8+
public function __construct(User $user)
9+
{
10+
$this->_user = $user;
11+
}
12+
13+
public function attach(\SplObserver $observer)
14+
{
15+
$this->observers[] = $observer;
16+
}
17+
18+
public function detach(\SplObserver $observer)
19+
{
20+
foreach($this->observers as $k => $obs) {
21+
if($observer === $obs) {
22+
unset($this->observers[$k]);
23+
break;
24+
}
25+
}
26+
}
27+
28+
public function notify(): void
29+
{
30+
foreach($this->observers as $observer) {
31+
$observer->update($this);
32+
}
33+
}
34+
35+
public function setUser(User $user)
36+
{
37+
$this->_user = $user;
38+
}
39+
40+
public function getUser()
41+
{
42+
return $this->_user;
43+
}
44+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php namespace App\Factory\Behavioral\Observer\NewUserSignedup;
2+
3+
class SendConfirmationEmail implements \SplObserver
4+
{
5+
public function update(\SplSubject $subject)
6+
{
7+
echo '<pre>Sending email to: ' . $subject->getUser()->getEmail();
8+
}
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php namespace App\Factory\Behavioral\Observer\NewUserSignedup;
2+
3+
class User
4+
{
5+
private $_email;
6+
private $_phoneNum;
7+
8+
public function __construct(string $email, string $phoneNum)
9+
{
10+
$this->_email = $email;
11+
$this->_phoneNum = $phoneNum;
12+
}
13+
14+
public function getPhoneNo()
15+
{
16+
return $this->_phoneNum;
17+
}
18+
19+
public function getEmail()
20+
{
21+
return $this->_email;
22+
}
23+
}

bootstraps/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
['GET', '/behavioral', ['App\Controller\BehavioralController', 'chainOfResponsibility']],
2222
['GET', '/behavioral/command', ['App\Controller\BehavioralController', 'command']],
2323
['GET', '/behavioral/iterator', ['App\Controller\BehavioralController', 'iterator']],
24+
['GET', '/behavioral/observer', ['App\Controller\BehavioralController', 'observer']],
2425
// ['GET', '/{slug}', ['App\Controller\CreationalController', 'show']],
2526
];

tests/ObserverPatternTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use App\Factory\Behavioral\Observer\NewUserSignedup\{
5+
User,
6+
NewUserSignedup,
7+
ConfirmWhatsapp,
8+
SendConfirmationEmail
9+
};
10+
11+
class ObserverPatternTest extends TestCase {
12+
public function testUserCanNotifyWhatsappObserver()
13+
{
14+
$user = new User('[email protected]', '123123123');
15+
$observable = new NewUserSignedup($user);
16+
$observable->attach(new ConfirmWhatsapp);
17+
18+
$this->expectOutputString('Sending text to: ' . $user->getPhoneNo());
19+
$observable->notify();
20+
}
21+
22+
public function testUserCanNotifyEmailObserver()
23+
{
24+
$user = new User('[email protected]', '123123123');
25+
$observable = new NewUserSignedup($user);
26+
$observable->attach(new SendConfirmationEmail);
27+
28+
$this->expectOutputString('Sending email to: ' . $user->getEmail());
29+
$observable->notify();
30+
}
31+
}

0 commit comments

Comments
 (0)