/**
  * @dataProvider messagesProvider
  * @test
  * @param ActorUpdated|ActorCreated $incomingEvent
  * @param ActorUpdatedEnrichedWithCdbXml|ActorCreatedEnrichedWithCdbXml $newEvent
  */
 public function it_should_retrieve_cdbxml_from_sapi_with_a_transformer($incomingEvent)
 {
     $this->expectHttpClientToReturnCdbXmlFromUrl('http://search-prod.lodgon.com/search/rest/detail/event/318F2ACB-F612-6F75-0037C9C29F44087A?noauth=true&version=3.3');
     $transformer = new OfferToSapiUrlTransformer('http://search-prod.lodgon.com/search/rest/detail/event/%s?noauth=true&version=3.3');
     $this->enricher->withUrlTransformer($transformer);
     $this->publish($incomingEvent);
 }
 public function register(Application $app)
 {
     /** @var \Qandidate\Toggle\ToggleManager $toggles */
     $toggles = $app['toggles'];
     $importFromSapi = $toggles->active('import-from-sapi', $app['toggles.context']);
     $app['udb2_log_handler'] = $app->share(function (Application $app) {
         return new \Monolog\Handler\StreamHandler(__DIR__ . '/../log/udb2.log');
     });
     $app['udb2_deserializer_locator'] = $app->share(function (Application $app) {
         $deserializerLocator = new SimpleDeserializerLocator();
         $deserializerLocator->registerDeserializer(new StringLiteral('application/vnd.cultuurnet.udb2-events.actor-created+json'), new ActorCreatedJSONDeserializer());
         $deserializerLocator->registerDeserializer(new StringLiteral('application/vnd.cultuurnet.udb2-events.actor-updated+json'), new ActorUpdatedJSONDeserializer());
         $deserializerLocator->registerDeserializer(new StringLiteral('application/vnd.cultuurnet.udb2-events.event-created+json'), new EventCreatedJSONDeserializer());
         $deserializerLocator->registerDeserializer(new StringLiteral('application/vnd.cultuurnet.udb2-events.event-updated+json'), new EventUpdatedJSONDeserializer());
         return $deserializerLocator;
     });
     $app['udb2_event_bus_forwarding_consumer_factory'] = $app->share(function (Application $app) {
         return new EventBusForwardingConsumerFactory($app['amqp-execution-delay'], $app['amqp.connection'], $app['logger.amqp.event_bus_forwarder'], $app['udb2_deserializer_locator'], $app['event_bus'], new StringLiteral($app['config']['amqp']['consumer_tag']));
     });
     $app['amqp.udb2_event_bus_forwarding_consumer'] = $app->share(function (Application $app) {
         $consumerConfig = $app['config']['amqp']['consumers']['udb2'];
         $exchange = new StringLiteral($consumerConfig['exchange']);
         $queue = new StringLiteral($consumerConfig['queue']);
         /** @var EventBusForwardingConsumerFactory $consumerFactory */
         $consumerFactory = $app['udb2_event_bus_forwarding_consumer_factory'];
         return $consumerFactory->create($exchange, $queue);
     });
     $app['cdbxml_enricher_http_client_adapter'] = $app->share(function (Application $app) {
         $handlerStack = new HandlerStack(\GuzzleHttp\choose_handler());
         $handlerStack->push(Middleware::prepareBody(), 'prepare_body');
         $responseTimeout = 3;
         $connectTimeout = 1;
         if (isset($app['udb2_cdbxml_enricher.http_response_timeout'])) {
             $responseTimeout = $app['udb2_cdbxml_enricher.http_response_timeout'];
         }
         if (isset($app['udb2_cdbxml_enricher.http_connect_timeout'])) {
             $connectTimeout = $app['udb2_cdbxml_enricher.http_connect_timeout'];
         }
         $client = new Client(['handler' => $handlerStack, 'timeout' => $responseTimeout, 'connect_timeout' => $connectTimeout]);
         return new ClientAdapter($client);
     });
     $app['cdbxml_enricher_logger'] = $app->share(function (Application $app) {
         $logger = new \Monolog\Logger('udb2-events-cdbxml-enricher');
         $logger->pushHandler($app['udb2_log_handler']);
         return $logger;
     });
     $app['udb2_events_cdbxml_enricher'] = $app->share(function (Application $app) use($importFromSapi) {
         $enricher = new EventCdbXmlEnricher($app['event_bus'], $app['cdbxml_enricher_http_client_adapter']);
         $enricher->setLogger($app['cdbxml_enricher_logger']);
         if ($importFromSapi) {
             $eventUrlFormat = $app['config']['udb2_import']['event_url_format'];
             if (!isset($eventUrlFormat)) {
                 throw new \Exception('can not import events from sapi without configuring an url format');
             }
             $transformer = new OfferToSapiUrlTransformer($eventUrlFormat);
             $enricher->withUrlTransformer($transformer);
         }
         return $enricher;
     });
     $app['udb2_actor_events_cdbxml_enricher'] = $app->share(function (Application $app) use($importFromSapi) {
         $enricher = new ActorEventCdbXmlEnricher($app['event_bus'], $app['cdbxml_enricher_http_client_adapter']);
         $enricher->setLogger($app['cdbxml_enricher_logger']);
         if ($importFromSapi) {
             $actorUrlFormat = $app['config']['udb2_import']['actor_url_format'];
             if (!isset($actorUrlFormat)) {
                 throw new \Exception('can not import actors from sapi without configuring an url format');
             }
             $transformer = new OfferToSapiUrlTransformer($actorUrlFormat);
             $enricher->withUrlTransformer($transformer);
         }
         return $enricher;
     });
     $app['udb2_events_to_udb3_place_applier'] = $app->share(function (Application $app) {
         $applier = new EventApplier(new LabeledAsUDB3Place(), $app['place_repository'], new EventToUDB3PlaceFactory());
         $logger = new \Monolog\Logger('udb2-events-to-udb3-place-applier');
         $logger->pushHandler($app['udb2_log_handler']);
         $applier->setLogger($logger);
         return $applier;
     });
     $app['udb2_events_to_udb3_event_applier'] = $app->share(function (Application $app) {
         $applier = new EventApplier(new Not(new LabeledAsUDB3Place()), $app['event_repository'], new EventToUDB3EventFactory());
         $logger = new \Monolog\Logger('udb2-events-to-udb3-event-applier');
         $logger->pushHandler($app['udb2_log_handler']);
         $applier->setLogger($logger);
         return $applier;
     });
     $app['udb2_actor_events_to_udb3_place_applier'] = $app->share(function (Application $app) {
         $applier = new ActorEventApplier($app['place_repository'], new ActorToUDB3PlaceFactory(), new QualifiesAsPlaceSpecification());
         $logger = new \Monolog\Logger('udb2-actor-events-to-udb3-place-applier');
         $logger->pushHandler($app['udb2_log_handler']);
         $applier->setLogger($logger);
         return $applier;
     });
     $app['udb2_actor_events_to_udb3_organizer_applier'] = $app->share(function (Application $app) {
         $applier = new ActorEventApplier($app['organizer_repository'], new ActorToUDB3OrganizerFactory(), new QualifiesAsOrganizerSpecification());
         $logger = new \Monolog\Logger('udb2-actor-events-to-udb3-organizer-applier');
         $logger->pushHandler($app['udb2_log_handler']);
         $applier->setLogger($logger);
         return $applier;
     });
     $app['udb2_label_importer'] = $app->share(function (Application $app) {
         $labelImporter = new LabelImporter($app['labels.constraint_aware_service']);
         $logger = new \Monolog\Logger('udb2-label-importer');
         $logger->pushHandler($app['udb2_log_handler']);
         $labelImporter->setLogger($logger);
         return $labelImporter;
     });
     $app['udb2_event_cdbid_extractor'] = $app->share(function (Application $app) {
         return new EventCdbIdExtractor($app['udb2_place_external_id_mapping_service'], $app['udb2_organizer_external_id_mapping_service']);
     });
     $app['udb2_place_external_id_mapping_service'] = $app->share(function (Application $app) {
         $yamlFileLocation = $app['udb2_place_external_id_mapping.yml_file_location'];
         return $app['udb2_external_id_mapping_service_factory']($yamlFileLocation);
     });
     $app['udb2_organizer_external_id_mapping_service'] = $app->share(function (Application $app) {
         $yamlFileLocation = $app['udb2_organizer_external_id_mapping.yml_file_location'];
         return $app['udb2_external_id_mapping_service_factory']($yamlFileLocation);
     });
     $app['udb2_external_id_mapping_service_factory'] = $app->protect(function ($yamlFileLocation) {
         $map = [];
         if (file_exists($yamlFileLocation)) {
             $yaml = file_get_contents($yamlFileLocation);
             $yaml = Yaml::parse($yaml);
             if (is_array($yaml)) {
                 $map = $yaml;
             }
         }
         return new ArrayMappingService($map);
     });
 }