Chris@0: db->select(['id' => $data['id']]); Chris@0: if ($result && (0 < count($result))) { Chris@0: $data['created_time'] = $result->current()->created_time; Chris@0: $now = $this->getNow(); Chris@0: if (array_key_exists('lease_seconds', $data) Chris@0: && $data['lease_seconds'] Chris@0: ) { Chris@0: $data['expiration_time'] = $now->add(new DateInterval('PT' . $data['lease_seconds'] . 'S')) Chris@0: ->format('Y-m-d H:i:s'); Chris@0: } Chris@0: $this->db->update( Chris@0: $data, Chris@0: ['id' => $data['id']] Chris@0: ); Chris@0: return false; Chris@0: } Chris@0: Chris@0: $this->db->insert($data); Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get subscription by ID/key Chris@0: * Chris@0: * @param string $key Chris@0: * @return array Chris@0: * @throws PubSubHubbub\Exception\InvalidArgumentException Chris@0: */ Chris@0: public function getSubscription($key) Chris@0: { Chris@12: if (empty($key) || ! is_string($key)) { Chris@0: throw new PubSubHubbub\Exception\InvalidArgumentException('Invalid parameter "key"' Chris@0: .' of "' . $key . '" must be a non-empty string'); Chris@0: } Chris@0: $result = $this->db->select(['id' => $key]); Chris@12: if ($result && count($result)) { Chris@0: return $result->current()->getArrayCopy(); Chris@0: } Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determine if a subscription matching the key exists Chris@0: * Chris@0: * @param string $key Chris@0: * @return bool Chris@0: * @throws PubSubHubbub\Exception\InvalidArgumentException Chris@0: */ Chris@0: public function hasSubscription($key) Chris@0: { Chris@12: if (empty($key) || ! is_string($key)) { Chris@0: throw new PubSubHubbub\Exception\InvalidArgumentException('Invalid parameter "key"' Chris@0: .' of "' . $key . '" must be a non-empty string'); Chris@0: } Chris@0: $result = $this->db->select(['id' => $key]); Chris@12: if ($result && count($result)) { Chris@0: return true; Chris@0: } Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Delete a subscription Chris@0: * Chris@0: * @param string $key Chris@0: * @return bool Chris@0: */ Chris@0: public function deleteSubscription($key) Chris@0: { Chris@0: $result = $this->db->select(['id' => $key]); Chris@12: if ($result && count($result)) { Chris@0: $this->db->delete( Chris@0: ['id' => $key] Chris@0: ); Chris@0: return true; Chris@0: } Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a new DateTime or the one injected for testing Chris@0: * Chris@0: * @return DateTime Chris@0: */ Chris@0: public function getNow() Chris@0: { Chris@0: if (null === $this->now) { Chris@0: return new DateTime(); Chris@0: } Chris@0: return $this->now; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set a DateTime instance for assisting with unit testing Chris@0: * Chris@0: * @param DateTime $now Chris@0: * @return Subscription Chris@0: */ Chris@0: public function setNow(DateTime $now) Chris@0: { Chris@0: $this->now = $now; Chris@0: return $this; Chris@0: } Chris@0: }