diff --git a/.gitignore b/.gitignore index 6b2f3a9..e2dc179 100755 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /app/public/css/theme.css.map /app/public/vendor/ /src/tests/build/ +/training/ diff --git a/run b/run index 789ee0f..afcc88c 100755 --- a/run +++ b/run @@ -5,16 +5,17 @@ namespace Behavioral\Observer; require_once 'vendor/autoload.php'; -$team = new FootballSubject('Динамо'); +$team = new FootballSubject('Manchester United'); -$team->attachObserver(new FootballObserver('Петя')); -$team->attachObserver(new FootballObserver('Вася')); +$team->attachObserver(new FootballObserver('John')); +$team->attachObserver(new FootballObserver('Bill')); $team->notify(new FootballEvent(FootballEvent::GOAL)); $team->notify(new FootballEvent(FootballEvent::MISS)); -$team->notify(new FootballEvent(FootballEvent::FIRE)); +$team->notify(new FootballEvent(FootballEvent::CARD)); -$team->detachObserver('Вася'); -$team->attachObserver(new FootballObserver('Иван')); +$team->detachObserver('Bill'); +$team->attachObserver(new FootballObserver('Steve')); $team->notify(new FootballEvent(FootballEvent::GOAL)); +$team->notify(new FootballEvent('Offside')); diff --git a/src/FootballEvent.php b/src/FootballEvent.php index c95d735..56d7e02 100755 --- a/src/FootballEvent.php +++ b/src/FootballEvent.php @@ -1,6 +1,6 @@ @@ -18,9 +18,9 @@ class FootballEvent implements EventInterface { - const GOAL = 'goal'; - const MISS = 'miss a ball'; - const FIRE = 'fire'; + const GOAL = 'Goal!!!'; + const MISS = 'missing a ball((('; + const CARD = 'getting a yellow card'; /** * @var string @@ -33,7 +33,7 @@ class FootballEvent implements EventInterface */ public function __construct(string $eventName) { - $this->eventName = $eventName; + $this->eventName = $eventName; } /** diff --git a/src/FootballObserver.php b/src/FootballObserver.php index cfea46a..7a7c0b2 100755 --- a/src/FootballObserver.php +++ b/src/FootballObserver.php @@ -33,34 +33,6 @@ public function __construct(string $observerName) $this->observerName = $observerName; } - public function eventReaction(EventInterface $event, SubjectInterface $subject): void - { - switch ($event->getEventName()) { - case FootballEvent::GOAL: - printf( - "%s празнует ГОЛ!!! %s\n", - $this->getObserverName(), $subject->getSubjectName() - ); - break; - case FootballEvent::MISS: - printf( - "%s поддерживает %s после пропущенного мяча\n", - $this->getObserverName(), $subject->getSubjectName() - ); - break; - case FootballEvent::FIRE: - printf( - "%s поджигает файер\n", $this->getObserverName() - ); - break; - default: - printf( - "%s отреагировал на событие %s\n", - $this->getObserverName(), $subject->getSubjectName() - ); - } - } - /** * @return string */ diff --git a/src/FootballSubject.php b/src/FootballSubject.php index 0a2b022..c8b938a 100755 --- a/src/FootballSubject.php +++ b/src/FootballSubject.php @@ -1,6 +1,6 @@ @@ -62,7 +62,8 @@ public function detachObserver(string $observerName): void public function notify(EventInterface $event): void { foreach ($this->observers as $observer) { - $observer->eventReaction($event, $this); + printf("%s has get information about: %s %s \n", + $observer->getObserverName(), $this->getSubjectName(), $event->getEventName()); } } diff --git a/src/ObserverInterface.php b/src/ObserverInterface.php index c2303e9..fa802fe 100755 --- a/src/ObserverInterface.php +++ b/src/ObserverInterface.php @@ -18,12 +18,6 @@ interface ObserverInterface { - /** - * @param EventInterface $event - * @param SubjectInterface $subject - */ - public function eventReaction(EventInterface $event, SubjectInterface $subject): void; - /** * @return string */ diff --git a/src/tests/ObserverTest.php b/src/tests/ObserverTest.php index d581b42..d6b8411 100755 --- a/src/tests/ObserverTest.php +++ b/src/tests/ObserverTest.php @@ -23,43 +23,43 @@ class ObserverTest extends PHPUnit_Framework_TestCase protected function setUp(): void { - $this->team = new FootballSubject('Динамо'); + $this->team = new FootballSubject('Manchester United'); } public function testFootballSubject(): void { $this->assertInstanceOf(FootballSubject::class, $this->getTeam()); - $this->assertEquals('Динамо', $this->getTeam()->getSubjectName()); + $this->assertEquals('Manchester United', $this->getTeam()->getSubjectName()); } public function testTeamAction(): void { - $this->getTeam()->attachObserver(new FootballObserver('Петя')); - $this->getTeam()->attachObserver(new FootballObserver('Вася')); + $this->getTeam()->attachObserver(new FootballObserver('John')); + $this->getTeam()->attachObserver(new FootballObserver('Bill')); ob_start(); $this->getTeam()->notify(new FootballEvent(FootballEvent::GOAL)); $goal = ob_get_clean(); - $this->assertEquals($goal, "Петя празнует ГОЛ!!! Динамо\nВася празнует ГОЛ!!! Динамо\n"); + $this->assertEquals($goal, "John has get information about: Manchester United Goal!!! \nBill has get information about: Manchester United Goal!!! \n"); ob_start(); $this->getTeam()->notify(new FootballEvent(FootballEvent::MISS)); $miss = ob_get_clean(); - $this->assertEquals($miss, "Петя поддерживает Динамо после пропущенного мяча\nВася поддерживает Динамо после пропущенного мяча\n"); + $this->assertEquals($miss, "John has get information about: Manchester United missing a ball((( \nBill has get information about: Manchester United missing a ball((( \n"); ob_start(); - $this->getTeam()->notify(new FootballEvent(FootballEvent::FIRE)); - $fire = ob_get_clean(); + $this->getTeam()->notify(new FootballEvent(FootballEvent::CARD)); + $card = ob_get_clean(); - $this->assertEquals($fire, "Петя поджигает файер\nВася поджигает файер\n"); + $this->assertEquals($card, "John has get information about: Manchester United getting a yellow card \nBill has get information about: Manchester United getting a yellow card \n"); ob_start(); $this->getTeam()->notify(new FootballEvent('random')); $random = ob_get_clean(); - $this->assertEquals($random, "Петя отреагировал на событие Динамо\nВася отреагировал на событие Динамо\n"); + $this->assertEquals($random, "John has get information about: Manchester United random \nBill has get information about: Manchester United random \n"); } public function testDetachObserver()