/** * Deletes the info with given id. * * @param string $id * * @throws NonExistentIdApiException */ public function delete($id) { $result = $this->connectToStorageInternalWorker->connect()->remove(['id' => $id]); if ($result['n'] == 0) { throw new NonExistentIdApiException(); } }
/** * Pops oldest info. * * @return array The info as an array with the following keys: * id, body, topics and created * * @throws EmptyQueueInternalException */ public function pop() { $item = $this->connectToInfoStorageInternalWorker->connect()->find()->sort(['created' => 1])->limit(1)->getNext(); if (!$item) { throw new EmptyQueueInternalException(); } $this->connectToInfoStorageInternalWorker->connect()->remove(['id' => $item['id']]); return $item; }
/** * Creates an info. * * @param string $body * @param string[] $topics * * @throws BlankBodyApiException * @throws NoTopicsApiException * @throws NonExistentTopicApiException */ public function create($body, $topics) { if ($body === '') { throw new BlankBodyApiException(); } if (count($topics) == 0) { throw new NoTopicsApiException(); } foreach ($topics as $topic) { try { $this->pickTopicApiWorker->pick($topic); } catch (NonExistentIdApiException $e) { throw new NonExistentTopicApiException(); } } $this->connectToStorageInternalWorker->connect()->insert(array('id' => uniqid(), 'body' => $body, 'topics' => $topics, 'created' => time())); }
/** * Updates the info with given id. * * @param string $id * @param string $body * @param string[] $topics * * @throws BlankBodyApiException * @throws NoTopicsApiException * @throws NonExistentTopicApiException * @throws NonExistentIdApiException */ public function update($id, $body, $topics) { if ($body === '') { throw new BlankBodyApiException(); } if (count($topics) == 0) { throw new NoTopicsApiException(); } foreach ($topics as $topic) { try { $this->pickTopicApiWorker->pick($topic); } catch (NonExistentIdApiException $e) { throw new NonExistentTopicApiException(); } } $result = $this->connectToStorageInternalWorker->connect()->update(array('id' => $id), array('$set' => array('body' => $body, 'topics' => $topics, 'created' => time()))); if ($result['n'] == 0) { throw new NonExistentIdApiException(); } }
/** * Creates an info. * * @param string $id * @param string $body * @param string[] $topics */ public function create($id, $body, $topics) { $this->connectToStorageInternalWorker->connect()->insert(array('id' => $id, 'body' => $body, 'topics' => $topics, 'created' => time())); sleep(1); }
/** * Collects infos, sorted descending by creation date. * * @return \Iterator An array of infos with the following keys: * id, body, topics and created */ public function collect() { return $this->connectToStorageInternalWorker->connect()->find()->fields(['_id' => 0])->sort(['created' => -1]); }