Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<?php
namespace Utility;



/**
 * Central registry for storing item during script execution.
 * @author andre.fourie
 *
 */
class Registry
{

	/**
	 * @var \Zend\Session\Container
	 */
	static protected $session;
	/**
	 * @var \Doctrine\ORM\EntityManager
	 */
	static protected $em;
	/**
	 * @var \Zend\ServiceManager\ServiceLocatorInterface
	 */
	static protected $sm;
	/**
	 * @var array
	 */
	static protected $container = array(
			'UseOnce' => array()
	);
	/**
	 * @var array
	 */
	static protected $config;


	/**
	 * Initiate session container.
	 */
	static protected function initSession()
	{
		is_null(self::$session)
			&& self::$session = new \Zend\Session\Container(__CLASS__);
	}

	/**
	 * Store an item.
	 * @param string $key
	 * @param unknown $value
	 * @return boolean
	 */
	static public function set($key, $value)
	{
		self::$container[$key] = $value;
		return true;
	}

	/**
	 * Retrieve an item.
	 * @param string $key
	 * @param unknown|null $default
	 * @return unknown|null
	 */
	static public function get($key, $default = null)
	{
		return isset(self::$container[$key])
			? self::$container[$key]
			: $default;
	}

	/**
	 * Set value for single retrieval.
	 * @param string $key
	 * @param unknown $value
	 */
	static public function setOnce($key, $value)
	{
		self::$container['UseOnce'][$key] = $value;
		return true;
	}

	/**
	 * Retrieve single use value. Value will be destroyed after this call.
	 * @param string $key
	 * @param unknown|null $default
	 * @return unknown|null
	 */
	static public function checkOnce($key, $default = null)
	{
		if (isset(self::$container['UseOnce'][$key]))
		{
			$value = self::$container['UseOnce'][$key];
			unset(self::$container['UseOnce'][$key]);
			return $value;
		}
		else
		{
			return $default;
		}
	}

