Skip to content

Commit 809908b

Browse files
committed
add proxy design pattern and test
1 parent e5c49b6 commit 809908b

File tree

9 files changed

+131
-0
lines changed

9 files changed

+131
-0
lines changed

app/Commands/CreateTestFileCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function __construct(string $className)
2020
*/
2121
public function handle()
2222
{
23+
$this->_className .= 'Test';
2324
$stub = file_get_contents(__DIR__ . '/../../stubs/Test.stub');
2425
$content = str_replace('class Test', 'class '. $this->_className, $stub);
2526
file_put_contents(__DIR__ . '/../../tests/' . $this->_className . '.php', $content);

app/Controller/StructuralController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,9 @@ public function decorator()
8787
(new \App\Factory\Structural\DecoratorPattern\ActiveRecord\Application)->run();
8888
}
8989

90+
public function proxy()
91+
{
92+
(new \App\Factory\Structural\ProxyPattern\FileReader\Application)->run();
93+
}
94+
9095
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php namespace App\Factory\Structural\ProxyPattern\FileReader;
2+
3+
class Application
4+
{
5+
public function run()
6+
{
7+
$file = (new JsonFileReaderProxy(__DIR__ . '/data.json'));
8+
echo 'IsFileReadAble: ' . $file->isReadable();
9+
echo '<br/>File Size is: ' . $file->getFileSize() . ' KB';
10+
echo '<br/>Is File Writable: ' . $file->isWritable();
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php namespace App\Factory\Structural\ProxyPattern\FileReader;
2+
3+
interface FileReader
4+
{
5+
public function getFileSize(): int;
6+
public function isWritable(): bool;
7+
public function isReadable(): bool;
8+
public function getContent();
9+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php namespace App\Factory\Structural\ProxyPattern\FileReader;
2+
3+
class JsonFileReader implements FileReader
4+
{
5+
private $_file = null;
6+
private $_fileSize = null;
7+
private $_isWritable = null;
8+
private $_isReadable = null;
9+
10+
public function __construct(string $path)
11+
{
12+
$this->_file = \json_decode(\file_get_contents($path));
13+
$this->_fileSize = filesize($path);
14+
$this->_isWritable = is_writable($path);
15+
$this->_isReadable = is_readable($path);
16+
}
17+
18+
public function getContent()
19+
{
20+
return $this->_file;
21+
}
22+
23+
public function getFileSize(): int
24+
{
25+
return $this->_fileSize;
26+
}
27+
28+
public function isWritable(): bool
29+
{
30+
return $this->_isWritable;
31+
}
32+
33+
public function isReadable(): bool
34+
{
35+
return $this->_isReadable;
36+
}
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php namespace App\Factory\Structural\ProxyPattern\FileReader;
2+
3+
class JsonFileReaderProxy implements FileReader
4+
{
5+
private $_file = null;
6+
private $_path = null;
7+
8+
public function __construct(string $path)
9+
{
10+
$this->_file = null;
11+
$this->_path = $path;
12+
}
13+
14+
public function getContent()
15+
{
16+
$this->loadFile();
17+
return $this->_file->getContent();
18+
}
19+
20+
public function getFileSize(): int
21+
{
22+
$this->loadFile();
23+
return $this->_file->getFileSize();
24+
}
25+
26+
public function isWritable(): bool
27+
{
28+
$this->loadFile();
29+
return $this->_file->isWritable();
30+
}
31+
32+
public function isReadable(): bool
33+
{
34+
$this->loadFile();
35+
return $this->_file->isReadable();
36+
}
37+
38+
private function loadFile()
39+
{
40+
if($this->_file === null) {
41+
$this->_file = new JsonFileReader($this->_path);
42+
}
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

bootstraps/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
['GET', '/structural/bridge', ['App\Controller\StructuralController', 'bridge']],
1818
['GET', '/structural/composite', ['App\Controller\StructuralController', 'composite']],
1919
['GET', '/structural/decorator', ['App\Controller\StructuralController', 'decorator']],
20+
['GET', '/structural/proxy', ['App\Controller\StructuralController', 'proxy']],
2021
['GET', '/behavioral', ['App\Controller\BehavioralController', 'chainOfResponsibility']],
2122
['GET', '/behavioral/command', ['App\Controller\BehavioralController', 'command']],
2223
// ['GET', '/{slug}', ['App\Controller\CreationalController', 'show']],

tests/ProxyPatternTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class ProxyPatternTest extends TestCase {
6+
private $filePath = __DIR__ . '/../app/Factory/Structural/ProxyPattern/FileReader/data.json';
7+
8+
public function testCanReadReadableFile()
9+
{
10+
$fileReader = new App\Factory\Structural\ProxyPattern\FileReader\JsonFileReaderProxy($this->filePath);
11+
12+
$this->assertTrue($fileReader->isReadable());
13+
}
14+
15+
public function testCanReadFileContent()
16+
{
17+
$fileReader = new App\Factory\Structural\ProxyPattern\FileReader\JsonFileReaderProxy($this->filePath);
18+
19+
$this->assertEquals($fileReader->getContent(), new \stdClass);
20+
}
21+
}

0 commit comments

Comments
 (0)