RequestHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_instance() 0 8 2
A init() 0 6 1
A action_save() 0 7 1
A action_reset_all() 0 7 1
A action_reset_section() 0 8 1
A get_request_settings_page() 0 7 1
A set_request_data() 0 16 3
1
<?php
2
3
namespace Amarkal\Settings;
4
5
class RequestHandler
6
{
7
    /**
8
     * @var Singleton The reference to *Singleton* instance of this class
9
     */
10
    private static $instance;
11
    
12
    private $request_data = array();
13
    
14
    const NONCE_ACTION = 'amarkal_settings';
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\RequestHandler> 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\Request...rkal\Settings\Singleton adds the type Amarkal\Settings\RequestHandler to the return on line 27 which is incompatible with the return type documented by Amarkal\Settings\RequestHandler::get_instance of type Amarkal\Settings\Singleton.
Loading history...
28
    }
29
    
30
    public function init()
31
    {
32
        \add_action('wp_ajax_amarkal_settings_save', array( $this, 'action_save'));
33
        \add_action('wp_ajax_amarkal_settings_reset_all', array( $this, 'action_reset_all'));
34
        \add_action('wp_ajax_amarkal_settings_reset_section', array( $this, 'action_reset_section'));
35
    }
36
    
37
    public function action_save()
38
    {
39
        $this->set_request_data();
40
        $settings_page  = $this->get_request_settings_page();
41
        
42
        \wp_send_json($settings_page->update($this->request_data));
43
    }
44
    
45
    public function action_reset_all()
46
    {
47
        $this->set_request_data();
48
        $settings_page = $this->get_request_settings_page();
49
        
50
        \wp_send_json($settings_page->reset());
51
    }
52
53
    public function action_reset_section()
54
    {
55
        $this->set_request_data();
56
        $settings_page = $this->get_request_settings_page();
57
        $section       = $this->request_data['_amarkal_settings_section'];
58
        
59
        \wp_send_json($settings_page->reset_section($section));
60
    }
61
    
62
    private function get_request_settings_page()
63
    {
64
        $manager    = Manager::get_instance();
65
        $slug       = $this->request_data['_amarkal_settings_slug'];
66
        
67
        return $manager->get_settings_page($slug);
68
    }
69
    
70
    private function set_request_data()
71
    {
72
        $this->request_data = filter_input(INPUT_POST,'data',FILTER_DEFAULT,FILTER_REQUIRE_ARRAY);
0 ignored issues
show
Documentation Bug introduced by
It seems like filter_input(INPUT_POST,..., FILTER_REQUIRE_ARRAY) of type * is incompatible with the declared type array of property $request_data.

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...
73
        $nonce = $this->request_data['_amarkal_settings_nonce'];
74
75
        if( !isset( $nonce ) ||
76
            !\wp_verify_nonce($nonce, self::NONCE_ACTION) ) 
77
        {
78
            \wp_send_json(array(
79
                'values' => array(),
80
                'errors' => array(
81
                    'Your nonce did not verify'
82
                )
83
            ));
84
        }
85
    }
86
}