|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* ReportingCloud Zend Framework 3 Module |
|
6
|
|
|
* |
|
7
|
|
|
* Zend Framework 3 Module for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
|
8
|
|
|
* |
|
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
|
10
|
|
|
* @link https://git.io/Je5US for the canonical source repository |
|
11
|
|
|
* @license https://github.com/TextControl/txtextcontrol-reportingcloud-php-zf-module/blob/master/LICENSE.md |
|
12
|
|
|
* @copyright © 2020 Text Control GmbH |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud\Service; |
|
16
|
|
|
|
|
17
|
|
|
use Interop\Container\ContainerInterface; |
|
18
|
|
|
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; |
|
19
|
|
|
use TxTextControl\ReportingCloud\ReportingCloud; |
|
20
|
|
|
use Zend\ServiceManager\Factory\FactoryInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ReportingCloudFactory |
|
24
|
|
|
* |
|
25
|
|
|
* @package TxTextControl\ReportingCloud |
|
26
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
|
27
|
|
|
*/ |
|
28
|
|
|
class ReportingCloudFactory implements FactoryInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @param ContainerInterface $container |
|
32
|
|
|
* @param string $requestedName |
|
33
|
|
|
* @param array|null $options |
|
34
|
|
|
* |
|
35
|
|
|
* @return ReportingCloud |
|
36
|
|
|
*/ |
|
37
|
36 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): ReportingCloud |
|
38
|
|
|
{ |
|
39
|
36 |
|
$config = $container->get('Config'); |
|
40
|
|
|
|
|
41
|
32 |
|
$credentials = $this->getCredentials($config); |
|
42
|
|
|
|
|
43
|
12 |
|
return new ReportingCloud($credentials); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Return required credentials to use the Reporting Cloud Web API |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $config |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
32 |
|
protected function getCredentials(array $config): array |
|
54
|
|
|
{ |
|
55
|
32 |
|
$ret = null; |
|
56
|
|
|
|
|
57
|
32 |
|
$source = '/vendor/textcontrol/txtextcontrol-reportingcloud-zf3-module/config/reportingcloud.local.php.dist'; |
|
58
|
32 |
|
$dest = '/config/autoload/reportingcloud.local.php'; |
|
59
|
|
|
|
|
60
|
32 |
|
$help = "Copy '{$source}' to '{$dest}' in your Zend Framework 3 application, "; |
|
61
|
32 |
|
$help .= "then add your ReportingCloud credentials to that file."; |
|
62
|
|
|
|
|
63
|
32 |
|
if (!array_key_exists('reportingcloud', $config)) { |
|
64
|
4 |
|
$message = "The key 'reportingcloud' has not been specified in your application's configuration file. "; |
|
65
|
4 |
|
$message .= $help; |
|
66
|
4 |
|
throw new InvalidArgumentException($message); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
28 |
|
if (!array_key_exists('credentials', $config['reportingcloud'])) { |
|
70
|
4 |
|
$message = "The key 'credentials' has not been specified under the key 'reportingcloud' "; |
|
71
|
4 |
|
$message .= "in your application's configuration file. "; |
|
72
|
4 |
|
$message .= $help; |
|
73
|
4 |
|
throw new InvalidArgumentException($message); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
24 |
|
$c = $config['reportingcloud']['credentials']; |
|
77
|
|
|
|
|
78
|
24 |
|
if (isset($c['api_key']) && !empty($c['api_key'])) { |
|
79
|
|
|
$ret = [ |
|
80
|
4 |
|
'api_key' => $c['api_key'], |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
24 |
|
if (isset($c['username']) && !empty($c['username']) && isset($c['password']) && !empty($c['password'])) { |
|
85
|
|
|
$ret = [ |
|
86
|
8 |
|
'username' => $c['username'], |
|
87
|
8 |
|
'password' => $c['password'], |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
24 |
|
if (null === $ret) { |
|
92
|
12 |
|
$message = "Neither the key 'api_key', nor the keys 'username' and 'password' have been specified under "; |
|
93
|
12 |
|
$message .= "the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. "; |
|
94
|
12 |
|
$message .= $help; |
|
95
|
12 |
|
throw new InvalidArgumentException($message); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
12 |
|
return $ret; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|