Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| MSUsers | |
0.00% |
0 / 37 |
|
0.00% |
0 / 10 |
306 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getInstance | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| offsetExists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| offsetGet | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| offsetSet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| offsetUnset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFromSession | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| storeInSession | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| fetchFromApi | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getUsers | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace AmgGroup; |
| 5 | |
| 6 | include_once '..\vendor\autoload.php'; |
| 7 | |
| 8 | //use AmgGroup\ApiClient; |
| 9 | class MSUsers implements \ArrayAccess |
| 10 | { |
| 11 | |
| 12 | const CACHE_KEY = 'msusers'; |
| 13 | |
| 14 | private static MSUsers $instance; |
| 15 | private array $cacheDefaults = [ |
| 16 | 'users' => 300 |
| 17 | ]; |
| 18 | private Config $config; |
| 19 | private Logger $logger; |
| 20 | |
| 21 | private function __construct() |
| 22 | { |
| 23 | $this->config = Config::getInstance(); |
| 24 | $this->logger = Logger::getInstance(); |
| 25 | |
| 26 | // Ensure session is started for caching |
| 27 | if (session_status() === PHP_SESSION_NONE) { |
| 28 | session_start(); |
| 29 | } |
| 30 | if (!isset($_SESSION[self::CACHE_KEY])) { |
| 31 | $_SESSION[self::CACHE_KEY] = []; |
| 32 | } |
| 33 | |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Provides access to the single instance of Smarty. |
| 38 | * |
| 39 | * @return \AmgGroup\MSUsers |
| 40 | */ |
| 41 | public static function getInstance(): MSUsers |
| 42 | { |
| 43 | if (self::$instance === null) { |
| 44 | self::$instance = new self(); |
| 45 | } |
| 46 | return self::$instance; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * @inheritDoc |
| 52 | */ |
| 53 | public function offsetExists(mixed $offset): bool |
| 54 | { |
| 55 | return $this->getFromSession($offset) !== null; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @inheritDoc |
| 60 | */ |
| 61 | public function offsetGet(mixed $offset): mixed |
| 62 | { |
| 63 | $cached = $this->getFromSession($offset); |
| 64 | if ($cached !== null) { |
| 65 | return $cached; |
| 66 | } |
| 67 | $fetchedData = $this->fetchFromApi($offset); |
| 68 | $this->storeInSession($offset, $fetchedData); |
| 69 | return $fetchedData; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @inheritDoc |
| 74 | */ |
| 75 | public function offsetSet(mixed $offset, mixed $value): void |
| 76 | { |
| 77 | $this->storeInSession($offset, $value); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @inheritDoc |
| 82 | */ |
| 83 | public function offsetUnset(mixed $offset): void |
| 84 | { |
| 85 | unset($_SESSION[self::CACHE_KEY][$offset]); |
| 86 | } |
| 87 | |
| 88 | private function getFromSession(string $offset): mixed |
| 89 | { |
| 90 | if (isset($_SESSION[self::CACHE_KEY][$offset])) { |
| 91 | $entry=$_SESSION[self::CACHE_KEY][$offset]; |
| 92 | if (time() - $entry['timestamp'] <= $this->cacheDefaults['users']) { |
| 93 | return $entry['data']; |
| 94 | } |
| 95 | unset($_SESSION[self::CACHE_KEY][$offset]); |
| 96 | } |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | private function storeInSession(string $offset, mixed $value): void |
| 101 | { |
| 102 | $_SESSION[self::CACHE_KEY][$offset] = [ |
| 103 | 'data' => $value, |
| 104 | 'timestamp' => time() |
| 105 | ]; |
| 106 | } |
| 107 | |
| 108 | private function fetchFromApi(string $offset): mixed |
| 109 | { |
| 110 | // $offset holds the array key we are looking for download |
| 111 | $methodName='get'.ucfirst($offset); |
| 112 | $method=[$this, $methodName]; |
| 113 | return $method(); |
| 114 | } |
| 115 | |
| 116 | private function getUsers(): array |
| 117 | { |
| 118 | $api=ApiClient::getInstance(); |
| 119 | $users=$api->getAllPages('https://graph.microsoft.com/v1.0/users'); |
| 120 | $userList=[]; |
| 121 | foreach ($users as $user) { |
| 122 | $userList[$user['id']]=$user; |
| 123 | } |
| 124 | return $userList; |
| 125 | } |
| 126 | } |