Passed
Pull Request — master (#774)
by Songda
01:53
created

ParserPlugin::getDirection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 11
rs 10
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 Psr\Http\Message\ResponseInterface;
9
use Yansongda\Pay\Contract\DirectionInterface;
10
use Yansongda\Pay\Contract\PackerInterface;
11
use Yansongda\Pay\Contract\PluginInterface;
12
use Yansongda\Pay\Exception\ContainerException;
13
use Yansongda\Pay\Exception\Exception;
14
use Yansongda\Pay\Exception\InvalidConfigException;
15
use Yansongda\Pay\Exception\ServiceNotFoundException;
16
use Yansongda\Pay\Pay;
17
use Yansongda\Pay\Rocket;
18
19
class ParserPlugin implements PluginInterface
20
{
21
    /**
22
     * @throws ServiceNotFoundException
23
     * @throws ContainerException
24
     * @throws InvalidConfigException
25
     */
26
    public function assembly(Rocket $rocket, Closure $next): Rocket
27
    {
28
        /* @var Rocket $rocket */
29
        $rocket = $next($rocket);
30
31
        /* @var ResponseInterface $response */
32
        $response = $rocket->getDestination();
33
34
        return $rocket->setDestination(
35
            $this->getDirection($rocket)->parse($this->getPacker($rocket), $response)
36
        );
37
    }
38
39
    /**
40
     * @throws ContainerException
41
     * @throws InvalidConfigException
42
     * @throws ServiceNotFoundException
43
     */
44
    protected function getDirection(Rocket $rocket): DirectionInterface
45
    {
46
        $packer = Pay::get($rocket->getDirection());
47
48
        $packer = is_string($packer) ? Pay::get($packer) : $packer;
49
50
        if (!$packer instanceof DirectionInterface) {
51
            throw new InvalidConfigException(Exception::INVALID_PARSER);
52
        }
53
54
        return $packer;
55
    }
56
57
    /**
58
     * @throws ContainerException
59
     * @throws InvalidConfigException
60
     * @throws ServiceNotFoundException
61
     */
62
    protected function getPacker(Rocket $rocket): PackerInterface
63
    {
64
        $packer = Pay::get($rocket->getPacker());
65
66
        $packer = is_string($packer) ? Pay::get($packer) : $packer;
67
68
        if (!$packer instanceof PackerInterface) {
69
            throw new InvalidConfigException(Exception::INVALID_PACKER);
70
        }
71
72
        return $packer;
73
    }
74
}
75