Passed
Push — master ( c11190...b979a3 )
by ma
03:25
created

BaseProvider::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace tinymeng\Chinaums\Provider;
4
5
use tinymeng\Chinaums\Exception\TException;
6
use tinymeng\Chinaums\Interfaces\ProviderInterface;
7
use tinymeng\Chinaums\Tools\Str;
8
use tinymeng\Chinaums\Tools\Verify;
9
10
/**
11
 * BaseProvider
12
 */
13
class BaseProvider implements ProviderInterface
14
{
15
    protected $config = [];
16
    public function __construct($config)
17
    {
18
        $this->config = $config;
19
    }
20
21
    /**
22
     * @param string $shortcut
23
     * @param array $params
24
     * @return mixed
25
     */
26
    public function __call(string $shortcut, array $params=[])
27
    {
28
        $class = str_replace('Provider','Service',static::class).'\\' . Str::studly($shortcut);
29
        if(!class_exists($class)){
30
            throw new TException("Chinaums:{$class}类不存在");
31
        }
32
33
        return new $class($this->config,$params);
34
    }
35
36
    /**
37
     * @param $contents
38
     * @return bool
39
     */
40
    public function callback($contents)
41
    {
42
        $params = array_map(function ($value) {
43
            return urldecode($value);
44
        }, $contents);
45
        $md5Key = $this->config['md5key'];
46
        $sign = Verify::makeSign($md5Key, $params);
47
        $notifySign = array_key_exists('sign', $params) ? $params['sign'] : '';
48
        if (strcmp($sign, $notifySign) == 0) {
49
            return true;
50
        }
51
        return false;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function success()
58
    {
59
        return 'SUCCESS';
60
    }
61
}
62