Exemplo n.º 1
0
 /**
  */
 public function connect()
 {
     //allready connected
     if ($this->pdo instanceof \PDO) {
         return;
     }
     //create PDO connection from INI file section [PDO]
     $settings = $this->iniConfiguration->get('PDO');
     $this->pdo = new \PDO($settings['dsn'], $settings['user'], $settings['pwd'], $this->getPdoOptions($settings));
     $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
 }
 /**
  * @param string $name
  * @param array $configRaw
  * @param integer $limit
  */
 protected function callSync($name, $configRaw, $limit)
 {
     //normalize config
     $config = array('last' => isset($configRaw[$name . '_last']) ? $configRaw[$name . '_last'] : 0, 'rows' => isset($configRaw[$name . '_rows']) ? $configRaw[$name . '_rows'] : 0);
     $filePath = $this->iniConfiguration->getTempPath() . '/' . $name . '.data';
     if ($config['rows'] == 0) {
         //create new request
         $response = $this->getWrapper()->request('data', $name, array('lastDownload' => $config['last']));
         //analyze response
         switch ($this->getWrapper()->analyzeResponse($response)) {
             case 'url':
                 //continue to download
                 $this->getWrapper()->download($response->url, $filePath);
                 break;
             case 'suspend':
                 //suspend processing for time in response
                 return $this->suspend($response->until);
             default:
                 throw new Api\ApiException('Response from datadepo is unknow');
         }
     }
     //open file and read header
     $file = new \SplFileObject($filePath);
     $header = json_decode($file->fgets(), TRUE);
     //check number of lines in downloaded file
     if ($config['rows'] == 0) {
         $file->seek(PHP_INT_MAX);
         $linesTotal = $file->key() + 1;
         if ($header['numRecords'] != $linesTotal - 1) {
             throw new Api\ApiException('Incompleted file downloaded in ' . $name . ' synchronizer');
         }
         $file->rewind();
     }
     $processCount = $this->iniConfiguration->get('limits', 'processCount');
     $collector = $this->createCollector();
     $count = 0;
     //skip line
     $file->seek($config['rows'] + 1);
     while ((!$file->eof() || $file->current() !== FALSE) && $count < $limit) {
         //add line to front
         $line = $this->wrapLine($file->current());
         $collector->add($line);
         $count++;
         //process
         if ($count % $processCount === 0) {
             $this->processChunk($collector);
             $collector = $this->createCollector();
         }
         $file->next();
     }
     //sync rest
     if (count($collector) > 0) {
         $this->processChunk($collector);
     }
     //check num processed
     if ($count == $limit) {
         $this->dataStore->setConfig($name . '_rows', $config['rows'] + $count);
     } else {
         $this->dataStore->setConfig($name . '_rows', 0);
         $this->dataStore->setConfig($name . '_last', $header['generatedAt']);
     }
     return new Api\DataDepoResponse(Api\DataDepoResponse::CODE_OK, NULL, array('processed' => $count));
 }