Completed
Pull Request — staging (#840)
by
unknown
16:43
created

Recaptcha   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 1
A get_context() 0 3 1
A get_script_params() 0 4 1
A __get() 0 9 2
A load_assets() 0 12 1
1
<?php
2
/**
3
 * YIKES Inc. Easy Forms.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\Recaptcha;
11
12
use YIKES\EasyForms\Assets\AssetsAware;
13
use YIKES\EasyForms\Assets\AssetsAwareness;
14
use YIKES\EasyForms\Assets\ScriptAsset;
15
use YIKES\EasyForms\Service;
16
use YIKES\EasyForms\View\View;
17
use YIKES\EasyForms\Model\Recaptcha as RecaptchaModel;
18
19
/**
20
 * Class Recaptcha
21
 *
22
 * @since %VERSION%
23
 */
24
final class Recaptcha implements Service, AssetsAware {
25
26
    use AssetsAwareness;
27
28
    const VIEW_URI        = 'views/recaptcha-box';
29
    const JS_HANDLE       = 'google-recaptcha-js';
30
    const JS_URI          = 'https://www.google.com/recaptcha/api.js';
31
    const JS_DEPENDENCIES = [ 'jquery', 'form-submission-helpers' ];
32
    const JS_VERSION      = '1.0.0';
33
34
    public function register() {
35
        $this->register_assets();
36
37
        add_action( 'easy_forms_do_recaptcha_box', function( $view ) {
38
            $this->enqueue_assets();
39
            echo $view->render_partial( static::VIEW_URI ); // phpcs:ignore WordPress.Security.EscapeOutput
40
        } );
41
    }
42
43
     /**
44
	 * Get the context to pass onto the view.
45
	 *
46
	 * Override to provide data to the view.
47
	 *
48
	 * @since %VERSION%
49
	 *
50
	 * @return array Context to pass onto view.
51
	 */
52
	protected function get_context() {
53
		return $this->recaptcha['recaptcha_options'];
0 ignored issues
show
Documentation introduced by
The property recaptcha does not exist on object<YIKES\EasyForms\Recaptcha\Recaptcha>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
    }
55
    
56
    private function get_script_params() {
57
        $recaptcha_options = ( new RecaptchaModel() )->setup();
58
        return $recaptcha_options['script_params'];
59
    }
60
61
    public function __get( $name ) {
62
		switch ( $name ) {
63
			case 'script_params':
64
				return $this->get_script_params();
65
66
			default:
67
				return null;
68
		}
69
    }
70
71
    /**
72
	 * Load asset objects for use.
73
	 *
74
	 * @since %SINCE%
75
	 */
76
	protected function load_assets() {  
77
		$this->assets= [
78
			new ScriptAsset(
79
                self::JS_HANDLE,
80
                self::JS_URI . $this->script_params,
0 ignored issues
show
Documentation introduced by
The property script_params does not exist on object<YIKES\EasyForms\Recaptcha\Recaptcha>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
81
                self::JS_DEPENDENCIES,
82
                self::JS_VERSION,
83
                ScriptAsset::ENQUEUE_HEADER,
84
                true
85
            ),
86
		];
87
    }
88
}