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

Notify   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.89%

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 9
c 4
b 2
f 1
lcom 1
cbo 5
dl 0
loc 72
ccs 16
cts 18
cp 0.8889
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A isValid() 0 6 1
B getNotify() 0 19 6
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