Skip to content

Commit

Permalink
feature: add initial factory classes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Apr 20, 2023
1 parent 87d2c1f commit 460b3c7
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 0 deletions.
85 changes: 85 additions & 0 deletions includes/factory/class-messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Notifications API:Messages factory class
*
* @package wordpress/wp-feature-notifications
*/

namespace WP\Notifications\Factory;

use DateTime;
use WP\Notifications\Framework;
use WP\Notifications\Model;

/**
* Class representing a messages factory.
*
* @implements Framework\Factory<Messages>
*/
class Messages extends Framework\Factory {

/**
* Instantiates a Message object.
*
* @param array|string $args {
* Array or string of arguments for creating a message. Supported arguments
* are described below.
*
* @type string $message Text content of the message.
* @type ?string $accept_label Optional label of the accept action.
* @type ?string $accept_link Optional url of the accept action.
* @type ?string $accept_message Optional label of the accept action.
* @type ?string $channel_title Optional human-readable title of the channel
* the message was emitted from.
* @type ?DateTime $created_at Optional datetime at which a message was created.
* Default `'null'`
* @type ?string $dismiss_label Optional label of the dismiss action.
* @type ?DateTime $expires_at Optional datetime at which a message expires.
* Default `'null'`
* @type ?string $icon Optional icon of the message. Default `null`
* @type ?int $id Optional database ID of the message. Default `null`
* @type bool $is_dismissible Optional boolean of whether the notice can
* be dismissed. Default `true`
* @type ?string $severity Optional severity of the message. Default `null`
* @type string $title Optional human-readable message label. Default `''`
* }
*
* @return Model\Message|false A newly created instance of Message or false.
*/
public function make( $args ) {
$parsed = wp_parse_args( $args );

// Required properties

$message = $parsed['message'];

// Optional properties

$accept_label = array_key_exists( 'accept_label', $parsed ) ? $parsed['accept_label'] : null;
$accept_link = array_key_exists( 'accept_link', $parsed ) ? $parsed['accept_link'] : null;
$channel_title = array_key_exists( 'channel_title', $parsed ) ? $parsed['channel_title'] : null;
$created_at = array_key_exists( 'created_at', $parsed ) ? $parsed['created_at'] : null;
$dismiss_label = array_key_exists( 'dismiss_label', $parsed ) ? $parsed['dismiss_label'] : null;
$expires_at = array_key_exists( 'expires_at', $parsed ) ? $parsed['expires_at'] : null;
$icon = array_key_exists( 'icon', $parsed ) ? $parsed['icon'] : null;
$id = array_key_exists( 'id', $parsed ) ? $parsed['id'] : null;
$is_dismissible = array_key_exists( 'is_dismissible', $parsed ) ? $parsed['is_dismissible'] : true;
$severity = array_key_exists( 'severity', $parsed ) ? $parsed['severity'] : null;
$title = array_key_exists( 'title', $parsed ) ? $parsed['title'] : '';

return new Model\Message(
$message,
$accept_label,
$accept_link,
$channel_title,
$created_at,
$dismiss_label,
$expires_at,
$icon,
$id,
$is_dismissible,
$severity,
$title,
);
}
}
87 changes: 87 additions & 0 deletions includes/factory/class-notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Notifications API:Notifications factory class
*
* @package wordpress/wp-feature-notifications
*/

namespace WP\Notifications\Factory;

use DateTime;
use WP\Notifications\Framework;
use WP\Notifications\Helper;
use WP\Notifications\Model;

