Skip to content

Commit 6ede915

Browse files
committed
change structure of the FileSystem interface
1 parent 3ec48b4 commit 6ede915

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

src/Behavioral/Command/OpenFileCommand.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ final class OpenFileCommand implements Command
1010
{
1111
private FileSystem $fileSystem;
1212

13-
public function __construct(
14-
FileSystem $fileSystem,
15-
private string $fileName
16-
) {
13+
public function __construct(FileSystem $fileSystem)
14+
{
1715
$this->fileSystem = $fileSystem;
1816
}
1917

2018
public function execute(): void
2119
{
22-
$this->fileSystem->openFile($this->fileName);
20+
$this->fileSystem->openFile();
2321
}
2422
}

src/Behavioral/Command/Receiver/FileSystem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
interface FileSystem
88
{
9-
public function openFile(string $fileName): void;
9+
public function openFile(): void;
1010
public function writeFile(string $content): void;
1111
public function closeFile(): void;
1212
}

src/Behavioral/Command/Receiver/UnixFileSystem.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@
66

77
final class UnixFileSystem implements FileSystem
88
{
9-
private string $fileName;
9+
private string $fileName = 'unix_file';
1010

11-
public function openFile(string $fileName): void
11+
public function openFile(): void
1212
{
13-
$this->fileName = $fileName;
14-
15-
printf('Unix file %s is open' . PHP_EOL, $fileName);
13+
printf('Unix file %s is open\n', $this->fileName);
1614
}
1715

1816
public function writeFile(string $content): void
1917
{
20-
printf('Unix file %s is writing' . PHP_EOL, $this->fileName);
21-
printf('Content: %s' . PHP_EOL, $content);
18+
printf('Unix file %s is writing\n', $this->fileName);
19+
printf('Content: %s\n', $content);
2220
}
2321

2422
public function closeFile(): void
2523
{
26-
printf('Unix file %s is closed' . PHP_EOL, $this->fileName);
24+
printf('Unix file %s is closed\n', $this->fileName);
2725
}
2826
}

src/Behavioral/Command/Receiver/WindowsFileSystem.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@
66

77
final class WindowsFileSystem implements FileSystem
88
{
9-
private string $fileName;
9+
private string $fileName = 'windows_file';
1010

11-
public function openFile(string $fileName): void
11+
public function openFile(): void
1212
{
13-
$this->fileName = $fileName;
14-
15-
printf('Windows file %s is open' . PHP_EOL, $fileName);
13+
printf('Windows file %s is open\n', $this->fileName);
1614
}
1715

1816
public function writeFile(string $content): void
1917
{
20-
printf('Windows file %s is writing' . PHP_EOL, $this->fileName);
18+
printf('Windows file %s is writing\n', $this->fileName);
2119
printf('Content: %s' . PHP_EOL, $content);
2220
}
2321

2422
public function closeFile(): void
2523
{
26-
printf('Windows file %s is closed' . PHP_EOL, $this->fileName);
24+
printf('Windows file %s is closed\n', $this->fileName);
2725
}
2826
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Growthdev\DesignPatterns\Tests\Behavioral\Command;
6+
7+
use Growthdev\DesignPatterns\Behavioral\Command\FileInvoker;
8+
use Growthdev\DesignPatterns\Behavioral\Command\OpenFileCommand;
9+
use Growthdev\DesignPatterns\Behavioral\Command\Receiver\FileSystem;
10+
use Growthdev\DesignPatterns\Behavioral\Command\Receiver\UnixFileSystem;
11+
use Growthdev\DesignPatterns\Behavioral\Command\Receiver\WindowsFileSystem;
12+
use PHPUnit\Framework\TestCase;
13+
14+
final class CommandTest extends TestCase
15+
{
16+
private static function getFileSystem(): FileSystem
17+
{
18+
$os = strtoupper(substr(PHP_OS, 0, 3)) === 'LIN';
19+
20+
return $os ? new UnixFileSystem() : new WindowsFileSystem();
21+
}
22+
23+
public function testCanInvokeFile(): void
24+
{
25+
$this->getFileSystem();
26+
27+
28+
}
29+
}

0 commit comments

Comments
 (0)