Model.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace ForkBB\Models\Stats;
  3. use ForkBB\Models\Model as ParentModel;
  4. use PDO;
  5. use RuntimeException;
  6. class Model extends ParentModel
  7. {
  8. /**
  9. * Загружает статистику из кеша/БД
  10. */
  11. public function init(): Model
  12. {
  13. $list = $this->c->Cache->get('stats');
  14. if (! \is_array($list)) {
  15. $list = $this->c->users->stats();
  16. if (true !== $this->c->Cache->set('stats', $list)) {
  17. throw new RuntimeException('Unable to write value to cache - stats');
  18. }
  19. }
  20. $this->userTotal = $list['total'];
  21. $this->userLast = $list['last'];
  22. $query = 'SELECT SUM(f.num_topics), SUM(f.num_posts)
  23. FROM ::forums AS f';
  24. list($this->topicTotal, $this->postTotal) = $this->c->DB->query($query)->fetch(PDO::FETCH_NUM);
  25. return $this;
  26. }
  27. /**
  28. * Сбрасывает кеш статистики
  29. */
  30. public function reset(): Model
  31. {
  32. if (true !== $this->c->Cache->delete('stats')) {
  33. throw new RuntimeException('Unable to remove key from cache - stats');
  34. }
  35. return $this;
  36. }
  37. }