Captcha::execute()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 9.6111
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\Observer;
9
10
use Hryvinskyi\InvisibleCaptcha\Helper\Config\General;
11
use Hryvinskyi\InvisibleCaptcha\Model\CaptchaInterface;
12
use Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha\VerifyReCaptcha;
13
use Magento\Framework\App\Action\Action;
14
use Magento\Framework\Event\Observer;
15
use Magento\Framework\Event\ObserverInterface;
16
17
class Captcha implements ObserverInterface
18
{
19
    /**
20
     * @var General
21
     */
22
    private $config;
23
24
    /**
25
     * @var VerifyReCaptcha
26
     */
27
    private $verifyReCaptcha;
28
29
    /**
30
     * @var CaptchaInterface
31
     */
32
    private $provider;
33
34
    /**
35
     * Action constructor.
36
     *
37
     * @param General $config
38
     * @param VerifyReCaptcha $verifyReCaptcha
39
     * @param CaptchaInterface $provider
40
     */
41
    public function __construct(
42
        General $config,
43
        VerifyReCaptcha $verifyReCaptcha,
44
        CaptchaInterface $provider
45
    ) {
46
        $this->config = $config;
47
        $this->verifyReCaptcha = $verifyReCaptcha;
48
        $this->provider = $provider;
49
    }
50
51
    /**
52
     * @param Observer $observer
53
     */
54
    public function execute(Observer $observer)
55
    {
56
        if ($this->provider->isEnabled() && $_SERVER['REQUEST_METHOD'] !== 'GET') {
57
            $verifyReCaptcha = $this->verifyReCaptcha
58
                ->setSecret($this->config->getSecretKey())
59
                ->setExpectedAction($this->provider->getAction())
60
                ->setScoreThreshold($this->provider->getScoreThreshold())
61
                ->verify($this->provider->getToken());
62
63
            if ($verifyReCaptcha->isSuccess() === false) {
64
                /** @var Action|null $controller */
65
                $controller = $observer->getData('controller_action');
66
                $this->provider->getFailure()->execute($verifyReCaptcha, $controller ? $controller->getResponse() : null);
67
            }
68
        }
69
    }
70
}
71