1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel SMSFactor. |
5
|
|
|
* |
6
|
|
|
* (c) Filippo Galante <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace IlGala\SMSFactor\Connectors; |
13
|
|
|
|
14
|
|
|
use IlGala\SMSFactor\Adapters\GuzzleAdapter; |
15
|
|
|
use GrahamCampbell\Manager\ConnectorInterface; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This is the guzzle connector class. |
20
|
|
|
* |
21
|
|
|
* @author Filippo Galante <[email protected]> |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
class GuzzleConnector implements ConnectorInterface |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Establish an adapter connection. |
28
|
|
|
* |
29
|
|
|
* @param string[] $config |
30
|
|
|
* |
31
|
|
|
* @return \IlGala\SMSFactor\Adapters\GuzzleAdapter |
32
|
|
|
*/ |
33
|
|
|
public function connect(array $config) |
34
|
|
|
{ |
35
|
|
|
$config = $this->getConfig($config); |
36
|
|
|
return $this->getAdapter($config); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the configuration. |
41
|
|
|
* |
42
|
|
|
* @param string[] $config |
43
|
|
|
* |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
* |
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
|
|
protected function getConfig(array $config) |
49
|
|
|
{ |
50
|
|
|
if (!array_key_exists('username', $config) || !array_key_exists('password', $config) || !array_key_exists('accept', $config)) { |
51
|
|
|
throw new InvalidArgumentException('The guzzle connector requires configuration.'); |
52
|
|
|
} |
53
|
|
|
return array_only($config, ['username', 'password', 'accept']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the guzzle adapter. |
58
|
|
|
* |
59
|
|
|
* @param string[] $config |
60
|
|
|
* |
61
|
|
|
* @return \IlGala\SMSFactor\Adapters\GuzzleAdapter |
62
|
|
|
*/ |
63
|
|
|
protected function getAdapter(array $config) |
64
|
|
|
{ |
65
|
|
|
return new GuzzleAdapter($config['username'], $config['password'], $config['accept']); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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.