Notify::isValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Payment;
4
5
use EntWeChat\Core\Exceptions\FaultException;
6
use EntWeChat\Support\Collection;
7
use EntWeChat\Support\XML;
8
use Symfony\Component\HttpFoundation\Request;
9
10
/**
11
 * Class Notify.
12
 */
13
class Notify
14
{
15
    /**
16
     * Merchant instance.
17
     *
18
     * @var \EntWeChat\Payment\Merchant
19
     */
20
    protected $merchant;
21
22
    /**
23
     * Request instance.
24
     *
25
     * @var \Symfony\Component\HttpFoundation\Request
26
     */
27
    protected $request;
28
29
    /**
30
     * Payment notify (extract from XML).
31
     *
32
     * @var Collection
33
     */
34
    protected $notify;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param Merchant $merchant
40
     * @param Request  $request
41
     */
42
    public function __construct(Merchant $merchant, Request $request = null)
43
    {
44
        $this->merchant = $merchant;
45
        $this->request = $request ?: Request::createFromGlobals();
46
    }
47
48
    /**
49
     * Validate the request params.
50
     *
51
     * @return bool
52
     */
53
    public function isValid()
54
    {
55
        $localSign = generate_sign($this->getNotify()->except('sign')->all(), $this->merchant->key, 'md5');
56
57
        return $localSign === $this->getNotify()->get('sign');
58
    }
59
60
    /**
61
     * Return the notify body from request.
62
     *
63
     * @throws \EntWeChat\Core\Exceptions\FaultException
64
     *
65
     * @return \EntWeChat\Support\Collection
66
     */
67
    public function getNotify()
68
    {
69
        if (!empty($this->notify)) {
70
            return $this->notify;
71
        }
72
        try {
73
            $xml = XML::parse(strval($this->request->getContent()));
74
        } catch (\Throwable $t) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
75
            throw new FaultException('Invalid request XML: '.$t->getMessage(), 400);
76
        } catch (\Exception $e) {
77
            throw new FaultException('Invalid request XML: '.$e->getMessage(), 400);
78
        }
79
80
        if (!is_array($xml) || empty($xml)) {
81
            throw new FaultException('Invalid request XML.', 400);
82
        }
83
84
        return $this->notify = new Collection($xml);
85
    }
86
}
87