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

src/Plugin/PackerPlugin.php (1 issue)

Labels
Severity
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
It seems like $rocket->getDestination() can also be of type Yansongda\Supports\Collection; however, parameter $response of Yansongda\Pay\Contract\PackerInterface::unpack() does only seem to accept Psr\Http\Message\ResponseInterface, 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->unpack(/** @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): 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