Passed
Push — master ( 66351d...335aef )
by Hirofumi
20:01
created

GatewayRegistry::setAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Domain\Model;
5
6
use InvalidArgumentException;
7
8 View Code Duplication
class GatewayRegistry
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 string $destinationType
34
     * @param Gateway $gateway
35
     */
36 4
    public function set(string $destinationType, Gateway $gateway): void
37
    {
38 4
        $this->gateways[$destinationType] = $gateway;
39 4
    }
40
41
    /**
42
     * @param array $gateways
43
     */
44
    public function setAll(array $gateways): void
45
    {
46
        $this->gateways = $gateways;
47
    }
48
49
    /**
50
     * @param Destination $destination
51
     * @return Gateway
52
     */
53 5
    public function get(Destination $destination): Gateway
54
    {
55 5
        $destinationType = $destination::type();
56 5
        if (!isset($this->gateways[$destinationType])) {
57 1
            throw new InvalidArgumentException(
58 1
                sprintf(
59 1
                    'No gateway for destination type (%s) is supported.',
60 1
                    $destinationType
61
                )
62
            );
63
        }
64
65 4
        return $this->gateways[$destinationType];
66
    }
67
}
68