AccountService.php
132 lines
| 1 | <?php |
| 2 | namespace SureCart\Account; |
| 3 | |
| 4 | use SureCart\Models\Account; |
| 5 | use SureCart\Models\ApiToken; |
| 6 | |
| 7 | /** |
| 8 | * Service for plugin activation. |
| 9 | */ |
| 10 | class AccountService { |
| 11 | /** |
| 12 | * Holds the global account model. |
| 13 | * |
| 14 | * @var \SureCart\Models\Account; |
| 15 | */ |
| 16 | protected $account = null; |
| 17 | |
| 18 | /** |
| 19 | * The key for the cache. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $cache_key = 'surecart_account'; |
| 24 | |
| 25 | /** |
| 26 | * Bootstrap the service. |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public function bootstrap() { |
| 31 | // clear account cache when account is updated. |
| 32 | \add_action( 'surecart/account_updated', [ $this, 'clearCache' ] ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * We get the account when the service is loaded. |
| 37 | * Since this is loaded in a service container, its |
| 38 | * cached so it only fetches once, no matter how many calls. |
| 39 | * |
| 40 | * This is also cached in a 60 second transient to prevent |
| 41 | * rate limited calls to the API. |
| 42 | * |
| 43 | * @param \SureCart\Support\Server $server The server utility to use. |
| 44 | */ |
| 45 | public function __construct( \SureCart\Support\Server $server ) { |
| 46 | $cache = null; |
| 47 | |
| 48 | if ( defined( 'SURECART_CACHE_ACCOUNT' ) ) { |
| 49 | $cache = SURECART_CACHE_ACCOUNT; |
| 50 | } |
| 51 | |
| 52 | // do not cache requests if specifically set to false. |
| 53 | if ( false === $cache ) { |
| 54 | return $this->fetchAccount(); |
| 55 | } |
| 56 | |
| 57 | // cache requests if specifically set to true. |
| 58 | if ( true === $cache ) { |
| 59 | return $this->fetchCachedAccount(); |
| 60 | } |
| 61 | |
| 62 | // don't cache on localhost if constant is not set. |
| 63 | if ( $server->isLocalHost() ) { |
| 64 | return $this->fetchAccount(); |
| 65 | } |
| 66 | |
| 67 | // cache requests if not explicitly set. |
| 68 | return $this->fetchCachedAccount(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Fetch the cached account. |
| 73 | * |
| 74 | * @return \SureCart\Models\Account |
| 75 | */ |
| 76 | public function fetchCachedAccount() { |
| 77 | $this->account = get_transient( $this->cache_key ); |
| 78 | if ( false === $this->account ) { |
| 79 | // fetch account. |
| 80 | $this->account = $this->fetchAccount(); |
| 81 | |
| 82 | // there was an error or the account could not be fetched by other means. |
| 83 | if ( is_wp_error( $this->account ) || empty( $this->account->id ) ) { |
| 84 | delete_transient( $this->cache_key ); |
| 85 | return $this->account; |
| 86 | } |
| 87 | |
| 88 | // set the transient. |
| 89 | set_transient( $this->cache_key, $this->account, 15 * MINUTE_IN_SECONDS ); |
| 90 | } |
| 91 | return $this->account; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Fetch the account. |
| 96 | * |
| 97 | * @return \SureCart\Models\Account |
| 98 | */ |
| 99 | protected function fetchAccount() { |
| 100 | $this->account = Account::with( [ 'brand', 'brand.address', 'portal_protocol', 'tax_protocol', 'tax_protocol.address', 'subscription_protocol', 'shipping_protocol', 'affiliation_protocol' ] )->find(); |
| 101 | return $this->account; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Clear account cache. |
| 106 | * |
| 107 | * @return boolean |
| 108 | */ |
| 109 | public function clearCache() { |
| 110 | return delete_transient( $this->cache_key ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Is the account connected? |
| 115 | * |
| 116 | * @return boolean |
| 117 | */ |
| 118 | public function isConnected() { |
| 119 | return ! empty( ApiToken::get() ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the account model attribute |
| 124 | * |
| 125 | * @param string $attribute Attribute name. |
| 126 | * @return mixed |
| 127 | */ |
| 128 | public function __get( $attribute ) { |
| 129 | return $this->account->$attribute ?? null; |
| 130 | } |
| 131 | } |
| 132 |