-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.php
186 lines (152 loc) · 4.86 KB
/
container.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* <container.php>
* Topics: Use of Lambda functions, Closures and Shared instance for
* simplifying code in a Container class
*
* @author Muhammad Anwar Hussain<[email protected]>
* Created on: 7th June 2019
*/
trait Accessibility
{
protected function grantAccess($userid, $facility)
{
echo "@ $userid - [ access to $facility ]: Granted". '<br>';
}
protected function preventAccess($userid, $facility)
{
echo "@ $userid - [ access to $facility ]: Denied". '<br>';
}
protected function invalidUser($userid, $facility)
{
echo "@ $userid - [ access to $facility ] : Invalid User!". '<br>';
}
}
class User
{
use Accessibility;
public function accessToFacility(Administration $admin, $facility, $user_id, $user_pass)
{
[$user_testify, $user_status] = $admin->validateUserData($user_id, $user_pass);
if ($user_testify)
{
if ($admin->checkFacility($user_status, $facility))
{
$this->grantAccess($user_id, $facility);
}
else
{
$this->preventAccess($user_id, $facility);
}
}
else
{
$this->invalidUser($user_id, $facility);
}
}
}
class Administration
{
private $data = [];
protected $facilities = array();
public function setUserInfo(array $user_data = [])
{
$this->data[] = $user_data;
}
public function setFacility(array $facility)
{
$this->facilities[] = $facility;
}
public function validateUserData($u_id, $u_pass)
{
$response = false;
foreach($this->data as $u_data)
{
if ($u_data['userid'] == $u_id && $u_data['password'] == $u_pass)
{
$response = true;
return [$response, $u_data['status']];
}
}
return $response;
}
public function checkFacility ($status, $facility)
{
foreach($this->facilities as $item)
{
if ($item['fkey'] == $facility)
{
if ($item['category'] == 'common' || $item['category'] == $status)
{
return $item['fname'];
}
return false;
}
}
}
}
class Container
{
protected $items = [];
function __set($key, $item)
{
$this->items[$key] = $item;
}
function __get($key)
{
if (!isset($this->items[$key]))
{
throw new Exception(sprintf('Item "%s" is not defined.', $key));
}
return is_callable($this->items[$key]) ? $this->items[$key]($this) : $this->items[$key];
}
public function getInstance($callable)
{
return function () use ($callable)
{
static $_instance;
if (is_null($_instance))
{
$_instance = $callable();
}
return $_instance;
};
}
}
$c = new Container();
// for Administration
$c->admin = $c->getInstance(function(){return new Administration;});
// Administration instance
$admin = $c->admin;
// set the facilities by the admin
$admin->setFacility(array('fkey' => 'coffee', 'fname' => 'accessToCoffee', 'category' => 'common'));
$admin->setFacility( array('fkey' => 'copy', 'fname' => 'accessToPrinter', 'category' => 'common'));
$admin->setFacility(array('fkey' => 'table-tennis', 'fname' => 'accessToTableTennis', 'category' => 'student'));
$admin->setFacility(array('fkey' => 'long-tennis', 'fname' => 'accessToLongTennis', 'category' => 'teacher'));
$admin->setFacility(array('fkey' => 'library', 'fname' => 'accessToLibrary', 'category' => 'common'));
$admin->setFacility( array('fkey' => 'restroom', 'fname' => 'accessToRestroom', 'category' => 'teacher'));
// set the user's info by the admin
$admin->setUserInfo(array('userid'=>'s_name1', 'password'=>'s_password1', 'status'=>'student'));
$admin->setUserInfo(array('userid'=>'s_name2', 'password'=>'s_password2', 'status'=>'student'));
$admin->setUserInfo(array('userid'=>'t_name1', 'password'=>'t_password1', 'status'=>'teacher'));
$admin->setUserInfo(array('userid'=>'t_name2', 'password'=>'t_password2', 'status'=>'teacher'));
// for Users
$c->user = $c->getInstance(function(){return new User;});
// User instance for students
$s_user = $c->user;
// let's say $admin is your access-card by which a facility is accessible
$s_user->accessToFacility($admin, 'copy', 's_name1', 's_password1');
$s_user->accessToFacility($admin, 'coffee', 's_name2', 's_password2');
$s_user->accessToFacility($admin, 'copy', 's_name2', 's_password2');
$s_user->accessToFacility($admin, 'long-tennis', 's_name2', 's_password2');
// access by an unauthorized student with userid 's_name'
$s_user->accessToFacility($admin, 'coffee', 's_name', 's_password');
// User instance for teachers, which is infact the same as the students
$t_user = $c->user;
$t_user->accessToFacility($admin, 'coffee', 't_name1', 't_password1');
$t_user->accessToFacility($admin, 'copy', 't_name2', 't_password2');
$t_user->accessToFacility($admin, 'table-tennis', 't_name2', 't_password2');
$t_user->accessToFacility($admin, 'library', 't_name1', 't_password1');
// access by an unauthorized teacher with userid 't_name'
$t_user->accessToFacility($admin, 'restroom', 't_name', 't_password');
?>