| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * ParamConverter class for entry point to Analytics Bundle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Graviton\AnalyticsBundle\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Graviton\AnalyticsBundle\Helper\JsonMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Graviton\AnalyticsBundle\Model\AnalyticModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Doctrine\Common\Cache\CacheProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\RequestStack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Service Request Converter and startup for Analytics | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @license  http://opensource.org/licenses/gpl-license.php GNU Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @link     http://swisscom.ch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class ServiceManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** Cache name for services */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     const CACHE_KEY_SERVICES = 'analytics_services'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     const CACHE_KEY_SERVICES_TIME = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     const CACHE_KEY_SERVICES_URLS = 'analytics_services_urls'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     const CACHE_KEY_SERVICES_URLS_TIME = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     const CACHE_KEY_SERVICES_PREFIX = 'analytics_'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** @var Request */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** @var AnalyticsManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected $analyticsManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** @var CacheProvider */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     protected $cacheProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** @var Router */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected $directory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * ServiceConverter constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param RequestStack     $requestStack        Sf Request information service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param AnalyticsManager $analyticsManager    Db Manager and query control | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param CacheProvider    $cacheProvider       Cache service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param Router           $router              To manage routing generation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param string           $definitionDirectory Where definitions are stored | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         RequestStack $requestStack, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         AnalyticsManager $analyticsManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         CacheProvider $cacheProvider, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         Router $router, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $definitionDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->request = $requestStack->getCurrentRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->analyticsManager = $analyticsManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->cacheProvider = $cacheProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->router = $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->directory = $definitionDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Scan base root directory for analytic definitions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     private function getDirectoryServices() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $services = $this->cacheProvider->fetch(self::CACHE_KEY_SERVICES); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (is_array($services)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return $services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $services = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if (strpos($this->directory, 'vendor/graviton/graviton')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->directory = str_replace('vendor/graviton/graviton/', '', $this->directory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (!is_dir($this->directory)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return $services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $finder = new Finder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $finder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ->files() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ->in($this->directory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ->path('/\/analytics\//i') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->name('*.json') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ->notName('_*') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ->sortByName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         foreach ($finder as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $key = $file->getFilename(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $data = json_decode($file->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             if (json_last_error()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 throw new InvalidConfigurationException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     sprintf('Analytics file: %s could not be loaded due to error: ', $key, json_last_error_msg()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $services[$data->route] = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->cacheProvider->save(self::CACHE_KEY_SERVICES, $services, self::CACHE_KEY_SERVICES_TIME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * Return array of available services | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function getServices() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $services = $this->cacheProvider->fetch(self::CACHE_KEY_SERVICES_URLS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if (is_array($services)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             return $services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $this->getDirectoryServices(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $services = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $r = $this->router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         foreach ($services as $name => $service) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $services[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 '$ref' => $r->generate('graviton_analytics_service', ['service' => $service->route], false), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 'profile' => $r->generate('graviton_analytics_service_schema', ['service' => $service->route], true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $this->cacheProvider->save(self::CACHE_KEY_SERVICES_URLS, $services, self::CACHE_KEY_SERVICES_URLS_TIME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * Get service definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param string $name Route name for service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @throws NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return AnalyticModel | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |     private function getServiceSchemaByRoute($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         $services = $this->getDirectoryServices(); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         // Locate the schema definition | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |         if (!array_key_exists($name, $services)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |             throw new NotFoundHttpException( | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |                 sprintf('Service Analytics for %s was not found', $name) | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         $mapper = new JsonMapper(); | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         /** @var AnalyticModel $schema */ | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |         $schema = $mapper->map($services[$name], new AnalyticModel()); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |         return $schema; | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * Will map and find data for defined route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function getData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $serviceRoute = $this->request->get('service'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         // Locate the schema definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $schema = $this->getServiceSchemaByRoute($serviceRoute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         $cacheTime = $schema->getCacheTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         //Cached data if configured | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         if ($cacheTime && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             $cache = $this->cacheProvider->fetch(self::CACHE_KEY_SERVICES_PREFIX.$schema->getRoute()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             return $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $data = $this->analyticsManager->getData($schema); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         if ($cacheTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             $this->cacheProvider->save(self::CACHE_KEY_SERVICES_PREFIX.$schema->getRoute(), $data, $cacheTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * Locate and display service definition schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     public function getSchema() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         $serviceRoute = $this->request->get('service'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         // Locate the schema definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         $schema =  $this->getServiceSchemaByRoute($serviceRoute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         return $schema->getSchema(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  |