| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Controller\Backend\Administration; |
| 6 |
|
| 7 |
use UserAccessManager\Controller\Controller; |
| 8 |
|
| 9 |
class AboutController extends Controller |
| 10 |
{ |
| 11 |
public const SUPPORTER_FILE = 'supporters.json'; |
| 12 |
public const SUPPORTER_FILE_URL = 'https://gm-alex.github.io/user-access-manager/supporters.json'; |
| 13 |
|
| 14 |
private const SUPPORTER_FILE_LIFETIME_IN_SECONDS = 24 * 60 * 60; |
| 15 |
|
| 16 |
protected ?string $template = 'AdminAbout.php'; |
| 17 |
private ?array $supporters = null; |
| 18 |
|
| 19 |
private function getAllSupporters(): ?array |
| 20 |
{ |
| 21 |
if ($this->supporters !== null) { |
| 22 |
return $this->supporters; |
| 23 |
} |
| 24 |
|
| 25 |
$realPath = rtrim($this->wordpressConfig->getRealPath(), DIRECTORY_SEPARATOR); |
| 26 |
$fileWithPath = $realPath . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . self::SUPPORTER_FILE; |
| 27 |
|
| 28 |
$needsUpdate = $this->php->isFile($fileWithPath) === false |
| 29 |
|| $this->php->fileMTime($fileWithPath) |
| 30 |
< $this->wordpress->currentTime('timestamp') - self::SUPPORTER_FILE_LIFETIME_IN_SECONDS; |
| 31 |
|
| 32 |
$fileContent = ($needsUpdate === true) ? $this->php->fileGetContents(self::SUPPORTER_FILE_URL) : false; |
| 33 |
|
| 34 |
if ($fileContent !== false) { |
| 35 |
$this->php->filePutContents($fileWithPath, $fileContent); |
| 36 |
} elseif ($this->php->isFile($fileWithPath) === true) { |
| 37 |
$fileContent = $this->php->fileGetContents($fileWithPath); |
| 38 |
} |
| 39 |
|
| 40 |
return $this->supporters = (is_string($fileContent) === true) ? json_decode($fileContent, true) : []; |
| 41 |
} |
| 42 |
|
| 43 |
private function getSupporterGroup(string $key): array |
| 44 |
{ |
| 45 |
return $this->getAllSupporters()[$key] ?? []; |
| 46 |
} |
| 47 |
|
| 48 |
public function getSpecialThanks(): array |
| 49 |
{ |
| 50 |
return $this->getSupporterGroup('special-thanks'); |
| 51 |
} |
| 52 |
|
| 53 |
public function getTopSupporters(): array |
| 54 |
{ |
| 55 |
return $this->getSupporterGroup('top-supporters'); |
| 56 |
} |
| 57 |
|
| 58 |
public function getSupporters(): array |
| 59 |
{ |
| 60 |
return $this->getSupporterGroup('supporters'); |
| 61 |
} |
| 62 |
} |
| 63 |
|