diff --git a/src/CancelCourseEvent.php b/src/CancelCourseEvent.php new file mode 100644 index 0000000..fa10eb7 --- /dev/null +++ b/src/CancelCourseEvent.php @@ -0,0 +1,8 @@ +notifyChannels = $notifyChannels; + } + + public function dispatch(User $user) + { + foreach ($this->notifyChannels as $notifyChannel) { + $message = $user->getLanguage()->getContent(get_called_class()); + $notifyChannel->notify($message); + } + } +} \ No newline at end of file diff --git a/src/HelloWorld.php b/src/HelloWorld.php index 595152d..540fee4 100644 --- a/src/HelloWorld.php +++ b/src/HelloWorld.php @@ -6,6 +6,6 @@ class HelloWorld { public function say() { - return 'Hello, world'; + return 'Hello, 123'; } } \ No newline at end of file diff --git a/src/Language.php b/src/Language.php new file mode 100644 index 0000000..4914fe8 --- /dev/null +++ b/src/Language.php @@ -0,0 +1,8 @@ + 'register success', + ReserveClassEvent::class => 'reserve class', + CancelCourseEvent::class => 'cancel class', + ]; + + return $map[$eventName]; + } +} \ No newline at end of file diff --git a/src/LanguageZHTW.php b/src/LanguageZHTW.php new file mode 100644 index 0000000..bbc04e8 --- /dev/null +++ b/src/LanguageZHTW.php @@ -0,0 +1,23 @@ + '註冊成功', + ReserveClassEvent::class => '預約課程', + CancelCourseEvent::class => '取消課程', + ]; + + return $map[$eventName]; + } +} + diff --git a/src/Notifychannel.php b/src/Notifychannel.php new file mode 100644 index 0000000..b68a7b8 --- /dev/null +++ b/src/Notifychannel.php @@ -0,0 +1,8 @@ +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); + } +} \ No newline at end of file diff --git a/src/index.php b/src/index.php index 0bfaa13..2f2d6b4 100644 --- a/src/index.php +++ b/src/index.php @@ -2,6 +2,38 @@ require_once 'vendor/autoload.php'; use App\HelloWorld; +use App\LanguageENUS; +use App\LanguageZHTW; +use App\User; -$helloWorld = new HelloWorld(); -echo $helloWorld->say(); \ No newline at end of file +// $helloWorld = new HelloWorld(); +// echo $helloWorld->say(); +// $helloWorld = new HelloWorld(); +// echo $helloWorld->say(); + + +$rick = new User(new LanguageENUS()); +$morty = new User(new LanguageZHTW()); + +// // 註冊成功: email & sms +// // [email] register success +// // [sms] register success +$rick->register(); + +// // [email] 註冊成功 +// // [sms] 註冊成功 +$morty->register(); + + +// 學生預約課程: email & telegram +// [email] reserve class +// [telegram] reserve class +$rick->reserve(); + +// [email] 學生預約課程 +// [telegram] 學生預約課程 +$morty->reserve(); + +// 學生取消課程: email & telegram +$rick->cancel(); +$morty->cancel(); \ No newline at end of file