AddFormAdditionalInfoIfMissing   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 64
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeToHtml() 0 38 4
A __construct() 0 6 1
1
<?php
2
/**
3
 * Copyright (c) 2020. 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\Plugin\Block\ContactForm;
11
12
use Hryvinskyi\InvisibleCaptcha\Model\Verify\Contact;
13
use Magento\Framework\Pricing\Render;
14
use Magento\Contact\Block\ContactForm as Subject;
15
use Psr\Log\LoggerInterface;
16
17
/**
18
 * Class AddFormAdditionalInfoIfMissing
19
 */
20
class AddFormAdditionalInfoIfMissing
21
{
22
    /**
23
     * @var LoggerInterface
24
     */
25
    private $logger;
26
27
    /**
28
     * @var Contact
29
     */
30
    private $verifyContact;
31
32
    /**
33
     * AddFormAdditionalInfoIfMissing constructor.
34
     *
35
     * @param Contact $verifyContact
36
     * @param LoggerInterface $logger
37
     */
38
    public function __construct(
39
        Contact $verifyContact,
40
        LoggerInterface $logger
41
    ) {
42
        $this->verifyContact = $verifyContact;
43
        $this->logger = $logger;
44
    }
45
46
    public function beforeToHtml(
47
        Subject $subject
48
    ) {
49
        try {
50
            $childrens = $subject->getChildNames();
51
52
            if (in_array('form.additional.info', $childrens) === false && $this->verifyContact->verify() === true) {
53
                $subject->getLayout()->addContainer(
54
                    'form.additional.info',
55
                    'Form Additional Info',
56
                    [],
57
                    $subject->getNameInLayout(),
58
                    'form.additional.info'
59
                );
60
61
62
                $block = $subject->getLayout()->createBlock(
63
                    \Hryvinskyi\InvisibleCaptcha\Block\Captcha::class,
64
                    $subject->getNameInLayout() . '.invisible.recaptcha',
65
                    [
66
                        'jsLayout' => [
67
                            'components' => [
68
                                'invisible-captcha' => [
69
                                    'component' =>'Hryvinskyi_InvisibleCaptcha/js/invisible-captcha',
70
                                    'action' => 'contact',
71
                                    'captchaId' => 'contact'
72
                                ]
73
                            ]
74
                        ],
75
                        'data' => [
76
                            'template' => 'Hryvinskyi_InvisibleCaptcha::captcha.phtml'
77
                        ]
78
                    ]
79
                );
80
                $subject->setChild('form.additional.info', $block);
81
            }
82
        } catch (\Throwable $exception) {
83
            $this->logger->critical($exception->getMessage(), $exception->getTrace());
84
        }
85
    }
86
}
87