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