Completed
Push — master ( 56c44b...57c31b )
by Songda
05:33 queued 11s
created

PackerPlugin::getPacker()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
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()));
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