1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yansongda\Pay\Plugin; |
||
6 | |||
7 | use Closure; |
||
8 | use Yansongda\Pay\Contract\PackerInterface; |
||
9 | use Yansongda\Pay\Contract\PluginInterface; |
||
10 | use Yansongda\Pay\Exception\InvalidConfigException; |
||
11 | use Yansongda\Pay\Pay; |
||
12 | use Yansongda\Pay\Rocket; |
||
13 | |||
14 | class PackerPlugin implements PluginInterface |
||
15 | { |
||
16 | /** |
||
17 | * @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
||
18 | * @throws \Yansongda\Pay\Exception\ContainerException |
||
19 | * @throws \Yansongda\Pay\Exception\ContainerDependencyException |
||
20 | * @throws \Yansongda\Pay\Exception\InvalidConfigException |
||
21 | */ |
||
22 | public function assembly(Rocket $rocket, Closure $next): Rocket |
||
23 | { |
||
24 | /* @var Rocket $rocket */ |
||
25 | $rocket = $next($rocket); |
||
26 | |||
27 | $packer = $this->getPacker($rocket); |
||
28 | |||
29 | return $rocket->setDestination($packer->unpack($rocket->getDestination())); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @throws \Yansongda\Pay\Exception\ContainerDependencyException |
||
34 | * @throws \Yansongda\Pay\Exception\ContainerException |
||
35 | * @throws \Yansongda\Pay\Exception\InvalidConfigException |
||
36 | * @throws \Yansongda\Pay\Exception\ServiceNotFoundException |
||
37 | */ |
||
38 | protected function getPacker(Rocket $rocket): PackerInterface |
||
39 | { |
||
40 | $packer = Pay::get($rocket->getDirection() ?? PackerInterface::class); |
||
41 | |||
42 | $packer = is_string($packer) ? Pay::get($packer) : $packer; |
||
43 | |||
44 | if (!($packer instanceof PackerInterface)) { |
||
45 | throw new InvalidConfigException(InvalidConfigException::INVALID_PACKER); |
||
46 | } |
||
47 | |||
48 | return $packer; |
||
49 | } |
||
50 | } |
||
51 |