Passed
Pull Request — master (#211)
by Dmitriy
02:14
created

StateResetter::reset()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 3
b 0
f 0
nc 4
nop 0
dl 0
loc 12
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * State resetter allows to reset state of the services that are currently stored in the container and have "reset"
11
 * callback defined. The reset should be triggered after each request-response cycle in case you build long-running
12
 * applications with tools like [Swoole](https://www.swoole.co.uk/) or [RoadRunner](https://roadrunner.dev/).
13
 */
14
class StateResetter
15
{
16
    private array $resetters;
17
    private ?ContainerInterface $container;
18
19 3
    public function __construct(array $resetters = [], ContainerInterface $container = null)
20
    {
21 3
        $this->resetters = $resetters;
22 3
        $this->container = $container;
23 3
    }
24
25 3
    public function reset(): void
26
    {
27 3
        foreach ($this->resetters as $resetter) {
28 3
            if ($resetter instanceof self) {
29 1
                $resetter->reset();
30 1
                continue;
31
            }
32 3
            if ($this->container !== null) {
33 3
                $resetter($this->container);
34 3
                continue;
35
            }
36
            $resetter();
37
        }
38 3
    }
39
}
40