Completed
Push — master ( b2278f...4f3584 )
by Askupa
02:03
created

Manager::get_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Amarkal\Settings;
4
5
class Manager
6
{
7
    /**
8
     * @var Singleton The reference to *Singleton* instance of this class
9
     */
10
    private static $instance;
11
    
12
    private $pages = array();
13
    
14
    private $child_pages = array();
15
    
16
    /**
17
     * Returns the *Singleton* instance of this class.
18
     *
19
     * @return Singleton The *Singleton* instance.
20
     */
21
    public static function get_instance()
22
    {
23
        if( null === static::$instance ) 
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
24
        {
25
            static::$instance = new static();
0 ignored issues
show
Documentation Bug introduced by
It seems like new static() of type this<Amarkal\Settings\Manager> is incompatible with the declared type object<Amarkal\Settings\Singleton> of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
26
        }
27
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Bug Compatibility introduced by
The expression static::$instance; of type Amarkal\Settings\Manager...rkal\Settings\Singleton adds the type Amarkal\Settings\Manager to the return on line 27 which is incompatible with the return type documented by Amarkal\Settings\Manager::get_instance of type Amarkal\Settings\Singleton.
Loading history...
28
    }
29
    
30
    /**
31
     * Add a page to the admin menu.
32
     * 
33
     * @param array $args
34
     * @throws \RuntimeException
35
     */
36
    public function add_page($args)
37
    {
38
        $slug = $args['slug'];
39
        if(array_key_exists($slug,$this->pages))
40
        {
41
            throw new \RuntimeException("A page with slug '$slug' has already been registered");
42
        }
43
        $this->pages[$slug] = new Page($args);
44
    }
45
    
46
    /**
47
     * Get a page from the set of registered pages.
48
     * 
49
     * @param string $slug
50
     * @return Page
51
     * @throws \RuntimeException If no page was found for the given slug
52
     */
53
    public function get_page($slug)
54
    {
55
        if(!array_key_exists($slug,$this->pages))
56
        {
57
            throw new \RuntimeException("The page '$slug' does not exist");
58
        }
59
        return $this->pages[$slug];
60
    }
61
    
62
    /**
63
     * Add a child setting page.
64
     * 
65
     * @param array $args
66
     */
67
    public function add_child_page($args)
68
    {
69
        $parent_slug = $args['parent_slug'];
70
        if(!array_key_exists($parent_slug, $this->child_pages))
71
        {
72
            $this->child_pages[$parent_slug] = array();
73
        }
74
        $this->child_pages[$parent_slug][$args['slug']] = new ChildPage($args);
75
    }
76
    
77
    /**
78
     * Get a child page from the set of registered child pages.
79
     * 
80
     * @param string $slug
81
     * @param string $parent_slug
82
     * @return ChildPage
83
     * @throws \RuntimeException If no child page was found for the given slug/parent_slug
84
     */
85
    public function get_child_page($slug, $parent_slug)
86
    {
87
        if(!array_key_exists($parent_slug, $this->child_pages) ||
88
           !array_key_exists($slug, $this->child_pages[$parent_slug]))
89
        {
90
            throw new \RuntimeException("The child page '$slug' does not exist for the parent '$parent_slug'");
91
        }
92
        return $this->child_pages[$parent_slug][$slug];
93
    }
94
    
95
    /**
96
     * Register styles & scripts to be enqueued by settings child pages
97
     */
98
    public function register_scripts()
99
    {
100
        \wp_register_style('amarkal-settings', \Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/css/dist/amarkal-settings.min.css'));
101
        \wp_register_script('amarkal-settings',\Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/js/dist/amarkal-settings.min.js'),array('amarkal-ui'));
102
    }
103
    
104
    /**
105
     * Private constructor to prevent instantiation
106
     */
107
    private function __construct() 
108
    {
109
        $this->init();
110
    }
111
    
112
    /**
113
     * Register scripts and initiate the request handler.
114
     */
115
    private function init()
116
    {
117
        \add_action('admin_init',array($this,'register_scripts'));
118
        
119
        $rh = RequestHandler::get_instance();
120
        $rh->init();
121
    }
122
}