Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/app/public/css/theme.css.map
/app/public/vendor/
/src/tests/build/
/training/
13 changes: 7 additions & 6 deletions run
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
10 changes: 5 additions & 5 deletions src/FootballEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/**
* @author : Korotkov Danila <dankorot@gmail.com>
Expand All @@ -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
Expand All @@ -33,7 +33,7 @@ class FootballEvent implements EventInterface
*/
public function __construct(string $eventName)
{
$this->eventName = $eventName;
$this->eventName = $eventName;
}

/**
Expand Down
28 changes: 0 additions & 28 deletions src/FootballObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
5 changes: 3 additions & 2 deletions src/FootballSubject.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/**
* @author : Korotkov Danila <dankorot@gmail.com>
Expand Down Expand Up @@ -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());
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/ObserverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
interface ObserverInterface
{

/**
* @param EventInterface $event
* @param SubjectInterface $subject
*/
public function eventReaction(EventInterface $event, SubjectInterface $subject): void;

/**
* @return string
*/
Expand Down
20 changes: 10 additions & 10 deletions src/tests/ObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down