Exemplo n.º 1
0
 /**
  * Handles GET action
  *
  * @return array
  * @throws NotFoundException
  */
 protected function handleGET()
 {
     if (empty($this->resource)) {
         $service = $this->request->getParameter('service');
         $type = $this->request->getParameter('type');
         $onlyScripted = $this->request->getParameterAsBool('only_scripted');
         if ($onlyScripted) {
             switch ($type) {
                 case 'process':
                     $scripts = EventScript::where('affects_process', 1)->lists('name')->all();
                     break;
                 case 'broadcast':
                     $scripts = EventScript::where('affects_process', 0)->lists('name')->all();
                     break;
                 default:
                     $scripts = EventScript::lists('name')->all();
                     break;
             }
             return ResourcesWrapper::cleanResources(array_values(array_unique($scripts)));
         }
         $results = $this->getEventMap();
         $allEvents = [];
         switch ($type) {
             case 'process':
                 $results = ArrayUtils::get($results, 'process', []);
                 foreach ($results as $serviceKey => $apis) {
                     if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                         unset($results[$serviceKey]);
                     } else {
                         foreach ($apis as $path => $operations) {
                             foreach ($operations['verb'] as $method => $events) {
                                 $allEvents = array_merge($allEvents, $events);
                             }
                         }
                     }
                 }
                 break;
             case 'broadcast':
                 $results = ArrayUtils::get($results, 'broadcast', []);
                 foreach ($results as $serviceKey => $apis) {
                     if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                         unset($results[$serviceKey]);
                     } else {
                         foreach ($apis as $path => $operations) {
                             foreach ($operations['verb'] as $method => $events) {
                                 $allEvents = array_merge($allEvents, $events);
                             }
                         }
                     }
                 }
                 break;
             default:
                 foreach ($results as $type => $services) {
                     foreach ($services as $serviceKey => $apis) {
                         if (!empty($service) && 0 !== strcasecmp($service, $serviceKey)) {
                             unset($results[$type][$serviceKey]);
                         } else {
                             foreach ($apis as $path => $operations) {
                                 foreach ($operations['verb'] as $method => $events) {
                                     $allEvents = array_merge($allEvents, $events);
                                 }
                             }
                         }
                     }
                 }
                 break;
         }
         if (!$this->request->getParameterAsBool(ApiOptions::AS_LIST)) {
             return $results;
         }
         return ResourcesWrapper::cleanResources(array_values(array_unique($allEvents)));
     }
     $related = $this->request->getParameter(ApiOptions::RELATED);
     if (!empty($related)) {
         $related = explode(',', $related);
     } else {
         $related = [];
     }
     //	Single script by name
     $fields = [ApiOptions::FIELDS_ALL];
     if (null !== ($value = $this->request->getParameter(ApiOptions::FIELDS))) {
         $fields = explode(',', $value);
     }
     if (null === ($foundModel = EventScript::with($related)->find($this->resource, $fields))) {
         throw new NotFoundException("Script not found.");
     }
     return ResponseFactory::create($foundModel->toArray(), $this->nativeFormat);
 }