Passed
Branch master (fbf0a6)
by Volodymyr
04:04
created

AbstractCheckEnabledVerify::getBackendConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2019. Volodymyr Hryvinskyi.  All rights reserved.
4
 * @author: <mailto:[email protected]>
5
 * @github: <https://github.com/hryvinskyi>
6
 */
7
8
namespace Hryvinskyi\InvisibleCaptcha\Model;
9
10
use Hryvinskyi\InvisibleCaptcha\Helper\Config\Backend;
11
use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend;
12
use Hryvinskyi\InvisibleCaptcha\Helper\Config\General;
13
use InvalidArgumentException;
14
use Magento\Framework\App\Area;
15
16
abstract class AbstractCheckEnabledVerify implements CheckEnabledVerifyInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $area;
22
23
    /**
24
     * @var General
25
     */
26
    private $generalConfig;
27
28
    /**
29
     * @var Frontend
30
     */
31
    private $frontendConfig;
32
33
    /**
34
     * @var Backend
35
     */
36
    private $backendConfig;
37
38
    /**
39
     * AbstractCheckEnableVerify constructor.
40
     *
41
     * @param General $generalConfig
42
     * @param Frontend $frontendConfig
43
     * @param Backend $backendConfig
44
     * @param string $area
45
     */
46
    public function __construct(
47
        General $generalConfig,
48
        Frontend $frontendConfig,
49
        Backend $backendConfig,
50
        string $area
51
    ) {
52
        $this->generalConfig = $generalConfig;
53
        $this->frontendConfig = $frontendConfig;
54
        $this->backendConfig = $backendConfig;
55
        $this->area = $area;
56
57
        if (!in_array($area, [Area::AREA_FRONTEND, Area::AREA_ADMINHTML])) {
58
            throw new InvalidArgumentException('Area parameter must be one of frontend or adminhtml');
59
        }
60
    }
61
62
    /**
63
     * @return General
64
     */
65
    public function getGeneralConfig(): General
66
    {
67
        return $this->generalConfig;
68
    }
69
70
    /**
71
     * @return Frontend
72
     */
73
    public function getFrontendConfig(): Frontend
74
    {
75
        return $this->frontendConfig;
76
    }
77
78
    /**
79
     * @return Backend
80
     */
81
    public function getBackendConfig(): Backend
82
    {
83
        return $this->backendConfig;
84
    }
85
86
    /**
87
     * Return true if area is configured to be active
88
     *
89
     * @return bool
90
     */
91
    public function checkArea(): bool
92
    {
93
        $return = false;
94
95
        if ($this->getGeneralConfig()->hasEnabled() === false) {
96
            return $return;
97
        }
98
99
        switch ($this->area) {
100
            case Area::AREA_FRONTEND:
101
                $return = $this->getFrontendConfig()->hasEnabled();
102
                break;
103
104
            case Area::AREA_ADMINHTML:
105
                $return = $this->getBackendConfig()->hasEnabled();
106
                break;
107
        }
108
109
        return $return;
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    public function verify(): bool
116
    {
117
        return $this->checkArea();
118
    }
119
}
120