1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TxTextControl\ReportingCloud\Service; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; |
7
|
|
|
use TxTextControl\ReportingCloud\ReportingCloud; |
8
|
|
|
use Zend\ServiceManager\Factory\FactoryInterface; |
9
|
|
|
|
10
|
|
|
class ReportingCloudFactory implements FactoryInterface |
11
|
|
|
{ |
12
|
7 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
13
|
|
|
{ |
14
|
7 |
|
$config = $container->get('Config'); |
15
|
|
|
|
16
|
6 |
|
$credentials = $this->getCredentials($config); |
17
|
|
|
|
18
|
1 |
|
return new ReportingCloud($credentials); |
19
|
|
|
} |
20
|
|
|
|
21
|
6 |
|
protected function getCredentials($config) |
22
|
|
|
{ |
23
|
6 |
|
$help = "Copy '/vendor/textcontrol/txtextcontrol-reportingcloud-zf3-module/config/reportingcloud.local.php.dist' to '/config/autoload/reportingcloud.local.php' in your Zend Framework 3 application, then add your ReportingCloud credentials to that file."; |
24
|
|
|
|
25
|
6 |
|
if (!array_key_exists('reportingcloud', $config)) { |
26
|
1 |
|
$message = "The key 'reportingcloud' has not been specified in your application's configuration file. {$help}"; |
27
|
1 |
|
throw new InvalidArgumentException($message); |
28
|
|
|
} |
29
|
|
|
|
30
|
5 |
|
if (!array_key_exists('credentials', $config['reportingcloud'])) { |
31
|
1 |
|
$message = "The key 'credentials' has not been specified under the key 'reportingcloud' in your application's configuration file. {$help}"; |
32
|
1 |
|
throw new InvalidArgumentException($message); |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
View Code Duplication |
if (!array_key_exists('username', $config['reportingcloud']['credentials'])) { |
|
|
|
|
36
|
2 |
|
$message = "The key 'username' has not been specified under the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. {$help}"; |
37
|
2 |
|
throw new InvalidArgumentException($message); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
View Code Duplication |
if (!array_key_exists('password', $config['reportingcloud']['credentials'])) { |
|
|
|
|
41
|
1 |
|
$message = "The key 'password' has not been specified under the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. {$help}"; |
42
|
1 |
|
throw new InvalidArgumentException($message); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return [ |
46
|
1 |
|
'username' => $config['reportingcloud']['credentials']['username'], |
47
|
1 |
|
'password' => $config['reportingcloud']['credentials']['password'], |
48
|
1 |
|
]; |
49
|
|
|
} |
50
|
|
|
} |
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.