forked from r3nyou/devmentor-BE101-PHP
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
237 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class CancelCourseEvent extends Event | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
|
||
class Email extends Notifychannel | ||
{ | ||
public function notify(string $message) | ||
{ | ||
echo '[Email]' . $message . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class Event | ||
{ | ||
// /** | ||
// * @var NotifyChannel[] | ||
// */ | ||
private $notifyChannels; | ||
|
||
public function __construct(array $notifyChannels) | ||
{ | ||
$this->notifyChannels = $notifyChannels; | ||
} | ||
|
||
public function dispatch(User $user) | ||
{ | ||
foreach ($this->notifyChannels as $notifyChannel) { | ||
$message = $user->getLanguage()->getContent(get_called_class()); | ||
$notifyChannel->notify($message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,6 @@ class HelloWorld | |
{ | ||
public function say() | ||
{ | ||
return 'Hello, world'; | ||
return 'Hello, 123'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
abstract class Language | ||
{ | ||
public abstract function getContent($eventName): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use App\Language; | ||
use App\CancelCourseEvent; | ||
use App\ReserveClassEvent; | ||
use App\RegisterSuccessEvent; | ||
|
||
class LanguageENUS extends Language | ||
{ | ||
public function getContent($eventName): string | ||
{ | ||
$map = [ | ||
RegisterSuccessEvent::class => 'register success', | ||
ReserveClassEvent::class => 'reserve class', | ||
CancelCourseEvent::class => 'cancel class', | ||
]; | ||
|
||
return $map[$eventName]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use App\Language; | ||
use App\CancelCourseEvent; | ||
use App\ReserveClassEvent; | ||
use App\RegisterSuccessEvent; | ||
|
||
class LanguageZHTW extends Language | ||
{ | ||
public function getContent($eventName): string | ||
{ | ||
$map = [ | ||
RegisterSuccessEvent::class => '註冊成功', | ||
ReserveClassEvent::class => '預約課程', | ||
CancelCourseEvent::class => '取消課程', | ||
]; | ||
|
||
return $map[$eventName]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
abstract class NotifyChannel | ||
{ | ||
public abstract function notify(string $message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class RegisterSuccessEvent extends Event | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class ReserveClassEvent extends Event | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class Sms extends NotifyChannel | ||
{ | ||
public function notify(string $message) | ||
{ | ||
echo '[SMS]' . $message . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class Student extends User | ||
{ | ||
public function reserve() | ||
{ | ||
|
||
} | ||
|
||
public function cancel() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class Telegram extends NotifyChannel | ||
{ | ||
public function notify(string $message) | ||
{ | ||
echo '[Telegram]' . $message . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use App\Sms; | ||
use App\Email; | ||
use App\Language; | ||
use App\RegisterSuccessEvent; | ||
|
||
class User | ||
{ | ||
protected $language; | ||
|
||
public function __construct(Language $language) | ||
{ | ||
$this->language = $language; | ||
} | ||
|
||
public function getLanguage() | ||
{ | ||
return $this->language; | ||
} | ||
|
||
public function register() | ||
{ | ||
$notifyChannels = [new Email(), new Sms()]; | ||
$event = new RegisterSuccessEvent($notifyChannels); | ||
$event->dispatch($this); | ||
} | ||
|
||
public function cancel() | ||
{ | ||
$notifyChannels = [new Email(), new Telegram()]; | ||
$event = new CancelCourseEvent($notifyChannels); | ||
$event->dispatch($this); | ||
} | ||
public function reserve() | ||
{ | ||
$notifyChannels = [new Email(), new Telegram()]; | ||
$event = new ReserveClassEvent($notifyChannels); | ||
$event->dispatch($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters