Renew subscriptions that got no response.
[phinde.git] / src / phinde / Subscriptions.php
index 5aac9b25d1200d7b99c4cf0b371f7474b29f8f97..e49e817549d811981fcfa362584e18129e0a68c5 100644 (file)
@@ -37,6 +37,22 @@ class Subscriptions
         return $stmt->fetchObject();
     }
 
+    /**
+     * Remove a topic
+     *
+     * @param string $topic Topic URL
+     *
+     * @return void
+     */
+    public function remove($topic)
+    {
+        $stmt = $this->db->prepare(
+            'DELETE FROM subscriptions'
+            . ' WHERE sub_topic = :topic'
+        );
+        $stmt->execute([':topic' => $topic]);
+    }
+
     /**
      * Count number of subscriptions
      *
@@ -59,6 +75,31 @@ class Subscriptions
         return $res;
     }
 
+    /**
+     * Get all topics that either expired or expire soon
+     *
+     * @return \PDOStatement Result iterator
+     */
+    public function getExpiring()
+    {
+        $stmt = $this->db->prepare(
+            'SELECT * FROM subscriptions'
+            . ' WHERE'
+            . '('
+            //expire soon
+            . '  sub_status IN ("active", "expired")'
+            . '  AND DATEDIFF(sub_expires, NOW()) <= 2'
+            . ') OR ('
+            //no reaction to subscription within 10 minutes
+            . '  sub_status = "subscribing"'
+            . '  AND TIMEDIFF(NOW(), sub_created) > "00:10:00"'
+            . ')'
+        );
+        $stmt->execute();
+
+        return $stmt;
+    }
+
     /**
      * Create a new subscription entry in database.
      * Automatically generates secret, capkey and lease seconds.
@@ -94,6 +135,23 @@ class Subscriptions
         );
     }
 
+    /**
+     * Renew a subscription: Set its status to "subscribing"
+     *
+     * @param integer $subId Subscription ID
+     *
+     * @return void
+     */
+    public function renew($subId)
+    {
+        $this->db->prepare(
+            'UPDATE subscriptions'
+            . ' SET sub_status  = "subscribing"'
+            . '   , sub_updated = NOW()'
+            . ' WHERE sub_id = :id'
+        )->execute([':id' => $subId]);
+    }
+
     /**
      * A subscription has been confirmed by the hub - mark it as active.
      *
@@ -137,6 +195,14 @@ class Subscriptions
         )->execute([':id' => $subId]);
     }
 
+    /**
+     * Subscription has been cancelled/denied for some reason
+     *
+     * @param integer $subId  Subscription ID
+     * @param string  $reason Cancellation reason
+     *
+     * @return void
+     */
     public function denied($subId, $reason)
     {
         $this->db->prepare(
@@ -148,6 +214,13 @@ class Subscriptions
         )->execute([':id' => $subId, ':reason' => $reason]);
     }
 
+    /**
+     * Topic update notification has been received
+     *
+     * @param integer $subId  Subscription ID
+     *
+     * @return void
+     */
     public function pinged($subId)
     {
         $this->db->prepare(