| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * ReportingCloud Laminas Module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Laminas 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/JexF4 for the canonical source repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license   https://git.io/JexFB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @copyright © 2020 Text Control GmbH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace TxTextControl\ReportingCloud\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Interop\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Laminas\ServiceManager\Factory\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use TxTextControl\ReportingCloud\ReportingCloud; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |         if (!array_key_exists('reportingcloud', $config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |             $message = "The key 'reportingcloud' has not been specified in your application's configuration file. "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |             $message .= $this->getHelp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |             throw new InvalidArgumentException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 28 |  |         if (!array_key_exists('credentials', $config['reportingcloud'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |             $message = "The key 'credentials' has not been specified under the key 'reportingcloud' "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |             $message .= "in your application's configuration file. "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |             $message .= $this->getHelp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  |             throw new InvalidArgumentException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 24 |  |         $credentials = $config['reportingcloud']['credentials']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 24 |  |         if (!empty($credentials['api_key'] ?? '')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |                 'api_key' => $credentials['api_key'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 20 |  |         if (!empty($credentials['username'] ?? '') && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 20 |  |             !empty($credentials['password'] ?? '')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 8 |  |                 'username' => $credentials['username'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 8 |  |                 'password' => $credentials['password'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 12 |  |         $message = "Either the key 'api_key', or the keys 'username' and 'password' have not been specified under "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 12 |  |         $message .= "the key 'reportingcloud', sub-key 'credentials' in your application's configuration file. "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 12 |  |         $message .= $this->getHelp(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 | 12 |  |         throw new InvalidArgumentException($message); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * Return help text, used as assistance in exception message | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 20 |  |     protected function getHelp(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 20 |  |         $path   = '/vendor/textcontrol/txtextcontrol-reportingcloud-laminas-module'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 20 |  |         $source = "{$path}/config/reportingcloud.local.php.dist"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 20 |  |         $dest   = '/config/autoload/reportingcloud.local.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 20 |  |         $ret = "Copy '{$source}' to '{$dest}' in your Laminas application, "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 20 |  |         $ret .= "then add your ReportingCloud credentials to that file."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 | 20 |  |         return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |