Passed
Push — master ( 96d02d...d08d15 )
by Songda
02:13 queued 10s
created

ParserPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 9
c 0
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPacker() 0 11 3
A assembly() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin;
6
7
use Closure;
8
use Yansongda\Pay\Contract\ParserInterface;
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 ParserPlugin 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->parse($rocket->getDestination()));
0 ignored issues
show
Bug introduced by
It seems like $rocket->getDestination() can also be of type Yansongda\Supports\Collection; however, parameter $response of Yansongda\Pay\Contract\ParserInterface::parse() does only seem to accept Psr\Http\Message\ResponseInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        return $rocket->setDestination($packer->parse(/** @scrutinizer ignore-type */ $rocket->getDestination()));
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): ParserInterface
39
    {
40
        $packer = Pay::get($rocket->getDirection() ?? ParserInterface::class);
41
42
        $packer = is_string($packer) ? Pay::get($packer) : $packer;
43
44
        if (!($packer instanceof ParserInterface)) {
45
            throw new InvalidConfigException(InvalidConfigException::INVALID_PACKER);
46
        }
47
48
        return $packer;
49
    }
50
}
51