Completed
Push — master ( 4118ee...672358 )
by Songda
01:48
created

src/Pay.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Yansongda\Pay;
4
5
use Yansongda\Pay\Contracts\GatewayApplicationInterface;
6
use Yansongda\Pay\Exceptions\InvalidGatewayException;
7
use Yansongda\Pay\Gateways\Alipay;
8
use Yansongda\Pay\Gateways\Wechat;
9
use Yansongda\Pay\Listeners\KernelLogSubscriber;
10
use Yansongda\Supports\Config;
11
use Yansongda\Supports\Log;
0 ignored issues
show
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...
12
use Yansongda\Supports\Str;
13
14
/**
15
 * @method static Alipay alipay(array $config) 支付宝
16
 * @method static Wechat wechat(array $config) 微信
17
 */
18
class Pay
19
{
20
    /**
21
     * Config.
22
     *
23
     * @var Config
24
     */
25
    protected $config;
26
27
    /**
28
     * Bootstrap.
29
     *
30
     * @author yansongda <[email protected]>
31
     *
32
     * @param array $config
33
     *
34
     * @throws \Exception
35
     */
36
    public function __construct(array $config)
37
    {
38
        $this->config = new Config($config);
39
40
        $this->registerLogService();
41
        $this->registerEventService();
42
    }
43
44
    /**
45
     * Magic static call.
46
     *
47
     * @author yansongda <[email protected]>
48
     *
49
     * @param string $method
50
     * @param array  $params
51
     *
52
     * @throws InvalidGatewayException
53
     * @throws \Exception
54
     *
55
     * @return GatewayApplicationInterface
56
     */
57
    public static function __callStatic($method, $params): GatewayApplicationInterface
58
    {
59
        $app = new self(...$params);
60
61
        return $app->create($method);
62
    }
63
64
    /**
65
     * Create a instance.
66
     *
67
     * @author yansongda <[email protected]>
68
     *
69
     * @param string $method
70
     *
71
     * @throws InvalidGatewayException
72
     *
73
     * @return GatewayApplicationInterface
74
     */
75
    protected function create($method): GatewayApplicationInterface
76
    {
77
        $gateway = __NAMESPACE__.'\\Gateways\\'.Str::studly($method);
78
79
        if (class_exists($gateway)) {
80
            return self::make($gateway);
81
        }
82
83
        throw new InvalidGatewayException("Gateway [{$method}] Not Exists");
84
    }
85
86
    /**
87
     * Make a gateway.
88
     *
89
     * @author yansongda <[email protected]>
90
     *
91
     * @param string $gateway
92
     *
93
     * @throws InvalidGatewayException
94
     *
95
     * @return GatewayApplicationInterface
96
     */
97
    protected function make($gateway): GatewayApplicationInterface
98
    {
99
        $app = new $gateway($this->config);
100
101
        if ($app instanceof GatewayApplicationInterface) {
102
            return $app;
103
        }
104
105
        throw new InvalidGatewayException("Gateway [{$gateway}] Must Be An Instance Of GatewayApplicationInterface");
106
    }
107
108
    /**
109
     * Register log service.
110
     *
111
     * @author yansongda <[email protected]>
112
     *
113
     * @throws \Exception
114
     */
115
    protected function registerLogService()
116
    {
117
        $logger = Log::createLogger(
118
            $this->config->get('log.file'),
119
            'yansongda.pay',
120
            $this->config->get('log.level', 'warning'),
121
            $this->config->get('log.type', 'daily'),
122
            $this->config->get('log.max_file', 30)
123
        );
124
125
        Log::setLogger($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