Passed
Pull Request — master (#48)
by
unknown
02:31
created

WC_Pagantis_Notices::check_plugin_settings()   F

Complexity

Conditions 17
Paths 1025

Size

Total Lines 86
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 47
c 1
b 0
f 0
nc 1025
nop 0
dl 0
loc 86
rs 1.0499

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
class WC_Pagantis_Notices
5
{
6
7
    //TODO https://wordpress.stackexchange.com/questions/242705/how-to-stop-showing-admin-notice-after-close-button-has-been-clicked
8
9
    /**
10
     * WC_Pagantis_Gateway for WooCommerce settings.
11
     *
12
     * @var $settings
0 ignored issues
show
Documentation Bug introduced by
The doc comment $settings at position 0 could not be parsed: Unknown type name '$settings' at position 0 in $settings.
Loading history...
13
     */
14
    public $settings = array();
15
16
17
    /**
18
     * Customizable configuration options
19
     *
20
     * @var array $extraConfig
21
     */
22
23
    private $extraConfig;
24
25
26
27
    /**
28
     * Array of allowed currencies with Pagantis
29
     *
30
     * @var array $allowed_currencies
31
     */
32
    private $allowed_currencies;
33
    /**
34
     * WC_Pagantis_Notices constructor.
35
     */
36
    public function __construct()
37
    {
38
39
        require_once dirname(__FILE__) . '/../includes/class-wc-pagantis-config.php';
40
        require_once dirname(__FILE__) . '/../includes/functions.php';
41
        $this->settings           = get_option('woocommerce_pagantis_settings');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->settings           = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
42
        $this->extraConfig        = WC_Pagantis_Config::getExtraConfig();
43
        $this->allowed_currencies = array('EUR');
44
        add_action('admin_notices', array($this, 'check_plugin_settings'));
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        add_action('admin_notices', array($this, 'check_plugin_settings'));
Loading history...
45
    }
46
47
    /**
48
     * Check dependencies.
49
     *
50
     * @hook   admin_notices
51
     * @throws Exception
52
     */
53
    public function check_plugin_settings()
54
    {
55
        if (!pg_wc_is_screen_correct()) {
56
            return;
57
        }
58
        if ($this->settings['enabled'] !== 'yes') {
59
            WC_Admin_Notices::add_custom_notice(
0 ignored issues
show
Bug introduced by
The type WC_Admin_Notices was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
                PAGANTIS_PLUGIN_ID,
61
                sprintf(
62
                // translators: 1:  URL to WP plugin page.
63
                    __('Activate Pagantis to start offering comfortable payments in installments to your clients. <a class="button button-primary" href="%1$s">Activate Pagantis now!</a>', 'pagantis'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
                    /** @scrutinizer ignore-call */ 
64
                    __('Activate Pagantis to start offering comfortable payments in installments to your clients. <a class="button button-primary" href="%1$s">Activate Pagantis now!</a>', 'pagantis'),
Loading history...
64
                    pg_wc_get_pagantis_admin_url()
65
                )
66
            );
67
        }
68
69
        if ($this->settings['pagantis_public_key'] === '' xor $this->settings['pagantis_private_key'] === ''
70
                                                              || $this->settings['enabled'] === 'yes'
71
        ) {
72
            WC_Admin_Notices::add_custom_notice(
73
                PAGANTIS_PLUGIN_ID.'keys_setup',
74
                sprintf(
75
                // translators: 1:  URL to WP plugin page.
76
                    __('Set your Pagantis merchant keys to start offering comfortable payments in installments  <a class="button button-primary" href="%1$s">Go to keys setup</a></p>', 'pagantis'),
77
                    pg_wc_get_pagantis_admin_url()
78
                )
79
            );
80
        }
81
82
83
        if ($this->settings['pagantis_public_key'] === '' xor $this->settings['pagantis_private_key'] === '') {
84
            WC_Admin_Notices::add_custom_notice(
85
                PAGANTIS_PLUGIN_ID,
86
                sprintf(
87
                // translators: 1:  URL to WP plugin page.
88
                    __('Check your Pagantis merchant keys to start offering Pagantis as a payment method .  <a class="button button-primary" href="%1$s">Go to keys setup</a>', 'pagantis'),
89
                    pg_wc_get_pagantis_admin_url()
90
                )
91
            );
92
        }
93
94
        if ($this->settings['pagantis_public_key'] === '' || $this->settings['pagantis_private_key'] === '') {
95
            WC_Admin_Notices::add_custom_notice(
96
                PAGANTIS_PLUGIN_ID.'keys_error',
97
                sprintf(
98
                // translators: 1:  URL to WP plugin page.
99
                    __('Set your Pagantis merchant Api keys to start offering comfortable payments in installments.  <a class="button button-primary" href="%1$s">Go to keys setup.</a>', 'pagantis'),
100
                    pg_wc_get_pagantis_admin_url()
101
                )
102
            );
103
        }
104
105
        if ($this->settings['pagantis_public_key'] !== '' xor $this->settings['pagantis_private_key'] !== '') {
106
            WC_Admin_Notices::remove_notice(PAGANTIS_PLUGIN_ID.'keys_setup');
107
        }
108
109
        if ($this->settings['pagantis_public_key'] !== '' || $this->settings['pagantis_private_key'] !== '') {
110
            WC_Admin_Notices::remove_notice(PAGANTIS_PLUGIN_ID.'keys_error');
111
        }
112
113
        if (! in_array(get_woocommerce_currency(), $this->allowed_currencies, true)) {
0 ignored issues
show
Bug introduced by
The function get_woocommerce_currency was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
        if (! in_array(/** @scrutinizer ignore-call */ get_woocommerce_currency(), $this->allowed_currencies, true)) {
Loading history...
114
            WC_Admin_Settings::add_error(__('Error: Pagantis only can be used in Euros.', 'pagantis'));
0 ignored issues
show
Bug introduced by
The type WC_Admin_Settings was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
115
            $this->settings['enabled'] = 'no';
116
        }
117
118
        if ($this->extraConfig['PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'] < '2'
119
            || $this->extraConfig['PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'] > '12'
120
        ) {
121
            $this->settings['enabled'] = 'no';
122
123
            WC_Admin_Settings::add_error(__(
124
                'Error: Pagantis can be used up to 12 installments please contact your account manager',
125
                'pagantis'
126
            ));
127
        }
128
129
        if ($this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'] < '2'
130
            || $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'] > '12'
131
        ) {
132
            WC_Admin_Settings::add_error(__(
133
                'Error: Pagantis can be used from 2 installments please contact your account manager',
134
                'pagantis'
135
            ));
136
        }
137
        if ($this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT'] < 0) {
138
            WC_Admin_Settings::add_error(__('Error: Pagantis can not be used for free products', 'pagantis'));
139
        }
140
    }
141
}
142