CaptchaNewsletter::getJsLayout()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 16
rs 9.2222
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
declare(strict_types=1);
9
10
namespace Hryvinskyi\InvisibleCaptcha\Block;
11
12
use Hryvinskyi\Base\Helper\Json;
13
use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend;
14
use Hryvinskyi\InvisibleCaptcha\Helper\Config\General;
15
use Hryvinskyi\InvisibleCaptcha\Model\LayoutSettings;
16
use Magento\Framework\View\Element\Template;
17
use Magento\Framework\View\Element\Template\Context;
18
19
class CaptchaNewsletter extends Captcha
20
{
21
    /**
22
     * @var General
23
     */
24
    private $generalConfig;
25
26
    /**
27
     * @var Frontend
28
     */
29
    private $frontendConfig;
30
31
    /**
32
     * @var LayoutSettings
33
     */
34
    private $layoutSettings;
35
36
    /**
37
     * Captcha constructor.
38
     *
39
     * @param Context $context
40
     * @param General $generalConfig
41
     * @param Frontend $frontendConfig
42
     * @param LayoutSettings $layoutSettings
43
     * @param array $data
44
     */
45
    public function __construct(
46
        Context $context,
47
        General $generalConfig,
48
        Frontend $frontendConfig,
49
        LayoutSettings $layoutSettings,
50
        array $data = []
51
    ) {
52
        parent::__construct($context, $generalConfig, $frontendConfig, $layoutSettings, $data);
53
54
        $this->generalConfig = $generalConfig;
55
        $this->frontendConfig = $frontendConfig;
56
        $this->layoutSettings = $layoutSettings;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getJsLayout()
63
    {
64
        $layout = $this->jsLayout;
65
66
        if ($this->frontendConfig->hasEnabled() && $this->isModuleOn()) {
67
            $layout['components']['invisible-captcha-newsletter']['config'] = $this->layoutSettings->getCaptchaSettings();
68
        }
69
70
        if (
71
            (!$this->frontendConfig->hasEnabled() || !$this->isModuleOn())
72
            && isset($layout['components']['invisible-captcha-newsletter'])
73
        ) {
74
            unset($layout['components']['invisible-captcha-newsletter']);
75
        }
76
77
        return Json::encode($layout);
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function isModuleOn(): bool
84
    {
85
        return $this->generalConfig->hasEnabled() || $this->frontendConfig->hasEnabledNewsletter();
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function toHtml()
92
    {
93
        if (!$this->isModuleOn()) {
94
            return '';
95
        }
96
97
        return parent::toHtml();
98
    }
99
}
100