1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\Min; |
9: | |
10: | use Aurora\Modules\Min\Models\MinHash; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | class Manager extends \Aurora\System\Managers\AbstractManager |
18: | { |
19: | public function __construct(\Aurora\System\Module\AbstractModule $oModule = null) |
20: | { |
21: | parent::__construct($oModule); |
22: | } |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | public function createMin($sHashID, $aParams, $iUserId = null, $iExpireDate = null) |
31: | { |
32: | $mResult = false; |
33: | $sNewMin = ''; |
34: | |
35: | if (is_string($sHashID) && 0 < strlen($sHashID) && false !== $this->getMinByID($sHashID)) { |
36: | return false; |
37: | } |
38: | |
39: | while (true) { |
40: | $sNewMin = \Aurora\System\Utils::GenerateShortHashString(10); |
41: | if (false === $this->getMinByHash($sNewMin)) { |
42: | break; |
43: | } |
44: | } |
45: | |
46: | if (0 < strlen($sNewMin)) { |
47: | $aParams['__hash_id__'] = $sHashID; |
48: | $aParams['__hash__'] = $sNewMin; |
49: | $aParams['__time__'] = time(); |
50: | $aParams['__time_update__'] = time(); |
51: | |
52: | if (MinHash::create([ |
53: | 'Hash' => $sNewMin, |
54: | 'HashId' => md5($sHashID), |
55: | 'UserId' => $iUserId, |
56: | 'Data' => @\json_encode($aParams), |
57: | 'ExpireDate' => $iExpireDate |
58: | ])) { |
59: | $mResult = $sNewMin; |
60: | } |
61: | } |
62: | |
63: | return $mResult; |
64: | } |
65: | |
66: | |
67: | |
68: | |
69: | private function parseGetMinDbResult($oMin) |
70: | { |
71: | $mResult = false; |
72: | |
73: | if ($oMin && !empty($oMin->Data)) { |
74: | $aData = @\json_decode($oMin->Data, true); |
75: | if (is_array($aData) && 0 < count($aData)) { |
76: | $mResult = $aData; |
77: | } |
78: | if ($oMin->ExpireDate) { |
79: | $mResult['ExpireDate'] = $oMin->ExpireDate; |
80: | } |
81: | } |
82: | |
83: | return $mResult; |
84: | } |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | public function getMinByID($sHashID) |
92: | { |
93: | $oMin = MinHash::firstWhere('HashId', \md5($sHashID)); |
94: | return $this->parseGetMinDbResult($oMin); |
95: | } |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | public function getMinByHash($sHash) |
103: | { |
104: | $oMin = MinHash::firstWhere('Hash', $sHash); |
105: | return $this->parseGetMinDbResult($oMin); |
106: | } |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | public function getMinListByUserId($iUserId) |
114: | { |
115: | return MinHash::where('UserId', $iUserId)->get(); |
116: | } |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | public function deleteMinByID($sHashID) |
124: | { |
125: | return !!MinHash::where('HashId', \md5($sHashID))->delete(); |
126: | } |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | public function deleteMinByHash($sHash) |
134: | { |
135: | return !!MinHash::where('Hash', $sHash)->delete(); |
136: | } |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | public function updateMinByID($sHashID, $aParams, $sNewHashID = null) |
146: | { |
147: | $aPrevParams = $this->getMinByID($sHashID); |
148: | if (isset($aPrevParams['__hash__'])) { |
149: | $aParams['__hash__'] = $aPrevParams['__hash__']; |
150: | } |
151: | if (!empty($sNewHashID)) { |
152: | $aParams['__hash_id__'] = $sNewHashID; |
153: | } |
154: | if (isset($aPrevParams['__time__'])) { |
155: | $aParams['__time__'] = $aPrevParams['__time__']; |
156: | } |
157: | |
158: | $aParams['__time_update__'] = time(); |
159: | $aMergedParams = array_merge($aPrevParams, $aParams); |
160: | |
161: | $aUpdate = [ |
162: | 'Data' => @\json_encode($aMergedParams) |
163: | ]; |
164: | if (!empty($sNewHashID)) { |
165: | $aUpdate['HashId'] = \md5($sNewHashID); |
166: | } |
167: | |
168: | return MinHash::where('HashId', \md5($sHashID))->update($aUpdate); |
169: | } |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | public function updateMinByHash($sHash, $aParams, $sNewHashID = null) |
179: | { |
180: | $aPrevParams = $this->getMinByHash($sHash); |
181: | if (isset($aPrevParams['__hash_id__'])) { |
182: | $aParams['__hash_id__'] = $aPrevParams['__hash_id__']; |
183: | } |
184: | if (!empty($sNewHashID)) { |
185: | $aParams['__hash_id__'] = $sNewHashID; |
186: | } |
187: | if (isset($aPrevParams['__time__'])) { |
188: | $aParams['__time__'] = $aPrevParams['__time__']; |
189: | } |
190: | |
191: | $aParams['__time_update__'] = time(); |
192: | |
193: | $aUpdate = [ |
194: | 'Data' => @\json_encode($aParams) |
195: | ]; |
196: | if (!empty($sNewHashID)) { |
197: | $aUpdate['HashId'] = \md5($sNewHashID); |
198: | } |
199: | |
200: | return MinHash::where('Hash', $sHash)->update($aUpdate); |
201: | } |
202: | } |
203: | |