1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\DominoGame; |
6
|
|
|
|
7
|
|
|
use Arp\DominoGame\Exception\DominoGameException; |
8
|
|
|
use Arp\DominoGame\Value\Player; |
9
|
|
|
use Arp\DominoGame\Value\PlayerCollection; |
10
|
|
|
use Laminas\Log\Formatter\Simple; |
11
|
|
|
use Laminas\Log\Logger; |
12
|
|
|
use Laminas\Log\PsrLoggerAdapter; |
13
|
|
|
use Laminas\Log\Writer\Stream; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
|
16
|
|
|
class DominoGameFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Create a new domino game with the provided player names. |
20
|
|
|
* |
21
|
|
|
* @todo Move to factory class |
22
|
|
|
* |
23
|
|
|
* @param array $playerNames |
24
|
|
|
* @param int $maxTileSize |
25
|
|
|
* @param LoggerInterface|null $logger |
26
|
|
|
* |
27
|
|
|
* @return DominoGame |
28
|
|
|
* @throws DominoGameException |
29
|
|
|
*/ |
30
|
|
|
public function create(array $playerNames, int $maxTileSize, LoggerInterface $logger = null): DominoGame |
31
|
|
|
{ |
32
|
|
|
$players = []; |
33
|
|
|
foreach ($playerNames as $playerName) { |
34
|
|
|
// @todo Sanitisation $playerName? |
35
|
|
|
$players[] = new Player($playerName); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$logger = $logger ?? $this->createDefaultLogger(); |
39
|
|
|
|
40
|
|
|
return new DominoGame(new PlayerCollection($players), $logger, $maxTileSize); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a simple default logger that will render the actions to stdout. |
45
|
|
|
* |
46
|
|
|
* @return LoggerInterface |
47
|
|
|
*/ |
48
|
|
|
private function createDefaultLogger(): LoggerInterface |
49
|
|
|
{ |
50
|
|
|
$logger = new Logger(); |
51
|
|
|
|
52
|
|
|
$writer = new Stream('php://stdout'); |
53
|
|
|
$writer->setFormatter(new Simple('%message%')); |
54
|
|
|
$logger->addWriter($writer); |
55
|
|
|
|
56
|
|
|
return new PsrLoggerAdapter($logger); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|