/**
  * @test
  */
 public function it_can_factor_a_label_collection()
 {
     $pair = new KeywordsVisiblesPair(new String('keyword a;keyword b;keyword c'), new String('false;true;true'));
     $expectedLabels = new LabelCollection([new Label('keyword a', false), new Label('keyword b', true), new Label('keyword c', true)]);
     $labels = $pair->getLabels();
     $this->assertEquals($expectedLabels, $labels);
 }
 /**
  * @param Application $app
  * @return Response
  */
 public function connect(Application $app)
 {
     $app['entry_api.command_handler'] = $app->share(function (Application $app) {
         return new SecurityDecoratedCommandHandler(new EntryAPIEventCommandHandler($app['event_repository']), $app['event.security']);
     });
     /** @var ControllerCollection $controllers */
     $controllers = $app['controllers_factory'];
     $app['entryapi_event_controller'] = $app->share(function (Application $app) {
         $controller = new EventController($app['event_repository'], $app['entryapi.link_base_url']);
         return $controller;
     });
     $controllers->get('event/checkpermission', function (Request $request, Application $app) {
         /** @var String[] $eventIds */
         $eventIds = [];
         if (!empty($request->query->get('ids'))) {
             $eventIds = explode(",", $request->query->get('ids'));
             $eventIds = array_filter($eventIds, function ($item) {
                 return trim($item) !== '';
             });
             $eventIds = array_map(function ($cdbid) {
                 return new String($cdbid);
             }, $eventIds);
         }
         $uitId = $request->query->get('user');
         /** @var PermissionQueryInterface $repository */
         $repository = $app['event_permission.repository'];
         $editableEvents = $repository->getEditableEvents(new String($uitId));
         if (empty($eventIds)) {
             $eventIds = $editableEvents;
         }
         /** @var EventPermission[] $eventPermissions */
         $eventPermissions = array_map(function (string $cdbid) use($editableEvents) {
             $isEditable = in_array($cdbid, $editableEvents);
             return new EventPermission($cdbid->toNative(), $isEditable);
         }, $eventIds);
         return $this->createPermissionResponse(new EventPermissionCollection($eventPermissions));
     });
     $controllers->post('/event', function (Request $request, Application $app) {
         $callback = function () use($request, $app) {
             if ($request->getContentType() !== 'xml') {
                 $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not XML.');
                 return $this->createResponse($rsp);
             }
             $xml = new SizeLimitedEventXmlString($request->getContent());
             /** @var \DOMElement $eventElement */
             $eventElement = $xml->eventElement();
             $id = $eventElement->getAttribute('cdbid');
             if ($id) {
                 // First try to retrieve the event from the JSON-LD read model.
                 // This will result in a EventNotFoundException if the event
                 // does not exist.
                 /** @var \CultuurNet\UDB3\EventServiceInterface $service */
                 $service = $app['event_service'];
                 $service->getEvent($id);
                 $command = new UpdateEventFromCdbXml(new String($id), $xml);
             } else {
                 $uuidGenerator = new \Broadway\UuidGenerator\Rfc4122\Version4Generator();
                 $id = $uuidGenerator->generate();
                 $command = new AddEventFromCdbXml(new String($id), $xml);
             }
             $commandHandler = $app['entry_api.command_handler'];
             $commandHandler->handle($command);
             $link = $app['entryapi.link_base_url'] . $id;
             $status = $command instanceof UpdateEventFromCdbXml ? 'ItemModified' : 'ItemCreated';
             $rsp = new Rsp('0.1', 'INFO', $status, $link, null);
             return $rsp;
         };
         return $this->processEventRequest($callback);
     });
     $controllers->post('/event/{cdbid}/keywords', function (Request $request, Application $app, $cdbid) {
         $callback = function () use($request, $app, $cdbid) {
             $repository = $app['event_repository'];
             if ($request->getContentType() !== 'form') {
                 $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not x-www-form-urlencoded.');
                 return $rsp;
             }
             $keywordsString = new String($request->request->get('keywords', ''));
             $visiblesString = new String($request->request->get('visibles', ''));
             $keywordsVisiblesPair = new KeywordsVisiblesPair($keywordsString, $visiblesString);
             $eventId = new String($cdbid);
             $command = new MergeLabels($eventId, $keywordsVisiblesPair->getLabels());
             $commandHandler = new EntryAPIEventCommandHandler($repository);
             $commandHandler->handle($command);
             $link = $app['entryapi.link_base_url'] . $cdbid;
             $rsp = new Rsp('0.1', 'INFO', 'KeywordsCreated', $link, null);
             return $rsp;
         };
         return $this->processEventRequest($callback);
     });
     $controllers->delete('/event/{cdbid}/keywords', 'entryapi_event_controller:deleteKeyword');
     $controllers->post('/event/{cdbid}/translations', 'entryapi_event_controller:translate');
     $controllers->delete('/event/{cdbid}/translations', 'entryapi_event_controller:deleteTranslation');
     $controllers->post('/event/{cdbid}/links', 'entryapi_event_controller:addLink');
     $putCallback = function (Request $request, Application $app, $cdbid) {
         $callback = function () use($request, $app, $cdbid) {
             // First try to retrieve the event from the JSON-LD read model.
             // This will result in a EventNotFoundException if the event
             // does not exist.
             /** @var \CultuurNet\UDB3\EventServiceInterface $service */
             $service = $app['event_service'];
             $service->getEvent($cdbid);
             if ($request->getContentType() !== 'xml') {
                 $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not XML.');
                 return $this->createResponse($rsp);
             }
             $xml = new SizeLimitedEventXmlString($request->getContent());
             $eventId = new String($cdbid);
             $command = new UpdateEventFromCdbXml($eventId, $xml);
             $commandHandler = $app['entry_api.command_handler'];
             $commandHandler->handle($command);
             $link = $app['entryapi.link_base_url'] . $eventId;
             $rsp = new Rsp('0.1', 'INFO', 'ItemModified', $link, null);
             return $rsp;
         };
         return $this->processEventRequest($callback);
     };
     $controllers->put('/event/{cdbid}', $putCallback);
     // Culturefeed Entry UI Drupal module seems to use POST instead of PUT.
     $controllers->post('/event/{cdbid}', $putCallback);
     return $controllers;
 }