Skip to content

Commit d3b3785

Browse files
committed
Added Proxy Design Pattern
1 parent 07d66ee commit d3b3785

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

Structural/Proxy/Api.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Structural\Proxy;
4+
5+
/**
6+
* Class Api
7+
* @package PhpDesignPatterns\Structural\Proxy
8+
*/
9+
class Api
10+
{
11+
/**
12+
* Just a fake api call method.
13+
*
14+
* @param string $url
15+
* @param array $data
16+
* @param string $method
17+
* @return array
18+
*/
19+
public function doApiCall($url, array $data, $method)
20+
{
21+
// Implement dummy api call.
22+
return array(
23+
'status' => 200,
24+
'message' => 'Api call performed successfully.',
25+
'data' => array(
26+
'id' => 1,
27+
'name' => 'foo',
28+
),
29+
);
30+
}
31+
}

Structural/Proxy/ApiProxy.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Structural\Proxy;
4+
5+
/**
6+
* Class ApiProxy
7+
* @package PhpDesignPatterns\Structural\Proxy
8+
*/
9+
class ApiProxy extends Api
10+
{
11+
/**
12+
* Instance of Api.
13+
*
14+
* @var Api
15+
*/
16+
private $wrapper;
17+
18+
/**
19+
* Do api call using proxied object.
20+
*
21+
* @param string $url
22+
* @param array $data
23+
* @param string $method
24+
* @return array
25+
*/
26+
public function doApiCall($url, array $data, $method)
27+
{
28+
$this->init();
29+
return $this->wrapper->doApiCall($url, $data, $method);
30+
}
31+
32+
/**
33+
* Initialize the proxied object.
34+
*/
35+
private function init()
36+
{
37+
if(!$this->wrapper) {
38+
$this->wrapper = new Api;
39+
}
40+
}
41+
}

Structural/Proxy/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Proxy Design Pattern
2+
3+
## Intent
4+
5+
“Provide a surrogate or placeholder for another object to control access to it.”
6+
7+
Excerpt From: Erich Gamma. “Design Patterns: Elements of Reusable Object-Oriented Software.”
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Tests\Structural\Proxy;
4+
5+
use PhpDesignPatterns\Structural\Proxy\ApiProxy;
6+
7+
/**
8+
* Class ApiProxyTest
9+
* @package PhpDesignPatterns\Tests\Structural\Proxy
10+
*/
11+
class ApiProxyTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* Test doApiCall.
15+
*/
16+
public function testDoApiCall()
17+
{
18+
$expects = array(
19+
'status' => 200,
20+
'message' => 'Api call performed successfully.',
21+
'data' => array(
22+
'id' => 1,
23+
'name' => 'foo',
24+
),
25+
);
26+
$api = new ApiProxy;
27+
$this->assertEquals($expects, $api->doApiCall('localhost', array(), 'GET'));
28+
}
29+
}

Tests/Structural/Proxy/ApiTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Tests\Structural\Proxy;
4+
5+
use PhpDesignPatterns\Structural\Proxy\Api;
6+
7+
/**
8+
* Class ApiTest
9+
* @package PhpDesignPatterns\Tests\Structural\Proxy
10+
*/
11+
class ApiTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* Testing doApiCall.
15+
*/
16+
public function testDoApiCall()
17+
{
18+
$expects = array(
19+
'status' => 200,
20+
'message' => 'Api call performed successfully.',
21+
'data' => array(
22+
'id' => 1,
23+
'name' => 'foo',
24+
),
25+
);
26+
$api = new Api();
27+
$this->assertEquals($expects, $api->doApiCall('localhost', array(), 'GET'));
28+
}
29+
}

0 commit comments

Comments
 (0)