-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstructural-proxy-3.php
112 lines (96 loc) · 3.19 KB
/
structural-proxy-3.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* This example just to simulate how Proxy can be work
* 2nees.com
*/
/**
* Interface YouTubeLib - The interface for original service and proxy
*/
interface YouTubeLib {
public function getVideoInfo(): string;
public function downloadVideo(): string;
public function setSelectedVideoId(string $vid): void;
public function getSelectedVideoId(): string;
}
/**
* Class YouTubeClass - Original Server which its a concrete implementation
*/
class YouTubeClass implements YouTubeLib {
private string $selectedVideoId;
public function getVideoInfo(): string
{
$seen = rand(500, 2000);
return "Video {$this->selectedVideoId} seen: {$seen}" . PHP_EOL;
}
public function downloadVideo(): string
{
$size = rand(1, 500);
return "Video {$this->selectedVideoId} size: {$size}MB" . PHP_EOL;
}
public function setSelectedVideoId(string $vid): void{
$this->selectedVideoId = $vid;
}
public function getSelectedVideoId(): string{
return $this->selectedVideoId;
}
}
/**
* Class YouTubeProxy - This is the Proxy class which implements the same interface for Original Class
*/
class YouTubeProxy implements YouTubeLib {
private YouTubeLib $youTubeLib;
private array $cachedInfos = [];
private array $cachedDownloadedFiles = [];
public function __construct()
{
$this->youTubeLib = new YouTubeClass();
}
public function getVideoInfo(): string
{
$key = $this->getSelectedVideoId();
if(!array_key_exists($key, $this->cachedInfos)) {
$this->cachedInfos[$key] = $this->youTubeLib->getVideoInfo();
}else {
echo "Get Info from Cache!" . PHP_EOL;
}
return $this->cachedInfos[$key];
}
public function downloadVideo(): string
{
$key = $this->getSelectedVideoId();
if(!array_key_exists($key, $this->cachedDownloadedFiles)) {
$this->cachedDownloadedFiles[$key] = $this->youTubeLib->downloadVideo();
}else {
echo "Downloaded Video from Cache!" . PHP_EOL;
}
return $this->cachedDownloadedFiles[$key];
}
public function setSelectedVideoId(string $vid): void
{
$this->youTubeLib->setSelectedVideoId($vid);
}
public function getSelectedVideoId(): string
{
return $this->youTubeLib->getSelectedVideoId();
}
}
$youtube = new YouTubeProxy();
$youtube->setSelectedVideoId("2nees-A");
echo $youtube->getVideoInfo();
echo $youtube->downloadVideo();
echo "==========================1========================" . PHP_EOL;
$youtube->setSelectedVideoId("2nees-B");
echo $youtube->getVideoInfo();
echo $youtube->downloadVideo();
echo "==========================2========================" . PHP_EOL;
$youtube->setSelectedVideoId("2nees-A");
echo $youtube->getVideoInfo();
echo $youtube->downloadVideo();
echo "==========================3========================" . PHP_EOL;
$youtube->setSelectedVideoId("2nees-C");
echo $youtube->getVideoInfo();
echo $youtube->downloadVideo();
echo "==========================4========================" . PHP_EOL;
$youtube->setSelectedVideoId("2nees-B");
echo $youtube->getVideoInfo();
echo $youtube->downloadVideo();