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