Skip to content

Commit

Permalink
decorator pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
reinerttomas committed Mar 8, 2024
1 parent 33ac8ab commit e2c7bcc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
6 changes: 3 additions & 3 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ services:
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

App\Game:
calls:
- subscribe: [ '@App\Observer\XpEarnedObserver' ]
# App\Game:
# calls:
# - subscribe: [ '@App\Observer\XpEarnedObserver' ]
7 changes: 7 additions & 0 deletions src/Command/GameCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use App\Character\CharacterType;
use App\Fight;
use App\Game;
use App\Observer\XpEarnedObserver;
use App\Service\OutputtingXpCalculator;
use App\Service\XpCalculator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -25,6 +28,10 @@ public function __construct(

protected function execute(InputInterface $input, OutputInterface $output): int
{
$xpCalculator = new XpCalculator();
$xpCalculator = new OutputtingXpCalculator($xpCalculator);
$this->game->subscribe(new XpEarnedObserver($xpCalculator));

$io = new SymfonyStyle($input, $output);

$io->text('Welcome to the game where warriors fight against each other for honor and glory... and 🍕!');
Expand Down
4 changes: 2 additions & 2 deletions src/Observer/XpEarnedObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace App\Observer;

use App\Fight;
use App\Service\XpCalculator;
use App\Service\XpCalculatorInterface;

readonly class XpEarnedObserver implements CanObserverFight
{
public function __construct(private XpCalculator $xpCalculator)
public function __construct(private XpCalculatorInterface $xpCalculator)
{
}

Expand Down
34 changes: 34 additions & 0 deletions src/Service/OutputtingXpCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Service;

use App\Character\Character;
use Symfony\Component\Console\Output\ConsoleOutput;

class OutputtingXpCalculator implements XpCalculatorInterface
{
public function __construct(
private readonly XpCalculatorInterface $innerCalculator,
) {
}

public function addXp(Character $winner, int $enemyLevel): void
{
$beforeLevel = $winner->getLevel();

$this->innerCalculator->addXp($winner, $enemyLevel);

$afterLevel = $winner->getLevel();

if ($beforeLevel !== $afterLevel) {
$output = new ConsoleOutput();

$output->writeln('--------------------------------');
$output->writeln('<bg=green;fg=white>Congratulations! You\'ve leveled up!</>');
$output->writeln(sprintf('You are now level "%d"', $winner->getLevel()));
$output->writeln('--------------------------------');
}
}
}
2 changes: 1 addition & 1 deletion src/Service/XpCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use App\Character\Character;

class XpCalculator
class XpCalculator implements XpCalculatorInterface
{
public function addXp(Character $winner, int $enemyLevel): void
{
Expand Down
12 changes: 12 additions & 0 deletions src/Service/XpCalculatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Service;

use App\Character\Character;

interface XpCalculatorInterface
{
public function addXp(Character $winner, int $enemyLevel): void;
}

0 comments on commit e2c7bcc

Please sign in to comment.