Passed
Pull Request — master (#579)
by Songda
02:12
created

ContainerServiceProvider::register()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 8
c 3
b 0
f 0
dl 0
loc 17
rs 8.8333
cc 7
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Service;
6
7
use Closure;
8
use DI\ContainerBuilder;
0 ignored issues
show
Bug introduced by
The type DI\ContainerBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Hyperf\Utils\ApplicationContext as HyperfApplication;
0 ignored issues
show
Bug introduced by
The type Hyperf\Utils\ApplicationContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Illuminate\Container\Container as LaravelContainer;
0 ignored issues
show
Bug introduced by
The type Illuminate\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Container\ContainerInterface;
12
use Throwable;
13
use Yansongda\Pay\Contract\ServiceProviderInterface;
14
use Yansongda\Pay\Exception\ContainerException;
15
use Yansongda\Pay\Exception\ContainerNotFoundException;
16
use Yansongda\Pay\Exception\Exception;
17
use Yansongda\Pay\Pay;
18
19
class ContainerServiceProvider implements ServiceProviderInterface
20
{
21
    private $detectApplication = [
22
        'laravel' => LaravelContainer::class,
23
        'hyperf' => HyperfApplication::class,
24
    ];
25
26
    /**
27
     * @throws \Yansongda\Pay\Exception\ContainerException
28
     */
29
    public function register($data = null): void
30
    {
31
        if ($data instanceof ContainerInterface || $data instanceof Closure) {
32
            Pay::setContainer($data);
33
34
            return;
35
        }
36
37
        foreach ($this->detectApplication as $framework => $application) {
38
            $method = $framework.'Application';
39
40
            if (class_exists($application) && method_exists($this, $method) && $this->{$method}()) {
41
                return;
42
            }
43
        }
44
45
        $this->defaultApplication();
46
    }
47
48
    /**
49
     * @throws \Yansongda\Pay\Exception\ContainerException
50
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
51
     */
52
    protected function laravelApplication(): bool
53
    {
54
        Pay::setContainer(static function () {
55
            return LaravelContainer::getInstance();
56
        });
57
58
        Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, LaravelContainer::getInstance());
59
60
        if (!Pay::has(ContainerInterface::class)) {
61
            Pay::set(ContainerInterface::class, LaravelContainer::getInstance());
62
        }
63
64
        return true;
65
    }
66
67
    /**
68
     * @throws \Yansongda\Pay\Exception\ContainerException
69
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
70
     */
71
    protected function hyperfApplication(): bool
72
    {
73
        if (!HyperfApplication::hasContainer()) {
74
            return false;
75
        }
76
77
        Pay::setContainer(static function () {
78
            return HyperfApplication::getContainer();
79
        });
80
81
        Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, HyperfApplication::getContainer());
82
83
        if (!Pay::has(ContainerInterface::class)) {
84
            Pay::set(ContainerInterface::class, HyperfApplication::getContainer());
85
        }
86
87
        return true;
88
    }
89
90
    /**
91
     * @throws \Yansongda\Pay\Exception\ContainerException
92
     */
93
    protected function defaultApplication(): void
94
    {
95
        if (!class_exists(ContainerBuilder::class)) {
96
            throw new ContainerNotFoundException('Init failed! Maybe you should install `php-di/php-di` first', Exception::CONTAINER_NOT_FOUND);
97
        }
98
99
        $builder = new ContainerBuilder();
100
101
        try {
102
            $container = $builder->build();
103
            $container->set(ContainerInterface::class, $container);
104
            $container->set(\Yansongda\Pay\Contract\ContainerInterface::class, $container);
105
106
            Pay::setContainer($container);
107
        } catch (Throwable $e) {
108
            throw new ContainerException($e->getMessage());
109
        }
110
    }
111
}
112