Completed
Push — master ( 5ddcc0...9e0ead )
by Carlos
42s
created

Notify::getNotify()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.2163

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 19
ccs 9
cts 11
cp 0.8182
rs 8.8571
cc 6
eloc 12
nc 5
nop 0
crap 6.2163
1
<?php
2
3
/**
4
 * Notify.php.
5
 *
6
 * @author    overtrue <[email protected]>
7
 * @copyright 2015 overtrue <[email protected]>
8
 *
9
 * @link      https://github.com/overtrue
10
 * @link      http://overtrue.me
11
 */
12
namespace EasyWeChat\Payment;
13
14
use EasyWeChat\Core\Exceptions\FaultException;
15
use EasyWeChat\Support\Collection;
16
use EasyWeChat\Support\XML;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Class Notify.
21
 */
22
class Notify
23
{
24
    /**
25
     * Merchant instance.
26
     *
27
     * @var \EasyWeChat\Payment\Merchant
28
     */
29
    protected $merchant;
30
31
    /**
32
     * Request instance.
33
     *
34
     * @var \Symfony\Component\HttpFoundation\Request
35
     */
36
    protected $request;
37
38
    /**
39
     * Payment notify (extract from XML).
40
     *
41
     * @var Collection
42
     */
43
    protected $notify;
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param Merchant $merchant
49
     * @param Request  $request
50
     */
51 6
    public function __construct(Merchant $merchant, Request $request = null)
52
    {
53 6
        $this->merchant = $merchant;
54 6
        $this->request = $request ?: Request::createFromGlobals();
55 6
    }
56
57
    /**
58
     * Validate the request params.
59
     *
60
     * @return bool
61
     */
62 1
    public function isValid()
63
    {
64 1
        $localSign = generate_sign($this->getNotify()->except('sign')->all(), $this->merchant->key, 'md5');
65
66 1
        return $localSign === $this->getNotify()->get('sign');
67
    }
68
69
    /**
70
     * Return the notify body from request.
71
     *
72
     * @return \EasyWeChat\Support\Collection
73
     */
74 4
    public function getNotify()
75
    {
76 4
        if (!empty($this->notify)) {
77 3
            return $this->notify;
78
        }
79
        try {
80 4
            $xml = XML::parse(strval($this->request->getContent()));
81 4
        } 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...
82
            throw new FaultException('Invalid request XML: '.$t->getMessage(), 400);
83 1
        } catch (\Exception $e) {
84 1
            throw new FaultException('Invalid request XML: '.$e->getMessage(), 400);
85
        }
86
87 3
        if (!is_array($xml) || empty($xml)) {
88
            throw new FaultException('Invalid request XML.', 400);
89
        }
90
91 3
        return $this->notify = new Collection($xml);
92
    }
93
}
94