This repository was archived by the owner on Jan 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifyMeManager.php
More file actions
176 lines (152 loc) · 3.68 KB
/
NotifyMeManager.php
File metadata and controls
176 lines (152 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/*
* This file is part of NotifyMe.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NotifyMeHQ\Manager;
use InvalidArgumentException;
use NotifyMeHQ\Contracts\FactoryInterface;
use NotifyMeHQ\Contracts\ManagerInterface;
use NotifyMeHQ\Support\Arr;
class NotifyMeManager implements ManagerInterface
{
/**
* The active connection instances.
*
* @var \NotifyMeHQ\Contracts\GatewayInterface[]
*/
protected $connections = [];
/**
* The connection factory instance.
*
* @var \NotifyMeHQ\Contracts\FactoryInterface
*/
protected $factory;
/**
* The connection configuration.
*
* @var array[]
*/
protected $config;
/**
* The default connection name.
*
* @var string
*/
protected $default;
/**
* Create a new notifyme manager instance.
*
* @param \NotifyMeHQ\Contracts\FactoryInterface $factory
* @param array[] $config
* @param string $default
*
* @return void
*/
public function __construct(FactoryInterface $factory, array $config, $default)
{
$this->factory = $factory;
$this->config = $config;
$this->default = $default;
}
/**
* Get a connection instance.
*
* @param string|null $name
*
* @return \NotifyMeHQ\Contracts\GatewayInterface
*/
public function connection($name = null)
{
$name = $name ?: $this->default;
if (!isset($this->connections[$name])) {
$this->connections[$name] = $this->makeConnection($name);
}
return $this->connections[$name];
}
/**
* Reconnect to the given connection.
*
* @param string|null $name
*
* @return \NotifyMeHQ\Contracts\GatewayInterface
*/
public function reconnect($name = null)
{
$name = $name ?: $this->default;
$this->disconnect($name);
return $this->connection($name);
}
/**
* Disconnect from the given connection.
*
* @param string|null $name
*
* @return void
*/
public function disconnect($name = null)
{
$name = $name ?: $this->default;
unset($this->connections[$name]);
}
/**
* Make the connection instance.
*
* @param string $name
*
* @return \NotifyMeHQ\Contracts\GatewayInterface
*/
protected function makeConnection($name)
{
$config = $this->getConnectionConfig($name);
return $this->factory->make($config);
}
/**
* Get the configuration for a connection.
*
* @param string $name
*
* @return array
*/
public function getConnectionConfig($name)
{
$name = $name ?: $this->default;
if (!is_array($config = Arr::get($this->config, $name)) && !$config) {
throw new InvalidArgumentException("Connection [$name] not configured.");
}
return $config;
}
/**
* Get the default connection name.
*
* @return string
*/
public function getDefaultConnection()
{
return $this->default;
}
/**
* Set the default connection name.
*
* @param string $name
*
* @return void
*/
public function setDefaultConnection($name)
{
$this->default = $name;
}
/**
* Return all of the created connections.
*
* @return \NotifyMeHQ\Contracts\GatewayInterface[]
*/
public function getConnections()
{
return $this->connections;
}
}