Notify::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace YEntWeChat\Payment;
4
5
use YEntWeChat\Core\Exceptions\FaultException;
6
use YEntWeChat\Support\Collection;
7
use YEntWeChat\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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $merchant of type object<YEntWeChat\Payment\Merchant> is incompatible with the declared type object<EntWeChat\Payment\Merchant> of property $merchant.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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