-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.php
199 lines (184 loc) · 4.77 KB
/
State.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
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace dee\tools;
use Yii;
use yii\base\Object;
use yii\di\Instance;
use yii\web\Cookie;
use yii\base\InvalidCallException;
use yii\db\Connection;
use yii\db\Query;
use yii\web\Application as WebApplication;
/**
* Description of State
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class State extends Object
{
/**
* @var Connection
*/
public $db = 'db';
/**
* @var string table name
*/
public $tableName = '{{%dee_client}}';
/**
* @var string table name
*/
public $cliFileKey = '@runtime/dee_client_id.data';
/**
* @var string
*/
public $cookieKey = 'dee_state_id';
/**
* @var array
*/
protected $_states;
/**
* @var integer
*/
private $_id;
/**
* @inheritdoc
*/
public function init()
{
$this->db = Instance::ensure($this->db, Connection::className());
$this->createTable();
if (Yii::$app instanceof WebApplication) {
$id = Yii::$app->getRequest()->getCookies()->getValue($this->cookieKey);
} else {
$file = Yii::getAlias($this->cliFileKey);
$id = is_file($file) ? (int) file_get_contents($file) : null;
}
if ($id === null || !is_numeric($id) || ($this->_states = $this->getData($id)) === false) {
$primary = $this->db->getSchema()->insert($this->tableName, [
'created_at' => time(),
]);
$id = $primary['id'];
$this->_states = [];
}
if (Yii::$app instanceof WebApplication) {
$cookie = new Cookie([
'name' => $this->cookieKey,
'value' => $id,
'expire' => time() + 30 * 24 * 3600,
]);
Yii::$app->getResponse()->getCookies()->add($cookie);
} elseif (isset($file)) {
file_put_contents($file, $id, LOCK_EX);
}
$this->_states['id'] = $this->_id = $id;
}
/**
* Create table if not exists
*/
protected function createTable()
{
if ($this->db->getTableSchema($this->tableName) === null) {
$this->db->createCommand()
->createTable($this->tableName, [
'id' => 'pk',
'created_at' => 'integer',
'updated_at' => 'integer',
'data' => 'binary',
])->execute();
$this->db->getTableSchema($this->tableName, true);
}
}
/**
* Get saved profile of the client
* @param integer $id
* @return boolean|array
*/
protected function getData($id)
{
$data = (new Query())->select(['data'])
->from($this->tableName)
->where(['id' => $id])->scalar();
if ($data !== false) {
return empty($data) ? [] : unserialize($data);
}
return false;
}
/**
* Save client's profile to database
*/
protected function save()
{
$this->db->createCommand()->update($this->tableName, [
'updated_at' => time(),
'data' => [serialize($this->_states), \PDO::PARAM_LOB],
], ['id' => $this->_id])->execute();
}
/**
* Get profile
* @param string $name
* @param mixed $default
* @return mixed
*/
public function get($name, $default = null)
{
return array_key_exists($name, $this->_states) ? $this->_states[$name] : $default;
}
/**
* Getter of profile
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->get($name);
}
/**
* Set the profile
* @param string $name
* @param mixed $value
* @throws InvalidCallException
*/
public function set($name, $value)
{
if ($name == 'id') {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
}
$this->_states[$name] = $value;
$this->save();
}
/**
* Setter of profile
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->set($name, $value);
}
/**
* Check if isset
* @param string $name
* @return boolean
*/
public function __isset($name)
{
return isset($this->_states[$name]);
}
/**
* Get or set profiles
* @param array $states
* @return array
*/
public function states($states = null)
{
if ($states === null) {
return $this->_states;
} else {
unset($states['id']);
foreach ($states as $key => $value) {
$this->_states[$key] = $value;
}
$this->save();
}
}
}