Exemplo n.º 1
0
 public function testParseResponseFromJsonToUniqueIds()
 {
     include_once dirname(__FILE__) . "/../../../Modules/SiCDSInterface/Parser.php";
     include_once dirname(__FILE__) . "/../../../Setup.php";
     $json = '{"key": "client1","results":[{"id": "item1","result":"unique"}]}';
     $parser = new \Swiftriver\SiCDSInterface\Parser();
     $parser->ParseResponseFromJsonToUniqueIds($json);
 }
 /**
  * Interface method that all PrePorcessing Steps must implement
  *
  * @param \Swiftriver\Core\ObjectModel\Content[] $contentItems
  * @param \Swiftriver\Core\Configuration\ConfigurationHandlers\CoreConfigurationHandler $configuration
  * @param \Log $logger
  * @return \Swiftriver\Core\ObjectModel\Content[]
  */
 public function Process($contentItems, $configuration, $logger)
 {
     try {
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method invoked]", \PEAR_LOG_DEBUG);
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [START: Loading module configuration]", \PEAR_LOG_DEBUG);
         $config = \Swiftriver\Core\Setup::DynamicModuleConfiguration()->Configuration;
         if (!key_exists($this->Name(), $config)) {
             $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [The SiCDS Pre Processing Step was called but no configuration exists for this module]", \PEAR_LOG_ERR);
             $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG);
             return $contentItems;
         }
         $config = $config[$this->Name()];
         foreach ($this->ReturnRequiredParameters() as $requiredParam) {
             if (!key_exists($requiredParam->name, $config)) {
                 $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [The SiCDS Pre Processing Step was called but all the required configuration properties could not be loaded]", \PEAR_LOG_ERR);
                 $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG);
                 return $contentItems;
             }
         }
         $apiKey = (string) $config["API Key"]->value;
         $serviceUrl = (string) $config["Service Url"]->value;
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [END: Loading module configuration]", \PEAR_LOG_DEBUG);
         $uniqueContentItems = array();
         $parser = new \Swiftriver\SiCDSInterface\Parser();
         $serviceInterface = new \Swiftriver\SiCDSInterface\ServiceInterface();
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [START: Looping through the content items]", \PEAR_LOG_DEBUG);
         foreach ($contentItems as $item) {
             try {
                 $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [START: Parsing content item into JSON]", \PEAR_LOG_DEBUG);
                 $jsonForService = $parser->ParseItemToRequestJson($item, $apiKey);
                 $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [END: Parsing content item into JSON]", \PEAR_LOG_DEBUG);
                 $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [START: Calling the SiCDS]", \PEAR_LOG_DEBUG);
                 $jsonFromService = $serviceInterface->InterafceWithService($serviceUrl, $jsonForService, $configuration);
                 $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [END: Calling the SiCDS]", \PEAR_LOG_DEBUG);
                 if ($parser->ContentIsUnique($jsonFromService, $item->id)) {
                     $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Content with Id: {$item->id} is unique]", \PEAR_LOG_DEBUG);
                     $uniqueContentItems[] = $item;
                 } else {
                     $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Content with Id: {$item->id} a duplicate]", \PEAR_LOG_DEBUG);
                 }
             } catch (\Exception $e) {
                 $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [An exception was thrown]", \PEAR_LOG_ERR);
                 $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [{$e}]", \PEAR_LOG_ERR);
                 $uniqueContentItems[] = $item;
             }
         }
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [END: Looping through the content items]", \PEAR_LOG_DEBUG);
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG);
         return $uniqueContentItems;
     } catch (\Exception $e) {
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [An exception was thrown]", \PEAR_LOG_ERR);
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [{$e}]", \PEAR_LOG_ERR);
         $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG);
         return $contentItems;
     }
 }