Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Introduce Admin_Page_Collection interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Arntz committed Jan 14, 2018
1 parent 255b4d4 commit 84b6d68
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Admin_Page_Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Admin_Page_Collection interface
*
* @package Leaves_And_Love\OOP_Admin_Pages
* @since 1.0.0
*/

namespace Leaves_And_Love\OOP_Admin_Pages;

use Leaves_And_Love\OOP_Admin_Pages\Exception\Admin_Page_Not_Found_Exception;

/**
* Interface to manage admin page instances.
*
* @since 1.0.0
*/
interface Admin_Page_Collection {

/**
* Registers the admin pages in the collection within the environment.
*
* @since 1.0.0
*/
public function register();

/**
* Checks whether an admin page is part of the collection.
*
* @since 1.0.0
*
* @param string $slug Identifier of the admin page to check for.
* @return bool True if the admin page is part of the collection, false otherwise.
*/
public function has_page( string $slug ) : bool;

/**
* Gets an admin page instance that is part of the collection.
*
* @since 1.0.0
*
* @param string $slug Identifier of the admin page to get.
* @return Admin_Page Admin page instance.
*
* @throws Admin_Page_Not_Found_Exception Thrown when the admin page is not found.
*/
public function get_page( string $slug ) : Admin_Page;

This comment has been minimized.

Copy link
@felixarntz

felixarntz Jan 20, 2018

Owner

It's usually a bad practice to provide different return types, since it means the consuming code needs to perform a check. Instead a lookup like this should either return a respective null object, or alternatively throw an exception, as in this example. A method to check whether an instance is registered should exist in order to work around throwing an exception.

}
20 changes: 20 additions & 0 deletions src/Exception/Admin_Page_Not_Found_Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Admin_Page_Not_Found_Exception class
*
* @package Leaves_And_Love\OOP_Admin_Pages\Exception
* @since 1.0.0
*/

namespace Leaves_And_Love\OOP_Admin_Pages\Exception;

use Exception;

/**
* Exception thrown when an admin page is not found in a collection.
*
* @since 1.0.0
*/
class Admin_Page_Not_Found_Exception extends Exception {

}

1 comment on commit 84b6d68

@felixarntz
Copy link
Owner

@felixarntz felixarntz commented on 84b6d68 Jan 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Admin_Page_Collection interface defines a container to hold the registered admin pages. This is not entirely necessary, but it's a common practice in WordPress to make registered objects available for third-party extensions. Having a collection to retrieve instances from makes this possible.

Another custom exception is also added, thrown in case an unregistered admin page should be retrieved.

Please sign in to comment.