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

Ajax::getToken()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 0
dl 0
loc 18
rs 9.9332
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\Provider\TokenResponse;
9
10
use Exception;
11
use Hryvinskyi\Base\Helper\Json;
12
use Hryvinskyi\InvisibleCaptcha\Model\Provider\TokenResponseInterface;
13
use Magento\Framework\App\RequestInterface;
14
use Magento\Framework\Message\ManagerInterface;
15
16
class Ajax implements TokenResponseInterface
17
{
18
    /**
19
     * @var RequestInterface
20
     */
21
    private $request;
22
23
    /**
24
     * @var ManagerInterface
25
     */
26
    private $messageManager;
27
28
    /**
29
     * Ajax constructor.
30
     *
31
     * @param RequestInterface $request
32
     * @param ManagerInterface $messageManager
33
     */
34
    public function __construct(
35
        RequestInterface $request,
36
        ManagerInterface $messageManager
37
    ) {
38
        $this->request = $request;
39
        $this->messageManager = $messageManager;
40
    }
41
42
    /**
43
     * Return token
44
     *
45
     * @return string|null
46
     */
47
    public function getToken(): ?string
48
    {
49
        $token = null;
50
        $content = $this->request->getContent();
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on Magento\Framework\App\RequestInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Magento\Framework\App\Console\Request. Are you sure you never get one of those? ( Ignorable by Annotation )

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

50
        /** @scrutinizer ignore-call */ 
51
        $content = $this->request->getContent();
Loading history...
51
52
        if ($content) {
53
            try {
54
                $params = Json::decode($content);
55
56
                if (isset($params['hryvinskyi_invisible_token'])) {
57
                    $token = $params['hryvinskyi_invisible_token'];
58
                }
59
            } catch (Exception $e) {
60
                $this->messageManager->addErrorMessage(__('Not found invisible captcha token'));
61
            }
62
        }
63
64
        return $token;
65
    }
66
}
67