123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- /**
- * This file is part of the ForkBB <https://github.com/forkbb>.
- *
- * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
- * @license The MIT License (MIT)
- */
- declare(strict_types=1);
- namespace ForkBB\Models\Poll;
- use ForkBB\Models\Manager;
- use ForkBB\Models\Poll\Poll;
- use RuntimeException;
- class Polls extends Manager
- {
- /**
- * Ключ модели для контейнера
- * @var string
- */
- protected $cKey = 'Polls';
- /**
- * Создает новый опрос
- */
- public function create(array $attrs = []): Poll
- {
- return $this->c->PollModel->setAttrs($attrs);
- }
- /**
- * Получает опрос по id
- */
- public function load(int $id): ?Poll
- {
- if ($this->isset($id)) {
- return $this->get($id);
- } else {
- $data = $this->c->Cache->get("poll{$id}", false);
- if (null === $data) {
- $poll = null;
- } elseif (\is_array($data)) {
- $poll = $this->create($data);
- } else {
- $poll = $this->Load->load($id);
- $data = $poll instanceof Poll ? $poll->getAttrs() : null; // ????
- $this->c->Cache->set("poll{$id}", $data);
- }
- $this->set($id, $poll);
- return $poll;
- }
- }
- /**
- * Обновляет опрос в БД
- */
- public function update(Poll $poll): Poll
- {
- $poll = $this->Save->update($poll);
- if (true === $poll->itWasModified) {
- $this->reset($poll->tid);
- }
- return $poll;
- }
- /**
- * Добавляет новый опрос в БД
- */
- public function insert(Poll $poll): int
- {
- $id = $this->Save->insert($poll);
- $this->set($id, $poll);
- return $id;
- }
- /**
- * Сбрасывает кеш указанного голосования
- */
- public function reset(int $id): Polls
- {
- if (true !== $this->c->Cache->delete("poll{$id}")) {
- throw new RuntimeException("Unable to remove key from cache - poll{$id}");
- }
- return $this;
- }
- }
|