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
|
18 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
13
|
|
|
{ |
14
|
18 |
|
$config = $container->get('Config'); |
15
|
|
|
|
16
|
16 |
|
$credentials = $this->getCredentials($config); |
17
|
|
|
|
18
|
6 |
|
return new ReportingCloud($credentials); |
19
|
|
|
} |
20
|
|
|
|
21
|
16 |
|
protected function getCredentials($config) |
22
|
|
|
{ |
23
|
16 |
|
$ret = null; |
24
|
|
|
|
25
|
16 |
|
$source = '/vendor/textcontrol/txtextcontrol-reportingcloud-zf3-module/config/reportingcloud.local.php.dist'; |
26
|
16 |
|
$dest = '/config/autoload/reportingcloud.local.php'; |
27
|
|
|
|
28
|
16 |
|
$help = "Copy '{$source}' to '{$dest}' in your Zend Framework 3 application, "; |
29
|
16 |
|
$help .= "then add your ReportingCloud credentials to that file."; |
30
|
|
|
|
31
|
16 |
|
if (!array_key_exists('reportingcloud', $config)) { |
32
|
2 |
|
$message = "The key 'reportingcloud' has not been specified in your application's configuration file. "; |
33
|
2 |
|
$message .= $help; |
34
|
2 |
|
throw new InvalidArgumentException($message); |
35
|
|
|
} |
36
|
|
|
|
37
|
14 |
|
if (!array_key_exists('credentials', $config['reportingcloud'])) { |
38
|
2 |
|
$message = "The key 'credentials' has not been specified under the key 'reportingcloud' "; |
39
|
2 |
|
$message .= "in your application's configuration file. "; |
40
|
2 |
|
$message .= $help; |
41
|
2 |
|
throw new InvalidArgumentException($message); |
42
|
|
|
} |
43
|
|
|
|
44
|
12 |
|
$c = $config['reportingcloud']['credentials']; |
45
|
|
|
|
46
|
12 |
|
if (isset($c['api_key']) && !empty($c['api_key'])) { |
47
|
|
|
$ret = [ |
48
|
2 |
|
'api_key' => $c['api_key'], |
49
|
1 |
|
]; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
12 |
|
if (isset($c['username']) && !empty($c['username']) && isset($c['password']) && !empty($c['password'])) { |
53
|
|
|
$ret = [ |
54
|
4 |
|
'username' => $c['username'], |
55
|
4 |
|
'password' => $c['password'], |
56
|
2 |
|
]; |
57
|
2 |
|
} |
58
|
|
|
|
59
|
12 |
|
if (null === $ret) { |
60
|
6 |
|
$message = "Neither the key 'api_key', nor the keys 'username' and 'password' have been specified under "; |
61
|
6 |
|
$message .= "the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. "; |
62
|
6 |
|
$message .= $help; |
63
|
6 |
|
throw new InvalidArgumentException($message); |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
return $ret; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|