Test Failed
Push — master ( 9d73a2...14c4f6 )
by hugh
06:42
created

Manager::getConfigValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 12
rs 10
1
<?php
2
3
namespace HughCube\ACMClient;
4
5
use Closure;
6
use Illuminate\Contracts\Redis\Factory;
7
use InvalidArgumentException;
8
9
/**
10
 * @mixin \Illuminate\Redis\Connections\Connection
11
 */
12
class Manager implements Factory
13
{
14
    /**
15
     * The application instance.
16
     *
17
     * @var \Illuminate\Contracts\Foundation\Application
18
     */
19
    protected $app;
20
21
    /**
22
     * The acm server configurations.
23
     *
24
     * @var array
25
     */
26
    protected $config;
27
28
    /**
29
     * The acm connections.
30
     *
31
     * @var Client[]
32
     */
33
    protected $connections = [];
34
35
    /**
36
     * Create a new Redis manager instance.
37
     *
38
     * @param \Illuminate\Contracts\Foundation\Application $app
39
     * @param string $driver
40
     * @param array $config
41
     * @return void
42
     */
43
    public function __construct($app, array $config)
44
    {
45
        $this->app = $app;
46
        $this->config = $config;
47
    }
48
49
    /**
50
     * Get a Redis connection by name.
51
     *
52
     * @param string|null $name
53
     * @return Client
54
     */
55
    public function connection($name = null)
56
    {
57
        $name = null == $name ? 'default' : $name;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $name of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
58
59
        if (isset($this->connections[$name])) {
60
            return $this->connections[$name];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connections[$name] returns the type HughCube\ACMClient\Client which is incompatible with the return type mandated by Illuminate\Contracts\Redis\Factory::connection() of Illuminate\Redis\Connections\Connection.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
61
        }
62
63
        return $this->connections[$name] = $this->resolve($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->connection...= $this->resolve($name) returns the type HughCube\ACMClient\Client which is incompatible with the return type mandated by Illuminate\Contracts\Redis\Factory::connection() of Illuminate\Redis\Connections\Connection.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
64
    }
65
66
    /**
67
     * Resolve the given connection by name.
68
     *
69
     * @param string|null $name
70
     * @return Client
71
     *
72
     * @throws \InvalidArgumentException
73
     */
74
    public function resolve($name = null)
75
    {
76
        $name = $name ?: 'default';
77
78
        if (isset($this->config[$name])) {
79
            $config = (new Config())->setNamespace($this->getConfigValue($name, "namespace"))
80
                ->setGroupName($this->getConfigValue($name, "groupName"))
81
                ->setAccessKey($this->getConfigValue($name, "accessKey"))
82
                ->setSecretKey($this->getConfigValue($name, "secretKey"));
83
84
            return new Client($config);
85
        }
86
87
        throw new InvalidArgumentException("ACM connection [{$name}] not configured.");
88
    }
89
90
    protected function getConfigValue($name, $key, $default = null)
91
    {
92
        if (isset($this->config[$name], $this->config[$name][$key])) {
93
            return $this->config[$name][$key];
94
        }
95
96
        $options = $this->config['options'] ?? [];
97
        if (isset($options[$key])) {
98
            return $options[$key];
99
        }
100
101
        return $default;
102
    }
103
104
    /**
105
     * Return all of the created connections.
106
     *
107
     * @return Client[]
108
     */
109
    public function connections()
110
    {
111
        return $this->connections;
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function createSubscription($channels, Closure $callback, $method = 'subscribe')
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

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

117
    public function createSubscription($channels, Closure $callback, /** @scrutinizer ignore-unused */ $method = 'subscribe')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $callback is not used and could be removed. ( Ignorable by Annotation )

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

117
    public function createSubscription($channels, /** @scrutinizer ignore-unused */ Closure $callback, $method = 'subscribe')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $channels is not used and could be removed. ( Ignorable by Annotation )

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

117
    public function createSubscription(/** @scrutinizer ignore-unused */ $channels, Closure $callback, $method = 'subscribe')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119
        // TODO: Implement createSubscription() method.
120
    }
121
}
122