Skip to content
Permalink
Browse files

Introduce Admin_Page_Collection interface.

  • Loading branch information...
felixarntz committed Jan 14, 2018
1 parent 255b4d4 commit 84b6d689821b820fbd6cfc93123ef6db1f9a7cd4
Showing with 68 additions and 0 deletions.
  1. +48 −0 src/Admin_Page_Collection.php
  2. +20 −0 src/Exception/Admin_Page_Not_Found_Exception.php
@@ -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

Author 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.

}
@@ -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

This comment has been minimized.

Copy link
Owner Author

commented on 84b6d68 Jan 20, 2018

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.
You can’t perform that action at this time.