Account
2 years ago
Cache
2 years ago
Contracts
2 years ago
Core
2 years ago
Http
2 years ago
Notices
2 years ago
Analyst.php
2 years ago
ApiRequestor.php
2 years ago
ApiResponse.php
2 years ago
Collector.php
2 years ago
Mutator.php
2 years ago
helpers.php
2 years 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('init', 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 |