AccountService.php
103 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 | * We get the account when the service is loaded. |
| 26 | * Since this is loaded in a service container, its |
| 27 | * cached so it only fetches once, no matter how many calls. |
| 28 | * |
| 29 | * This is also cached in a 60 second transient to prevent |
| 30 | * rate limited calls to the API. |
| 31 | * |
| 32 | * @param \SureCart\Support\Server $server The server utility to use. |
| 33 | */ |
| 34 | public function __construct( \SureCart\Support\Server $server ) { |
| 35 | $cache = null; |
| 36 | |
| 37 | if ( defined( 'SURECART_CACHE_ACCOUNT' ) ) { |
| 38 | $cache = SURECART_CACHE_ACCOUNT; |
| 39 | } |
| 40 | |
| 41 | // do not cache requests if specifically set to false. |
| 42 | if ( false === $cache ) { |
| 43 | return $this->fetchAccount(); |
| 44 | } |
| 45 | |
| 46 | // cache requests if specifically set to true. |
| 47 | if ( true === $cache ) { |
| 48 | return $this->fetchCachedAccount(); |
| 49 | } |
| 50 | |
| 51 | // don't cache on localhost if constant is not set. |
| 52 | if ( $server->isLocalHost() ) { |
| 53 | return $this->fetchAccount(); |
| 54 | } |
| 55 | |
| 56 | // cache requests if not explicitly set. |
| 57 | return $this->fetchCachedAccount(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Fetch the cached account. |
| 62 | * |
| 63 | * @return \SureCart\Models\Account |
| 64 | */ |
| 65 | public function fetchCachedAccount() { |
| 66 | $this->account = get_transient( $this->cache_key ); |
| 67 | if ( false === $this->account ) { |
| 68 | $this->account = $this->fetchAccount(); |
| 69 | set_transient( $this->cache_key, $this->account, 60 ); |
| 70 | } |
| 71 | return $this->account; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Fetch the account. |
| 76 | * |
| 77 | * @return \SureCart\Models\Account |
| 78 | */ |
| 79 | protected function fetchAccount() { |
| 80 | $this->account = Account::with( [ 'brand', 'brand.address', 'portal_protocol', 'tax_protocol', 'tax_protocol.address' ] )->find(); |
| 81 | return $this->account; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Clear account cache. |
| 86 | * |
| 87 | * @return boolean |
| 88 | */ |
| 89 | public function clearCache() { |
| 90 | return delete_transient( $this->cache_key ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the account model attribute |
| 95 | * |
| 96 | * @param string $attribute Attribute name. |
| 97 | * @return mixed |
| 98 | */ |
| 99 | public function __get( $attribute ) { |
| 100 | return $this->account->$attribute ?? null; |
| 101 | } |
| 102 | } |
| 103 |