Skip to content

Commit be356d2

Browse files
committed
Reorganize parameters
1 parent dbdd4b9 commit be356d2

File tree

5 files changed

+45
-46
lines changed

5 files changed

+45
-46
lines changed

src/DiagramGenerator/Board.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ public function getImage()
7878
protected function generateImage()
7979
{
8080
if ($this->config->hasThemeUrls()) {
81-
$storage = new StorageNew($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
81+
$storage = new StorageNew($this->cacheDir, $this->config);
8282
} else {
8383
$storage = new Storage($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
8484
}
8585
$image = new Image($storage, $this->config);
86-
$topPadding = $storage->getMaxPieceHeight($this->fen, $this->config) - $this->config->getSize()->getCell();
86+
$topPadding = $storage->getMaxPieceHeight($this->fen) - $this->config->getSize()->getCell();
8787

8888
$image->drawBoardWithFigures(
8989
$this->fen,

src/DiagramGenerator/Image/Image.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function addCoordinates(int $topPaddingOfCell, bool $isCoordinatesInside
135135
*/
136136
public function drawBoardWithFigures(Fen $fen, $cellSize, $topPaddingOfCell)
137137
{
138-
$this->image = $this->drawBoard($this->storage->getBackgroundTextureImage($this->config), $cellSize, $topPaddingOfCell);
138+
$this->image = $this->drawBoard($this->storage->getBackgroundTextureImage(), $cellSize, $topPaddingOfCell);
139139

140140
$boardHasTexture = !empty($this->config->getTexture()) ||
141141
($this->config->hasThemeUrls() && isset($this->config->getThemeUrls()['board']));
@@ -226,7 +226,7 @@ protected function drawFigures(Fen $fen, Config $config, $topPaddingOfCell)
226226
$cellSize = $config->getSize()->getCell();
227227

228228
foreach ($fen->getPieces() as $piece) {
229-
$pieceImage = $this->storage->getPieceImage($piece, $config);
229+
$pieceImage = $this->storage->getPieceImage($piece);
230230

231231
$this->image = $this->image->insert(
232232
$pieceImage,

src/DiagramGenerator/Image/StorageNew.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,36 @@ class StorageNew
1818
/** @var string */
1919
protected $cacheDirectory;
2020

21-
/** @var string */
22-
protected $pieceThemeUrl;
23-
24-
/** @var string */
25-
protected $boardTextureUrl;
21+
/** @var Config */
22+
protected $config;
2623

2724
/**
2825
* @param string $cacheDirectory
29-
* @param string $pieceThemeUrl
30-
* @param string $boardTextureUrl
26+
* @param Config $config
3127
*/
32-
public function __construct($cacheDirectory, $pieceThemeUrl, $boardTextureUrl)
28+
public function __construct($cacheDirectory, Config $config)
3329
{
3430
$this->cacheDirectory = $cacheDirectory;
35-
$this->pieceThemeUrl = $pieceThemeUrl;
36-
$this->boardTextureUrl = $boardTextureUrl;
31+
$this->config = $config;
3732
}
3833

3934
/**
4035
*
4136
* @return Image
4237
*/
43-
public function getPieceImage(Piece $piece, Config $config)
38+
public function getPieceImage(Piece $piece)
4439
{
45-
return $this->getPieceImageFromTheme($piece, $config);
40+
return $this->getPieceImageFromTheme($piece);
4641
}
4742

4843
/**
4944
* Gets piece image from theme URLs.
5045
*
5146
* @return Image
5247
*/
53-
protected function getPieceImageFromTheme(Piece $piece, Config $config)
48+
protected function getPieceImageFromTheme(Piece $piece)
5449
{
55-
$themeUrls = $config->getThemeUrls();
50+
$themeUrls = $this->config->getThemeUrls();
5651
$pieceShortName = $piece->getShortName();
5752

5853
if (!isset($themeUrls[$pieceShortName])) {
@@ -63,7 +58,7 @@ protected function getPieceImageFromTheme(Piece $piece, Config $config)
6358
$cacheKey = $pieceUrl;
6459

6560
if (!isset($this->pieces[$cacheKey])) {
66-
$this->pieces[$cacheKey] = $this->fetchRemotePieceImageFromTheme($piece, $config);
61+
$this->pieces[$cacheKey] = $this->fetchRemotePieceImageFromTheme($piece);
6762
}
6863

6964
return $this->pieces[$cacheKey];
@@ -72,19 +67,19 @@ protected function getPieceImageFromTheme(Piece $piece, Config $config)
7267
/**
7368
* @return Image|null
7469
*/
75-
public function getBackgroundTextureImage(Config $config)
70+
public function getBackgroundTextureImage()
7671
{
77-
return $this->getBackgroundTextureImageFromTheme($config);
72+
return $this->getBackgroundTextureImageFromTheme();
7873
}
7974

8075
/**
8176
* Gets background texture image from theme URL.
8277
*
8378
* @return Image|null
8479
*/
85-
protected function getBackgroundTextureImageFromTheme(Config $config)
80+
protected function getBackgroundTextureImageFromTheme()
8681
{
87-
$themeUrls = $config->getThemeUrls();
82+
$themeUrls = $this->config->getThemeUrls();
8883

8984
if (!isset($themeUrls['board'])) {
9085
return null;
@@ -108,11 +103,11 @@ protected function getBackgroundTextureImageFromTheme(Config $config)
108103
*
109104
* @return int
110105
*/
111-
public function getMaxPieceHeight(Fen $fen, Config $config)
106+
public function getMaxPieceHeight(Fen $fen)
112107
{
113-
$maxHeight = $config->getSize()->getCell();
108+
$maxHeight = $this->config->getSize()->getCell();
114109
foreach ($fen->getPieces() as $piece) {
115-
$pieceImage = $this->getPieceImage($piece, $config);
110+
$pieceImage = $this->getPieceImage($piece);
116111

117112
if ($pieceImage->getHeight() > $maxHeight) {
118113
$maxHeight = $pieceImage->getHeight();
@@ -129,9 +124,9 @@ public function getMaxPieceHeight(Fen $fen, Config $config)
129124
*
130125
* @return Image
131126
*/
132-
protected function fetchRemotePieceImageFromTheme(Piece $piece, Config $config)
127+
protected function fetchRemotePieceImageFromTheme(Piece $piece)
133128
{
134-
$themeUrls = $config->getThemeUrls();
129+
$themeUrls = $this->config->getThemeUrls();
135130
$pieceShortName = $piece->getShortName();
136131

137132
if (!isset($themeUrls[$pieceShortName])) {
@@ -144,7 +139,7 @@ protected function fetchRemotePieceImageFromTheme(Piece $piece, Config $config)
144139
try {
145140
$image = ImageManagerStatic::make($pieceCachedPath);
146141
} catch (NotReadableException $exception) {
147-
$this->downloadPieceImagesFromTheme($config);
142+
$this->downloadPieceImagesFromTheme();
148143
$image = ImageManagerStatic::make($pieceCachedPath);
149144
}
150145

@@ -154,9 +149,9 @@ protected function fetchRemotePieceImageFromTheme(Piece $piece, Config $config)
154149
/**
155150
* Downloads all piece images from theme URLs.
156151
*/
157-
private function downloadPieceImagesFromTheme(Config $config)
152+
private function downloadPieceImagesFromTheme()
158153
{
159-
$themeUrls = $config->getThemeUrls();
154+
$themeUrls = $this->config->getThemeUrls();
160155
$pieces = Piece::generateAllPieces();
161156

162157
$handles = [];

tests/DiagramGenerator/Tests/Image/ImageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function tearDown(): void
4646
public function testDrawBoardWithFiguresDetectsBoardTextureFromThemeUrls()
4747
{
4848
$config = $this->createConfigWithThemeUrls();
49-
$storage = new StorageNew($this->cacheDirectory, $this->pieceThemeUrl, $this->boardTextureUrl);
49+
$storage = new StorageNew($this->cacheDirectory, $config);
5050
$image = new Image($storage, $config);
5151

5252
// Verify that hasThemeUrls works correctly

tests/DiagramGenerator/Tests/Image/StorageTest.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ protected function tearDown(): void
4646
public function testStorageNewGetPieceImageWithThemeUrls()
4747
{
4848
$config = $this->createConfigWithThemeUrls();
49-
$storage = $this->createStorageNew();
49+
$storage = $this->createStorageNew($config);
5050
$piece = new Pawn('white');
5151
$piece->setRow(1)->setColumn(0);
5252

5353
// Storage should use theme URLs
5454
// It will fail when trying to download/load the image, but that's expected
5555
$this->expectException(\Intervention\Image\Exception\NotReadableException::class);
5656

57-
$storage->getPieceImage($piece, $config);
57+
$storage->getPieceImage($piece);
5858
}
5959

6060
public function testStorageNewGetPieceImageThrowsExceptionWhenPieceUrlMissing()
6161
{
6262
$config = $this->createConfigWithPartialThemeUrls();
63-
$storage = $this->createStorageNew();
63+
$storage = $this->createStorageNew($config);
6464

6565
// Create a piece that doesn't have a URL in themeUrls
6666
$pieceWithoutUrl = new \DiagramGenerator\Fen\King('white');
@@ -70,7 +70,7 @@ public function testStorageNewGetPieceImageThrowsExceptionWhenPieceUrlMissing()
7070
$this->expectException(\RuntimeException::class);
7171
$this->expectExceptionMessage('Piece URL not found in theme for piece: wk');
7272

73-
$storage->getPieceImage($pieceWithoutUrl, $config);
73+
$storage->getPieceImage($pieceWithoutUrl);
7474
}
7575

7676
public function testStorageGetPieceImage()
@@ -96,24 +96,24 @@ public function testStorageGetPieceImage()
9696
public function testStorageNewGetBackgroundTextureImageWithThemeUrls()
9797
{
9898
$config = $this->createConfigWithThemeUrls();
99-
$storage = $this->createStorageNew();
99+
$storage = $this->createStorageNew($config);
100100

101101
// Should try to load from theme URLs
102102
// It will fail when trying to download/load the image, but that's expected
103103
$this->expectException(\Intervention\Image\Exception\NotReadableException::class);
104104

105-
$storage->getBackgroundTextureImage($config);
105+
$storage->getBackgroundTextureImage();
106106
}
107107

108108
public function testStorageNewGetBackgroundTextureImageReturnsNullWhenBoardUrlMissing()
109109
{
110110
$config = $this->createConfigWithPartialThemeUrls();
111111
// Remove board URL
112112
$config->setThemeUrls(['wp' => 'https://example.com/wp.png']);
113-
$storage = $this->createStorageNew();
113+
$storage = $this->createStorageNew($config);
114114

115115
// Should return null when board URL is not in themeUrls
116-
$result = $storage->getBackgroundTextureImage($config);
116+
$result = $storage->getBackgroundTextureImage();
117117
$this->assertNull($result);
118118
}
119119

@@ -129,7 +129,8 @@ public function testStorageGetBackgroundTextureImageReturnsNullWhenNoTexture()
129129

130130
public function testGetCachedPieceFilePathFromTheme()
131131
{
132-
$storage = $this->createStorageNew();
132+
$config = $this->createConfigWithThemeUrls();
133+
$storage = $this->createStorageNew($config);
133134
$pieceUrl = 'https://example.com/pieces/wp.png';
134135
$piece = 'wp';
135136

@@ -152,7 +153,8 @@ public function testGetCachedPieceFilePathFromTheme()
152153

153154
public function testGetCachedPieceFilePathFromThemeWithCustomExtension()
154155
{
155-
$storage = $this->createStorageNew();
156+
$config = $this->createConfigWithThemeUrls();
157+
$storage = $this->createStorageNew($config);
156158
$pieceUrl = 'https://example.com/pieces/wp.jpg';
157159
$piece = 'wp';
158160

@@ -175,7 +177,8 @@ public function testGetCachedPieceFilePathFromThemeWithCustomExtension()
175177

176178
public function testGetCachedTextureFilePathFromTheme()
177179
{
178-
$storage = $this->createStorageNew();
180+
$config = $this->createConfigWithThemeUrls();
181+
$storage = $this->createStorageNew($config);
179182
$boardUrl = 'https://example.com/boards/board.png';
180183

181184
$reflection = new \ReflectionClass($storage);
@@ -196,7 +199,8 @@ public function testGetCachedTextureFilePathFromTheme()
196199

197200
public function testGetCachedTextureFilePathFromThemeWithCustomExtension()
198201
{
199-
$storage = $this->createStorageNew();
202+
$config = $this->createConfigWithThemeUrls();
203+
$storage = $this->createStorageNew($config);
200204
$boardUrl = 'https://example.com/boards/board.jpg';
201205

202206
$reflection = new \ReflectionClass($storage);
@@ -220,9 +224,9 @@ protected function createStorage()
220224
return new Storage($this->cacheDirectory, $this->pieceThemeUrl, $this->boardTextureUrl);
221225
}
222226

223-
protected function createStorageNew()
227+
protected function createStorageNew(Config $config)
224228
{
225-
return new StorageNew($this->cacheDirectory, $this->pieceThemeUrl, $this->boardTextureUrl);
229+
return new StorageNew($this->cacheDirectory, $config);
226230
}
227231

228232
protected function createConfigWithThemeUrls()

0 commit comments

Comments
 (0)