-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProxy.php
76 lines (64 loc) · 1.85 KB
/
Proxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace DesignPatterns\Structural;
/**
* Declares common operations for both Subject and the Proxy
*/
interface WeatherClient
{
public function getWeather(string $location): string;
}
/**
* Third party weather API client (which we probably can't modify)
* contains some core business logic
*/
class Weather implements WeatherClient
{
protected $token;
public function __construct(string $token)
{
$this->token = $token;
}
public function getWeather(string $location): string
{
$json = file_get_contents('http://api.openweathermap.org/data/2.5/weather?q=' . $location
. '&APPID=' . $this->token);
$data = json_decode($json, true);
return 'weather: ' . $data['weather'][0]['description'] ?? 'no data' . PHP_EOL;
}
}
/**
* Proxy has an interface identical to the Subject and allows us caching the results
*/
class WeatherProxy implements WeatherClient
{
/** @var WeatherClient Real third party client */
protected $client;
private $cache = [];
public function __construct(WeatherClient $client)
{
$this->client = $client;
}
public function getWeather(string $location): string
{
if (!isset($this->cache[$location])) {
echo "- cache: MISS\n";
$this->cache[$location] = $this->client->getWeather($location);
} else {
echo "+ cache: HIT, retrieving result from cache..\n";
}
return $this->cache[$location];
}
}
# Client code example
$weather = new Weather('177b4a1be7dfd10e0d30e8fdeabe0ea9');
$proxy = new WeatherProxy($weather);
echo $proxy->getWeather('Kiev');
echo $proxy->getWeather('Lviv');
echo $proxy->getWeather('Kiev');
/* Output example:
- cache: MISS
weather: clear sky
- cache: MISS
weather: scattered clouds
+ cache: HIT, retrieving result from cache..
weather: clear sky */