Skip to content

Commit

Permalink
feature: add notice repository
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Apr 19, 2023
1 parent 0529d30 commit a1a3b57
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
24 changes: 23 additions & 1 deletion includes/class-notice-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
*/
class Notice_Factory {

/**
* Container for the main instance of the class.
*
* @var Notice_Repository|null
*/
private static $instance = null;

/**
* Instantiates a Notice object.
Expand Down Expand Up @@ -50,7 +57,7 @@ public function make( $args ) {
$displayed_at = array_key_exists( 'displayed_at', $parsed ) ? $parsed['displayed_at'] : null;
$expires_at = array_key_exists( 'expires_at', $parsed ) ? $parsed['expires_at'] : null;

// Deserialize MySQL datetime stings.
// Deserialize MySQL datetime strings.

$created_at = Helpers\Serde::maybe_deserialize_mysql_date( $created_at );
$dismissed_at = Helpers\Serde::maybe_deserialize_mysql_date( $dismissed_at );
Expand All @@ -70,4 +77,19 @@ public function make( $args ) {

return $notice;
}

/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @return Notice_Factory The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}
}
159 changes: 159 additions & 0 deletions includes/class-notice-repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace WP\Notifications;

use DateTime;
use DateInterval;
use DateTimeInterface;

class Notice_Repository {

/**
* Container for the main instance of the class.
*
* @var Notice_Repository|null
*/
private static $instance = null;

/**
* Supported collection query parameters.
*/
protected $collection_query_params = array( 'count', 'offset' );

/**
* Supported sort by parameters.
*/
protected $sort_by_params = array( 'created_at' );

/**
* Find a notice by channel name and user ID.
*
* @param string $channel_name Namespaced name of the channel to look for.
* @param int $user_id User ID to look for.
*
* @return Notice|false Maybe a notice that match the user's ID and channel's
* name, or false if not found.
*/
public function find_by( string $channel_name, int $user_id ) {
global $wpdb;

$row = $wpdb->get_row(
$wpdb->prepare(
'SELECT `channel_name`, `user_id`, `snoozed_until` FROM `' . $wpdb->prefix . 'notifications_subscriptions` WHERE `channel_name` = %s AND `user_id` = %d',
$channel_name,
$user_id
),
ARRAY_A
);

if ( null === $row ) {
return false;
}

return Subscription_Factory::get_instance()->make( $row );
}

/**
* Find notes by user ID.
*
* @param int $user_id User ID to look for.
*
* @return Subscription[] A collection of notices that match the user's ID, an empty array if none are found.
*/
public function find_by_user_id( int $user_id, array $args ) {
global $wpdb;

$rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT `channel_name`, `created_at`, `user_id`, `snoozed_until` FROM . $wpdb->prefix . 'notifications_subscriptions` WHERE `user_id` = %d",
$user_id
),
ARRAY_A
);

if ( null === $rows ) {
return false;
}

$subscriptions = array();

foreach ( $rows as $row ) {
$subscription = Subscription_Factory::get_instance()->make( $row );

if ( ! ( false === $subscription ) ) {
$subscriptions[] = $subscription;
}
}

return $subscriptions;
}

/**
* Insert a notice into the database.
*
* A notice is composed of possibly multiple message rows containing translated
* message data matching the preferences of users subscribed to the channel, the
* messages are related to users through the queue table. The queue table contains
* all the data associated with an individual user's notification state.
*
* @return bool Whether insert is successful,
*/
public function insert( Subscription $subscription ) {
global $wpdb;

$snoozed_until = $subscription->get_snoozed_until();

if ( $snoozed_until instanceof DateTime ) {
$snoozed_until = $snoozed_until()->format( 'Y-m-d H:i:s' );
}

// if ( ! Subscription_Factory::validate_user_id( $subscription ) ) {
// return false;
// }

$result = $wpdb->query(
$wpdb->prepare(
'INSERT INTO `' . $wpdb->prefix . 'notifications_subscriptions` (
`channel_name`,
`created_at`,
`user_id`,
`snoozed_until`
)
VALUES (
%s,
UTC_TIMESTAMP(),
%d,
%s
);',
array(
$subscription->get_channel_name(),
$subscription->get_user_id(),
$snoozed_until,
),
)
);

if ( ! ( false === $result ) ) {
if ( 0 < $result ) {
return true;
}
}

return false;
}

/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @return Notice_Repository The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}
}
2 changes: 2 additions & 0 deletions includes/class-subscription-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function make( $args, $validate = false ) {
$created_at = array_key_exists( 'created_at', $parsed ) ? $parsed['created_at'] : null;
$snoozed_until = array_key_exists( 'snoozed_until', $parsed ) ? $parsed['snoozed_until'] : null;

// Deserialize MySQL datetime strings.

$created_at = Helpers\Serde::maybe_deserialize_mysql_date( $created_at );
$snoozed_until = Helpers\Serde::maybe_deserialize_mysql_date( $snoozed_until );

Expand Down

0 comments on commit a1a3b57

Please sign in to comment.