	/**
	 * Set Doctrine Entity Manager for easy access.
	 * @param \Doctrine\ORM\EntityManager $em
	 * @return boolean
	 */
	static public function setEntityManager($em)
	{
		self::$em = $em;
		$config = new \Doctrine\ORM\Configuration();
		$config->setQueryCacheImpl(new \Doctrine\Common\Cache\FilesystemCache('data/DoctrineORMModule/QueryCache'));
		$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\FilesystemCache('data/DoctrineORMModule/MetaCache'));
		$config->setAutoGenerateProxyClasses(false);
		return true;
	}

	/**
	 * Retrieve Doctrine Entity Manager.
	 * @return \Doctrine\ORM\EntityManager
	 */
	static public function getEntityManager()
	{
		return self::$em;
	}

	/**
	 * Set Doctrine Entity Manager for easy access.
	 * @param \Zend\ServiceManager\ServiceLocatorInterface $sm
	 * @return boolean
	 */
	static public function setServiceManager($sm)
	{
		self::$sm = $sm;
		self::$em = self::$sm->get('doctrine.entitymanager.orm_default');
		$config = new \Doctrine\ORM\Configuration();
		$config->setQueryCacheImpl(new \Doctrine\Common\Cache\FilesystemCache('data/DoctrineORMModule/QueryCache'));
		$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\FilesystemCache('data/DoctrineORMModule/MetaCache'));
		$config->setAutoGenerateProxyClasses(false);
		//$config->ensureProductionSettings();
		return true;
	}

	/**
	 * Retrieve Doctrine Entity Manager.
	 * @return \Zend\ServiceManager\ServiceLocatorInterface
	 */
	static public function getServiceManager()
	{
		return self::$sm;
	}

	/**
	 * Set device session from token.
	 * @param string $token
	 * @throws \Exception
	 * @return boolean
	 */
	static public function setSession($token)
	{
		self::$session = null;
		$sm = new \Zend\Session\SessionManager();
		$sm->setId($token);
		self::initSession();
		if (!isset(self::$session->authData)
			|| self::$session->authData['authToken'] != $token)
		{
			throw new \Exception('Invalid session requested.');
		}
		return true;
	}

	/**
	 * Set authentication data in session for global accessibility.
	 * @param array $authData
	 * @return boolean
	 */
	static public function clearAuthData()
	{
		self::initSession();
		if (isset(self::$session->authData))
		{
			unset(self::$session->authData);
		}
		return true;
	}

	/**
	 * Clear grid data stored in session.
	 * @param array $authData
	 * @return boolean
	 */
	static public function clearSessionData()
	{
		self::initSession();
		if (isset(self::$session->sessData))
		{
			unset(self::$session->sessData);
		}
		return true;
	}

	/**
	 * Should session data be cleared?
	 * @param string $containerName
	 * @return boolean
	 */
	static public function initSessionStorage($containerName)
	{
		self::initSession();
		isset(self::$session->sessData)
			|| self::$session->sessData = array();
		if (!isset(self::$session->sessData[$containerName]))
		{
			self::$session->sessData[$containerName] = true;
			return true;
		}
		return false;
	}

	/**
	 * Set authentication data in session for global accessibility.
	 * @param array $authData
	 * @return boolean
	 */
	static public function setAuthData(array $authData)
	{
		self::initSession();
		$sm = new \Zend\Session\SessionManager();
		isset($authData['Sudo'])
			|| $authData['Sudo'] = array();
		$authData['authToken'] = $sm->getId();
		self::$session->authData = $authData;
		return true;
	}

	/**
	 * Set a data parameter in the authentication packet.
	 * @param string $param
	 * @param unknown $value
	 * @return boolean
	 */
	static public function setAuthParam($param, $value)
	{
		self::initSession();
		if (isset(self::$session->authData))
		{
			self::$session->authData[$param] = $value;
		}
		return isset(self::$session->authData);
	}

	/**
	 * Set a data parameter in the authentication packet.
	 * @param string $param
	 * @param unknown $value
	 * @return boolean
	 */
	static public function setAuthSudo($param, $label, $value)
	{
		self::initSession();
		if (isset(self::$session->authData))
		{
			self::$session->authData['Sudo'][$param] = $value;
			self::$session->authData['Sudo'][$param . 'Id'] = $value;
			self::$session->authData['Sudo'][$param . 'Name'] = $label;
			self::$session->authData['Sudo']['Names'][$param] = $label;
		}
		return isset(self::$session->authData);
	}

	/**
	 * Retrieve sudo filter.
	 * @param string $param
	 * @param unknown $default
	 * @return unknown
	 */
	static public function getSudo($param, $default)
	{
		self::initSession();
		return isset(self::$session->authData)
				&& isset(self::$session->authData['Sudo'][$param])
			? self::$session->authData['Sudo'][$param]
			: $default;
	}

	/**
	 * Retrieve authentication data.
	 * @return array|null
	 */
	static public function getAuthData()
	{
		self::initSession();
		return isset(self::$session->authData)
			? self::$session->authData
			: null;
	}

	/**
	 * Check if we have authentication data.
	 * @return boolean
	 */
	static public function isAuthenticated()
	{
		self::initSession();
		return isset(self::$session->authData);
	}

	/**
	 * Get an authentication parameter.
	 * @param string $key
	 * @return unknown|null
	 */
	static public function getAuthParam($key)
	{
		self::initSession();
		return isset(self::$session->authData) && isset(self::$session->authData[$key])
			? self::$session->authData[$key]
			: null;
	}

	/**
	 * Get authenticated user's user type.
	 * @return string|null
	 */
	static public function getUserType()
	{
		self::initSession();
		return isset(self::$session->authData) && isset(self::$session->authData['permissions']['name'])
			? self::$session->authData['permissions']['name']
			: null;
	}

	/**
	 * Resolve Company from session and sudo settings.
	 * @param integer|null $companyId
	 * @throws \Exception
	 * @return integer|object|null
	 */
	static public function resolveCompanyContext($companyId = null)
	{
		if (is_null(self::$em))
		{
			return $companyId;
		}
		self::initSession();
		if (!isset(self::$session->authData)
			|| (!is_null($companyId)
					&& 'Administrator' == self::$session->authData['userType']))
		{
			return !is_null($companyId)
				? self::$em->getReference(
						'\Company\Entity\Company',
						$companyId
						)
				: null;
		}
		if (is_null($companyId))
		{
			$companyFilter = \Utility\Registry::getSudo('Company', false);
			if (!$companyFilter)
			{
				return self::$em->getReference(
						'\Company\Entity\Company',
						self::$session->authData['company']['id']
				);
			}
			else
			{
				return self::$em->getReference(
						'\Company\Entity\Company',
						$companyFilter
				);
			}
		}
		else
		{
			$companyFilter = \Utility\Registry::getSudo('Company', false);
			if ('Administrator' != self::$session->authData['userType'] || !$companyFilter)
			{
				throw new \Exception('No dealership selected for actioning.');
			}
			return self::$em->getReference(
					'\Company\Entity\Company',
					$companyFilter
			);
		}
	}

	/**
	 * Resolve Profile from session and sudo settings.
	 * @param integer|null $profileId
	 * @throws \Exception
	 * @return \User\Entity\Profile|null
	 */
	static public function resolveProfileContext($profileId = null)
	{
		if (is_null(self::$em))
		{
			return $profileId;
		}
		self::initSession();
		if (!isset(self::$session->authData))
		{
			return !is_null($profileId)
				? self::$em->getReference(
						'\User\Entity\Profile',
						$profileId
						)
				: null;
		}
		else
		{
			$profileFilter = \Utility\Registry::getSudo('Profile', false);
			return ($profileFilter)
				? self::$em->getReference(
						'\User\Entity\Profile',
						$profileFilter
						)
				: self::$em->getReference(
						'\User\Entity\Profile',
						self::$session->authData['id']
						);
		}
	}

	/**
	 * Retrieve global config.
	 * @return array
	 */
	static public function getGlobalConfig()
	{
		is_null(self::$config)
			&& self::$config = array_merge(
				include __DIR__ . '/../../../../config/autoload/global.php',
				include __DIR__ . '/../../../../config/autoload/local.php'
				);
		return self::$config;
	}

	/**
	 * Add changable config data to global config.
	 * @param array $config
	 * @return boolean
	 */
	static public function addDynamicConfig(array $config)
	{
		self::getGlobalConfig();
		self::$config = array_merge(self::$config, $config);
		return true;
	}

	/**
	 * Retrieve section from global config.
	 * @param string $key
	 * @return unknown|null
	 */
	static public function getConfigParam($key)
	{
		self::getGlobalConfig();
		return isset(self::$config[$key])
			? self::$config[$key]
			: null;
	}

	/**
	 * Store data to file.
	 * @param  string  $key  Storage key
	 * @param  unknown $data Data to store
	 * @return void
	 */
	static public function storeJson($key, $data)
	{
		file_put_contents(getcwd() . '/data/file/' . $key . '.json', json_encode($data));
	}

	/**
	 * Retrieve data from file.
	 * @param  string  $key Storage key
	 * @return unknown      Stored data or null
	 */
	static public function fetchJson($key)
	{
		if (!file_exists(getcwd() . '/data/file/' . $key . '.json'))
		{
			return null;
		}
		$result = file_get_contents(getcwd() . '/data/file/' . $key . '.json');
		return json_decode($result, true);
	}

	/**
	 * Delete data file.
	 * @param  string  $key Storage key
	 * @return void
	 */
	static public function deleteJson($key)
	{
		if (file_exists(getcwd() . '/data/file/' . $key . '.json'))
		{
			unlink(getcwd() . '/data/file/' . $key . '.json');
		}
	}

	/**
	 * Cleanup files older than [hours].
	 * @param  integer $hours Number of hours
	 * @return void
	 */
	static public function cleanupJson($hours)
	{
		$files = glob(getcwd() . '/data/file/*');
		$now   = time();
		foreach ($files as $file)
		{
			if (is_file($file))
			{
				if ($now - filemtime($file) >= 60 * 60 * (int) $hours)
				{
					unlink($file);
				}
			}
		}
	}

}

Commits for namibiamodule/Utility/src/Utility/Registry.php

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit