-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSdk.php
More file actions
116 lines (99 loc) · 3.57 KB
/
Copy pathSdk.php
File metadata and controls
116 lines (99 loc) · 3.57 KB
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
<?php
namespace SplitIO;
use SplitIO\Component\Initialization\CacheTrait;
use SplitIO\Component\Initialization\LoggerFactory;
use SplitIO\Component\Common\ServiceProvider;
use SplitIO\Exception\Exception;
use SplitIO\Sdk\Factory\LocalhostSplitFactory;
use SplitIO\Sdk\Factory\SplitFactory;
use SplitIO\Component\Common\Di;
use SplitIO\Engine\Splitter;
class Sdk
{
/**
* Sdk class should be used as statically
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* @param $apiKey
* @param array $options
* @return \SplitIO\Sdk\Factory\SplitFactoryInterface
*/
public static function factory($apiKey = 'localhost', array $options = array())
{
//Adding API Key into args array.
$options['apiKey'] = $apiKey;
if (self::instanceExists()) {
return null;
}
self::registerInstance();
if ($apiKey == 'localhost') {
//Register Logger
self::registerLogger((isset($options['log'])) ? $options['log'] : array());
return new LocalhostSplitFactory($options);
} else {
//Register Logger
self::registerLogger((isset($options['log'])) ? $options['log'] : array());
//Register Cache
self::registerCache((isset($options['cache'])) ? $options['cache'] : array());
if (isset($options['ipAddress'])) {
self::setIP($options['ipAddress']);
}
Di::set('splitter', new Splitter());
return new SplitFactory($apiKey, $options);
}
}
/**
* Register the logger class
*/
private static function registerLogger(array $options)
{
$logger = LoggerFactory::setupLogger($options);
ServiceProvider::registerLogger($logger);
}
private static function registerCache(array $options)
{
$_options = array();
$cacheAdapter = isset($options['adapter']) ? $options['adapter'] : 'redis';
if ($cacheAdapter == 'redis') {
throw new Exception("'redis' adapter is not longer supported. Please use 'predis' instead");
} elseif ($cacheAdapter == 'predis') {
$_options['predis-options'] = isset($options['options']) ? $options['options'] : null;
$_options['predis-parameters'] = isset($options['parameters']) ? $options['parameters'] : null;
$_options['predis-sentinels'] = isset($options['sentinels']) ? $options['sentinels'] : null;
$_options['predis-clusterNodes'] = isset($options['clusterNodes']) ? $options['clusterNodes'] : null;
$_options['predis-distributedStrategy'] = isset($options['distributedStrategy'])
? $options['distributedStrategy'] : null;
} else {
throw new Exception("A valid cache system is required. Given: $cacheAdapter");
}
CacheTrait::addCache($cacheAdapter, $_options);
}
private static function setIP($ip)
{
\SplitIO\Component\Common\Di::set('ipAddress', $ip);
}
/**
* Check factory instance
*/
private static function instanceExists()
{
$value = Di::get(Di::KEY_FACTORY_TRACKER);
if (is_null($value) || !$value) {
return false;
}
Di::getLogger()->critical("Factory Instantiation: creating multiple factories is not possible. "
. "You have already created a factory.");
return true;
}
/**
* Register factory instance
*/
private static function registerInstance()
{
Di::set(Di::KEY_FACTORY_TRACKER, true);
}
}