Passed
Pull Request — master (#803)
by Songda
01:54
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\PackerInterface;
10
use Yansongda\Pay\Contract\PluginInterface;
11
use Yansongda\Pay\Exception\ContainerException;
12
use Yansongda\Pay\Exception\Exception;
13
use Yansongda\Pay\Exception\InvalidConfigException;
14
use Yansongda\Pay\Exception\ServiceNotFoundException;
15
use Yansongda\Pay\Pay;
16
use Yansongda\Pay\Rocket;
17
18
use function Yansongda\Pay\get_direction;
19
20
class ParserPlugin implements PluginInterface
21
{
22
    /**
23
     * @throws ServiceNotFoundException
24
     * @throws ContainerException
25
     * @throws InvalidConfigException
26
     */
27
    public function assembly(Rocket $rocket, Closure $next): Rocket
28
    {
29
        /* @var Rocket $rocket */
30
        $rocket = $next($rocket);
31
32
        /* @var ResponseInterface $response */
33
        $response = $rocket->getDestination();
34
35
        return $rocket->setDestination(
36
            get_direction($rocket->getDirection())->parse($this->getPacker($rocket), $response)
37
        );
38
    }
39
40
    /**
41
     * @throws ContainerException
42
     * @throws InvalidConfigException
43
     * @throws ServiceNotFoundException
44
     */
45
    protected function getPacker(Rocket $rocket): PackerInterface
46
    {
47
        $packer = Pay::get($rocket->getPacker());
48
49
        $packer = is_string($packer) ? Pay::get($packer) : $packer;
50
51
        if (!$packer instanceof PackerInterface) {
52
            throw new InvalidConfigException(Exception::INVALID_PACKER);
53
        }
54
55
        return $packer;
56
    }
57
}
58