/**
* Class representing a notifications factory.
*
* @implements Framework\Factory<Notifications>
*/
class Notifications extends Framework\Factory {

/**
* Instantiates a Notification object.
* @param array|string $args {
* Array or string of arguments for creating a notification. Supported arguments are described below.
*
* @type string $channel_name Channel name, including namespace,
* the notification was emitted from.
* @type int $message_id ID of the message related to the
* notification.
* @type int $user_id ID of the user the notification
* belongs to.
* @type ?string $context Optional display context of the
* notification. Default `'adminbar'`
* @type string|DateTime|null $created_at Optional datetime at which the
* notification was created. Default `null`
* @type string|DateTime|null $dismissed_at Optional datetime t which the
* notification was dismissed. Default `null`
* @type string|DateTime|null $displayed_at Optional datetime at which the
* notification was first displayed.
* Default `null`
* @type string|DateTime|null $expires_at Optional datetime at which the
* notification expires. Default `null`
* }
* @param bool $validate Optionally validate the arguments.
*
* @return Model\Notification|false A newly created instance of Channel or false.
*/
public function make( $args ) {
$parsed = wp_parse_args( $args );

// Required properties

$channel_name = $parsed['channel_name'];
$message_id = $parsed['message_id'];
$user_id = $parsed['user_id'];

// Optional properties

$context = array_key_exists( 'context', $parsed ) ? $parsed['context'] : 'adminbar';
$created_at = array_key_exists( 'created_at', $parsed ) ? $parsed['created_at'] : null;
$dismissed_at = array_key_exists( 'dismissed_at', $parsed ) ? $parsed['dismissed_at'] : null;
$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 strings.

$created_at = Helper\Serde::maybe_deserialize_mysql_date( $created_at );
$dismissed_at = Helper\Serde::maybe_deserialize_mysql_date( $dismissed_at );
$displayed_at = Helper\Serde::maybe_deserialize_mysql_date( $displayed_at );
$expires_at = Helper\Serde::maybe_deserialize_mysql_date( $expires_at );

$notification = new Model\Notification(
$channel_name,
$message_id,
$user_id,
$context,
$created_at,
$dismissed_at,
$displayed_at,
$expires_at
);

return $notification;
}
}
68 changes: 68 additions & 0 deletions includes/factory/class-subscriptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Notifications API:Subscriptions factory class
*
* @package wordpress/wp-feature-notifications
*/

namespace WP\Notifications;

use DateTime;
use WP\Notifications\Framework;
use WP\Notifications\Helper;
use WP\Notifications\Model;

/**
* Class representing a subscriptions factory
*
* @implements Framework\Factory<Subscriptions>.
*/
class Subscriptions extends Framework\Factory {

/**
* Instantiates a Subscription object.
*
* @param array|string $args {
* Array or string of arguments for creating a subscription. Supported
* arguments are described below.
*
* @type string $channel_name Namespaced channel name of the
* subscription.
* @type int $user_id ID of the user the subscription
* belongs to.
* @type string|DateTime|null $created_at Optional datetime at which the
* subscription was created.
* @type string|DateTime|null $snoozed_until Optional snoozed until datetime
* of the subscription.
* }
*
* @return Model\Subscription|false A newly created instance of Subscription or false.
*/
public function make( $args ) {
$parsed = wp_parse_args( $args );

// Required properties

$channel_name = $parsed['channel_name'];
$user_id = $parsed['user_id'];

// Optional properties

$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 = Helper\Serde::maybe_deserialize_mysql_date( $created_at );
$snoozed_until = Helper\Serde::maybe_deserialize_mysql_date( $snoozed_until );

$subscription = new Model\Subscription(
$channel_name,
$user_id,
$created_at,
$snoozed_until,
);

return $subscription;
}
}
48 changes: 48 additions & 0 deletions includes/framework/class-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Notifications API:Factory abstract class
*
* @package wordpress/wp-feature-notifications
*/

namespace WP\Notifications\Framework;

/**
* Abstract class representing a factory.
*
* @template Model
*/
abstract class Factory {

/**
* Container for the main instance of the class.
*
* @var ?Model
*/
protected static $instance = null;

/**
* Instantiates a model object.
* @param array|string $args Array or string of arguments for creating a model.
*
* @return Model|false A newly created instance of model or false.
*/
abstract public function make( $args );

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

return self::$instance;
}
}
4 changes: 4 additions & 0 deletions wp-feature-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
}

// Require interface/class declarations..
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/framework/class-factory.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/helper/class-serde.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/model/class-channel.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/model/class-message.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/model/class-notification.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/model/class-subscription.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/factory/class-messages.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/factory/class-notifications.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/factory/class-subscriptions.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/exceptions/interface-exception.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/exceptions/class-runtime-exception.php';
require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/interface-status.php';
Expand Down

0 comments on commit 460b3c7

Please sign in to comment.