Passed
Push — master ( c6d1df...be73a8 )
by Hirofumi
07:35
created

GatewayRegistry::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Domain\Model;
5
6
use InvalidArgumentException;
7
8
class GatewayRegistry
9
{
10
    /**
11
     * @var null|GatewayRegistry
12
     */
13
    private static $instance = null;
14
15
    /**
16
     * @var Gateway[]
17
     */
18
    protected $gateways = [];
19
20
    /**
21
     * @return GatewayRegistry
22
     */
23
    public static function instance(): GatewayRegistry
24
    {
25
        if (null === static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
26
            static::$instance = new GatewayRegistry;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
27
        }
28
29
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
30
    }
31
32
    /**
33
     * @param Gateway $gateway
34
     */
35 4
    public function set(Gateway $gateway)
36
    {
37 4
        $this->gateways[$gateway->destinationType()] = $gateway;
38 4
    }
39
40
    /**
41
     * @param Destination $destination
42
     * @return Gateway
43
     */
44 5
    public function get(Destination $destination): Gateway
45
    {
46 5
        $destinationType = $destination->destinationType();
47 5
        if (!isset($this->gateways[$destinationType])) {
48 1
            throw new InvalidArgumentException(
49 1
                sprintf(
50 1
                    'No gateway for destination type (%s) is supported.',
51 1
                    $destinationType
52
                )
53
            );
54
        }
55
56 4
        return $this->gateways[$destinationType];
57
    }
58
}
59