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

ContainerServiceProvider::hyperfApplication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Service;
6
7
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...
8
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...
9
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...
10
use Psr\Container\ContainerInterface;
11
use think\Container as ThinkContainer;
0 ignored issues
show
Bug introduced by
The type think\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...
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
        'think' => ThinkContainer::class,
24
        'hyperf' => HyperfApplication::class,
25
    ];
26
27
    /**
28
     * @throws \Yansongda\Pay\Exception\ContainerException
29
     */
30
    public function register(?array $data = null): void
31
    {
32
        foreach ($this->detectApplication as $application) {
33
            if (class_exists($application) && $this->{$application.'Application'}()) {
34
                return;
35
            }
36
        }
37
38
        $this->defaultApplication();
39
    }
40
41
    protected function laravelApplication(): bool
42
    {
43
        Pay::setContainer(function () {
44
            return LaravelContainer::getInstance();
45
        });
46
47
        return true;
48
    }
49
50
    protected function thinkApplication(): bool
51
    {
52
        Pay::setContainer(function () {
53
            return ThinkContainer::getInstance();
54
        });
55
56
        return true;
57
    }
58
59
    protected function hyperfApplication(): bool
60
    {
61
        if (!HyperfApplication::hasContainer()) {
62
            return false;
63
        }
64
65
        Pay::setContainer(function () {
66
            return HyperfApplication::getContainer();
67
        });
68
69
        return true;
70
    }
71
72
    /**
73
     * @throws \Yansongda\Pay\Exception\ContainerException
74
     */
75
    protected function defaultApplication(): void
76
    {
77
        if (!class_exists(ContainerBuilder::class)) {
78
            throw new ContainerNotFoundException('Init failed! Maybe you should install `php-di/php-di` first', Exception::CONTAINER_NOT_FOUND);
79
        }
80
81
        $builder = new ContainerBuilder();
82
83
        try {
84
            $container = $builder->build();
85
            $container->set(ContainerInterface::class, $container);
86
            $container->set(\Yansongda\Pay\Contract\ContainerInterface::class, $container);
87
88
            Pay::setContainer($container);
89
        } catch (Throwable $e) {
90
            throw new ContainerException($e->getMessage());
91
        }
92
    }
93
}
94