Stats.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Stats;
  10. use ForkBB\Models\Model;
  11. use PDO;
  12. use RuntimeException;
  13. class Stats extends Model
  14. {
  15. /**
  16. * Ключ модели для контейнера
  17. * @var string
  18. */
  19. protected $cKey = 'Stats';
  20. /**
  21. * Загружает статистику из кеша/БД
  22. */
  23. public function init(): Stats
  24. {
  25. $list = $this->c->Cache->get('stats');
  26. if (! \is_array($list)) {
  27. $list = $this->c->users->stats();
  28. if (true !== $this->c->Cache->set('stats', $list)) {
  29. throw new RuntimeException('Unable to write value to cache - stats');
  30. }
  31. }
  32. $this->userTotal = $list['total'];
  33. $this->userLast = $list['last'];
  34. $query = 'SELECT SUM(f.num_topics), SUM(f.num_posts)
  35. FROM ::forums AS f';
  36. list($this->topicTotal, $this->postTotal) = $this->c->DB->query($query)->fetch(PDO::FETCH_NUM);
  37. return $this;
  38. }
  39. /**
  40. * Сбрасывает кеш статистики
  41. */
  42. public function reset(): Stats
  43. {
  44. if (true !== $this->c->Cache->delete('stats')) {
  45. throw new RuntimeException('Unable to remove key from cache - stats');
  46. }
  47. return $this;
  48. }
  49. }