PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 2.1.7
Backup Migration v2.1.7
2.1.7 2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / analyst / src / Analyst.php
backup-backup / analyst / src Last commit date
Account 2 weeks ago Cache 2 weeks ago Contracts 2 weeks ago Core 2 weeks ago Http 2 weeks ago Notices 2 weeks ago Storage 2 weeks ago Analyst.php 2 weeks ago ApiRequestor.php 2 weeks ago ApiResponse.php 2 weeks ago Collector.php 2 weeks ago Mutator.php 2 weeks ago helpers.php 2 weeks ago
Analyst.php
181 lines
1 <?php
2
3
4 namespace Analyst;
5
6 if ( ! defined( 'ABSPATH' ) ) exit;
7
8 use Account\Account;
9 use Account\AccountDataFactory;
10 use Analyst\Cache\DatabaseCache;
11 use Analyst\Contracts\AnalystContract;
12 use Analyst\Contracts\RequestorContract;
13 use Analyst\Storage\DatabaseStorage;
14 use Analyst\Storage\FileStorage;
15
16 class Analyst implements AnalystContract
17 {
18 /**
19 * All plugin's accounts
20 *
21 * @var array
22 */
23 protected $accounts = array();
24
25 /**
26 * @var Mutator
27 */
28 protected $mutator;
29
30 /**
31 * @var AccountDataFactory
32 */
33 protected $accountDataFactory;
34
35 /**
36 * Base url to api
37 *
38 * @var string
39 */
40 protected $apiBase = 'https://feedback.sellcodes.com/api/v1';
41
42 /**
43 * @var Collector
44 */
45 protected $collector;
46
47 /**
48 * Singleton instance
49 *
50 * @var static
51 */
52 protected static $instance;
53
54 /**
55 * Get instance of analyst
56 *
57 * @return Analyst
58 * @throws \Exception
59 */
60 public static function getInstance()
61 {
62 if (!static::$instance) {
63 static::$instance = new Analyst();
64 }
65
66 return static::$instance;
67 }
68
69 protected function __construct()
70 {
71 $dbStorage = new DatabaseStorage();
72 $fileStorage = new FileStorage();
73
74 DatabaseCache::setStorageBackends($dbStorage, $fileStorage);
75 AccountDataFactory::setStorageBackends($dbStorage, $fileStorage);
76
77 $this->mutator = new Mutator();
78
79 $this->accountDataFactory = AccountDataFactory::instance();
80
81 $this->mutator->initialize();
82
83 $this->collector = new Collector($this);
84
85 $this->initialize();
86 }
87
88 /**
89 * Initialize rest of application
90 */
91 public function initialize()
92 {
93 add_action('wp_loaded', function () {
94 $this->collector->loadCurrentUser();
95 });
96 }
97
98 /**
99 * Register new account
100 *
101 * @param Account $account
102 * @return Analyst
103 * @throws \Exception
104 */
105 public function registerAccount($account)
106 {
107 // Stop propagation when account is already registered
108 if ($this->isAccountRegistered($account)) {
109 return $this;
110 }
111
112 // Resolve account data from factory
113 $accountData = $this->accountDataFactory->resolvePluginAccountData($account);
114
115 $account->setData($accountData);
116
117 $account->setRequestor(
118 $this->resolveRequestorForAccount($account)
119 );
120
121 $account->setCollector($this->collector);
122
123 $account->registerHooks();
124
125 $this->accounts[$account->getId()] = $account;
126
127 return $this;
128 }
129
130 /**
131 * Must return version of analyst
132 *
133 * @return string
134 */
135 public static function version()
136 {
137 $version = require __DIR__ . '/../version.php';
138
139 return $version['sdk'];
140 }
141
142 /**
143 * Is this account registered
144 *
145 * @param Account $account
146 * @return bool
147 */
148 protected function isAccountRegistered($account)
149 {
150 return isset($this->accounts[$account->getId()]);
151 }
152
153 /**
154 * Resolves requestor for account
155 *
156 * @param Account $account
157 * @return RequestorContract
158 * @throws \Exception
159 */
160 protected function resolveRequestorForAccount(Account $account)
161 {
162 $requestor = new ApiRequestor($account->getId(), $account->getClientSecret(), $this->apiBase);
163
164 // Set SDK version
165 $requestor->setDefaultHeader(
166 'x-analyst-client-user-agent',
167 sprintf('Analyst/%s', $this->version())
168 );
169
170 return $requestor;
171 }
172
173 /**
174 * @return string
175 */
176 public function getApiBase()
177 {
178 return $this->apiBase;
179 }
180 }
181