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

StateResetter::setResetters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 implements StateResetterInterface
15
{
16
    private array $resetters;
17
    private ?ContainerInterface $container;
18
19 5
    public function __construct(array $resetters = [], ContainerInterface $container = null)
20
    {
21 5
        $this->resetters = $resetters;
22 5
        $this->container = $container;
23 5
    }
24
25 5
    public function reset(): void
26
    {
27 5
        foreach ($this->resetters as $resetter) {
28 5
            if ($resetter instanceof StateResetterInterface) {
29 3
                $resetter->reset();
30 3
                continue;
31
            }
32 5
            if ($this->container !== null) {
33 5
                $resetter($this->container);
34 5
                continue;
35
            }
36
            $resetter();
37
        }
38 5
    }
39
40 3
    public function setResetters(array $resetters): void
41
    {
42 3
        foreach ($resetters as $serviceId => $callback) {
43 3
            $instance = $this->container->get($serviceId);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            /** @scrutinizer ignore-call */ 
44
            $instance = $this->container->get($serviceId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44 3
            $this->resetters[] = $callback->bindTo($instance, get_class($instance));
45
        }
46 3
    }
47
}
48