Pay   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 6
dl 0
loc 122
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerEventService() 0 6 1
A __construct() 0 7 1
A __callStatic() 0 6 1
A create() 0 10 2
A make() 0 10 2
A registerLogService() 0 10 1
1
<?php
2
3
namespace Yansongda\Pay;
4
5
use Exception;
6
use Yansongda\Pay\Contracts\GatewayApplicationInterface;
7
use Yansongda\Pay\Exceptions\InvalidGatewayException;
8
use Yansongda\Pay\Gateways\Alipay;
9
use Yansongda\Pay\Gateways\Wechat;
10
use Yansongda\Pay\Listeners\KernelLogSubscriber;
11
use Yansongda\Supports\Config;
12
use Yansongda\Supports\Log;
0 ignored issues
show
Bug introduced by yansongda
This use statement conflicts with another class in this namespace, Yansongda\Pay\Log.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Yansongda\Supports\Logger;
14
use Yansongda\Supports\Str;
15
16
/**
17
 * @method static Alipay alipay(array $config) 支付宝
18
 * @method static Wechat wechat(array $config) 微信
19
 */
20
class Pay
21
{
22
    /**
23
     * Config.
24
     *
25
     * @var Config
26
     */
27
    protected $config;
28
29
    /**
30
     * Bootstrap.
31
     *
32
     * @author yansongda <[email protected]>
33
     *
34
     * @param array $config
35
     *
36
     * @throws Exception
37
     */
38
    public function __construct(array $config)
39
    {
40
        $this->config = new Config($config);
41
42
        $this->registerLogService();
43
        $this->registerEventService();
44
    }
45
46
    /**
47
     * Magic static call.
48
     *
49
     * @author yansongda <[email protected]>
50
     *
51
     * @param string $method
52
     * @param array  $params
53
     *
54
     * @throws InvalidGatewayException
55
     * @throws Exception
56
     *
57
     * @return GatewayApplicationInterface
58
     */
59
    public static function __callStatic($method, $params): GatewayApplicationInterface
60
    {
61
        $app = new self(...$params);
62
63
        return $app->create($method);
64
    }
65
66
    /**
67
     * Create a instance.
68
     *
69
     * @author yansongda <[email protected]>
70
     *
71
     * @param string $method
72
     *
73
     * @throws InvalidGatewayException
74
     *
75
     * @return GatewayApplicationInterface
76
     */
77
    protected function create($method): GatewayApplicationInterface
78
    {
79
        $gateway = __NAMESPACE__.'\\Gateways\\'.Str::studly($method);
80
81
        if (class_exists($gateway)) {
82
            return self::make($gateway);
83
        }
84
85
        throw new InvalidGatewayException("Gateway [{$method}] Not Exists");
86
    }
87
88
    /**
89
     * Make a gateway.
90
     *
91
     * @author yansongda <[email protected]>
92
     *
93
     * @param string $gateway
94
     *
95
     * @throws InvalidGatewayException
96
     *
97
     * @return GatewayApplicationInterface
98
     */
99
    protected function make($gateway): GatewayApplicationInterface
100
    {
101
        $app = new $gateway($this->config);
102
103
        if ($app instanceof GatewayApplicationInterface) {
104
            return $app;
105
        }
106
107
        throw new InvalidGatewayException("Gateway [{$gateway}] Must Be An Instance Of GatewayApplicationInterface");
108
    }
109
110
    /**
111
     * Register log service.
112
     *
113
     * @author yansongda <[email protected]>
114
     *
115
     * @throws Exception
116
     */
117
    protected function registerLogService()
118
    {
119
        $config = $this->config->get('log');
120
        $config['identify'] = 'yansongda.pay';
121
122
        $logger = new Logger();
123
        $logger->setConfig($config);
124
125
        Log::setInstance($logger);
126
    }
127
128
    /**
129
     * Register event service.
130
     *
131
     * @author yansongda <[email protected]>
132
     *
133
     * @return void
134
     */
135
    protected function registerEventService()
136
    {
137
        Events::setDispatcher(Events::createDispatcher());
138
139
        Events::addSubscriber(new KernelLogSubscriber());
140
    }
141
}
142