Passed
Pull Request — master (#67)
by Moiseenko
20:30
created

RoadRunnerGrpcApplicationRunner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 14
nc 1
nop 13
dl 0
loc 29
ccs 15
cts 15
cp 1
crap 1
rs 9.7998
c 1
b 0
f 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Runner\RoadRunner;
6
7
use Spiral\RoadRunner\GRPC\Invoker;
8
use Spiral\RoadRunner\GRPC\InvokerInterface;
9
use Spiral\RoadRunner\GRPC\Server;
10
use Spiral\RoadRunner\GRPC\ServiceInterface;
11
use Spiral\RoadRunner\Worker;
12
use Yiisoft\Yii\Runner\ApplicationRunner;
13
14
/**
15
 * `RoadRunnerGrpcApplicationRunner` runs the Yii gRPC application using RoadRunner.
16
 */
17
final class RoadRunnerGrpcApplicationRunner extends ApplicationRunner
18
{
19
    private ?InvokerInterface $invoker = null;
20
    public bool $workerInterceptSideEffects = true;
21
    private array $services = [];
22
    private ?Worker $worker = null;
23
24
    /**
25
     * @param string $rootPath The absolute path to the project root.
26
     * @param bool $debug Whether the debug mode is enabled.
27
     * @param bool $checkEvents Whether to check events' configuration.
28
     * @param string|null $environment The environment name.
29
     * @param string $bootstrapGroup The bootstrap configuration group name.
30
     * @param string $eventsGroup The events' configuration group name.
31
     * @param string $diGroup The container definitions' configuration group name.
32
     * @param string $diProvidersGroup The container providers' configuration group name.
33
     * @param string $diDelegatesGroup The container delegates' configuration group name.
34
     * @param string $diTagsGroup The container tags' configuration group name.
35
     * @param string $paramsGroup The configuration parameters group name.
36
     * @param array $nestedParamsGroups Configuration group names that are included into configuration parameters group.
37
     * This is needed for recursive merging of parameters.
38
     * @param array $nestedEventsGroups Configuration group names that are included into events' configuration group.
39
     * This is needed for reverse and recursive merge of events' configurations.
40
     *
41
     * @psalm-param list<string> $nestedParamsGroups
42
     * @psalm-param list<string> $nestedEventsGroups
43
     */
44 2
    public function __construct(
45
        string $rootPath,
46
        bool $debug = false,
47
        bool $checkEvents = false,
48
        ?string $environment = null,
49
        string $bootstrapGroup = 'bootstrap-web',
50
        string $eventsGroup = 'events-web',
51
        string $diGroup = 'di-web',
52
        string $diProvidersGroup = 'di-providers-web',
53
        string $diDelegatesGroup = 'di-delegates-web',
54
        string $diTagsGroup = 'di-tags-web',
55
        string $paramsGroup = 'params-web',
56
        array $nestedParamsGroups = ['params'],
57
        array $nestedEventsGroups = ['events'],
58
    ) {
59 2
        parent::__construct(
60 2
            $rootPath,
61 2
            $debug,
62 2
            $checkEvents,
63 2
            $environment,
64 2
            $bootstrapGroup,
65 2
            $eventsGroup,
66 2
            $diGroup,
67 2
            $diProvidersGroup,
68 2
            $diDelegatesGroup,
69 2
            $diTagsGroup,
70 2
            $paramsGroup,
71 2
            $nestedParamsGroups,
72 2
            $nestedEventsGroups,
73 2
        );
74
    }
75
76 1
    public function run(): void
77
    {
78 1
        $server = new Server($this->getInvoker(), ['debug' => $this->debug]);
79
80
        /**
81
         * @var class-string<ServiceInterface> $interface
82
         * @var ServiceInterface $service
83
         */
84 1
        foreach ($this->getServices() as $interface => $service) {
85 1
            $server->registerService($interface, new $service());
86
        }
87
88 1
        $server->serve($this->getWorker(), static function () {
89 1
            gc_collect_cycles();
90 1
        });
91
    }
92
93
    /**
94
     * Returns a new instance with the specified gRPC worker instance
95
     *
96
     * @return $this
97
     */
98 1
    public function withWorker(Worker $worker): self
99
    {
100 1
        $instance = clone $this;
101 1
        $instance->worker = $worker;
102
103 1
        return $instance;
104
    }
105
106
    /**
107
     * Transmitted services for registration gRPC server
108
     *
109
     * @param array $services Services array (key-value pairs)
110
     * ```php
111
     * [
112
     *      ServiceInterface::class => Service::class
113
     * ]
114
     * ```
115
     *
116
     * @return $this
117
     */
118 2
    public function setServices(array $services): self
119
    {
120 2
        $this->services = $services;
121
122 2
        return $this;
123
    }
124
125 2
    public function getServices(): array
126
    {
127 2
        return $this->services;
128
    }
129
130 1
    public function getWorker(): Worker
131
    {
132 1
        return $this->worker ?? Worker::create($this->workerInterceptSideEffects);
133
    }
134
135 1
    public function getInvoker(): InvokerInterface
136
    {
137 1
        return $this->invoker ?? new Invoker();
138
    }
139
}
140