Passed
Push — master ( 4f79b1...15f90b )
by wannanbigpig
05:41 queued 10s
created

Handle::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the wannanbigpig/alipay-sdk-php.
4
 *
5
 * (c) wannanbigpig <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace EasyAlipay\Kernel\Notify;
12
13
use Closure;
14
use EasyAlipay\Kernel\Exceptions\InvalidSignException;
15
use EasyAlipay\Kernel\ServiceContainer;
16
use EasyAlipay\Kernel\Traits\Helpers;
17
use Symfony\Component\HttpFoundation\Response;
18
use WannanBigPig\Supports\Collection;
19
20
/**
21
 * Class Handle
22
 *
23
 * @author   liuml  <[email protected]>
24
 * @DateTime 2019-07-23  17:24
25
 */
26
class Handle
27
{
28
    use Helpers;
29
30
    /**
31
     * @var string
32
     */
33
    protected $fail = 'fail';
34
35
    /**
36
     * @var string
37
     */
38
    protected $success = 'success';
39
40
    /**
41
     * @var array
42
     */
43
    protected $data;
44
45
    /**
46
     * @var string
47
     */
48
    protected $response;
49
50
    /**
51
     * @var \EasyAlipay\Payment\Application
52
     */
53
    protected $app;
54
55
    /**
56
     * Handle constructor.
57
     *
58
     * @param \EasyAlipay\Kernel\ServiceContainer $app
59
     */
60 14
    public function __construct(ServiceContainer $app)
61
    {
62 14
        $this->app = $app;
1 ignored issue
show
Documentation Bug introduced by
$app is of type EasyAlipay\Kernel\ServiceContainer, but the property $app was declared to be of type EasyAlipay\Payment\Application. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
63 14
    }
64
65
    /**
66
     * run.
67
     *
68
     * @param \Closure $closure
69
     *
70
     * @return \Symfony\Component\HttpFoundation\Response
71
     */
72 14
    public function run(Closure $closure): Response
73
    {
74
        try {
75 14
            $response = \call_user_func($closure, $this->getMessage(), [$this, 'response']);
76 12
        } catch (\Exception $e) {
77 12
            $this->app['logger']->error('Alipay asynchronous notification fatal error:'.$e->getMessage());
78 12
            $response = $this->response(false);
79
        }
80
81 14
        return Response::create($response);
82
    }
83
84
    /**
85
     * response.
86
     *
87
     * @param bool $result
88
     *
89
     * @return string
90
     */
91 14
    public function response(bool $result = true): string
92
    {
93 14
        if ($result === true) {
94 2
            return $this->success;
95
        }
96
97 12
        return $this->fail;
98
    }
99
100
    /**
101
     * getMessage.
102
     *
103
     * @return \WannanBigPig\Supports\Collection
104
     *
105
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
106
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
107
     */
108 14
    public function getMessage(): Collection
109
    {
110 14
        $signData = $request = $this->app['request']->request->all();
111 14
        unset($signData['sign'], $signData['sign_type']);
112 14
        if ($this->verify($this->getSignContentUrlencode($signData), $request['sign'], $request['sign_type'])) {
113 2
            return new Collection($request);
114
        }
115
116 2
        throw new InvalidSignException('Asynchronous notification signature verification failed');
117
    }
118
}
119