Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Module
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 make
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 validate
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
90
 register
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace NewfoldLabs\WP\ModuleLoader;
4
5use InvalidArgumentException;
6use WP_Forge\Fluent\Fluent;
7
8/**
9 * Module class.
10 *
11 * @property callable $callback
12 * @property boolean  $isActive
13 * @property boolean  $isHidden
14 * @property string   $label
15 * @property string   $name
16 *
17 * @method void callback( callable $callback )
18 * @method void isActive( boolean $isActive )
19 * @method void isHidden( boolean $isHidden )
20 * @method void label( string $label )
21 * @method void name( string $name )
22 */
23class Module extends Fluent {
24
25    /**
26     * Create a new module instance.
27     *
28     * @param array $attributes Module attributes
29     *
30     * @return Module
31     */
32    public static function make( $attributes = [] ) {
33        return new self( $attributes );
34    }
35
36    /**
37     * Constructor.
38     *
39     * @param array $attributes Module attributes
40     */
41    public function __construct( $attributes = [] ) {
42        $defaults = [
43            'isActive' => false,
44            'isHidden' => false,
45        ];
46        parent::__construct( array_merge( $defaults, $attributes ) );
47    }
48
49    /**
50     * Validate module.
51     *
52     * @return bool
53     *
54     * @throws InvalidArgumentException
55     */
56    public function validate() {
57
58        if ( ! $this->has( 'name' ) ) {
59            throw new InvalidArgumentException( 'Module must have a name!' );
60        }
61
62        if ( ! is_string( $this->name ) ) {
63            throw new InvalidArgumentException( 'Module `name` argument must be a string!' );
64        }
65
66        if ( ! $this->has( 'label' ) ) {
67            throw new InvalidArgumentException( 'Module must have a label!' );
68        }
69
70        if ( ! is_string( $this->label ) ) {
71            throw new InvalidArgumentException( 'Module `label` argument must be a string!' );
72        }
73
74        if ( ! $this->has( 'callback' ) ) {
75            throw new InvalidArgumentException( 'Module must have a callback!' );
76        }
77
78        if ( ! is_callable( $this->callback ) ) {
79            throw new InvalidArgumentException( 'Module must have a valid callback!' );
80        }
81
82        if ( ! is_bool( $this->get( 'isActive' ) ) ) {
83            throw new InvalidArgumentException( 'Module `isActive` argument must be a boolean!' );
84        }
85
86        if ( ! is_bool( $this->get( 'isHidden' ) ) ) {
87            throw new InvalidArgumentException( 'Module `isHidden` argument must be a boolean!' );
88        }
89
90        return true;
91
92    }
93
94    /**
95     * Register the module.
96     */
97    public function register() {
98        $this->validate();
99        ModuleRegistry::register( $this );
100    }
101
102}