protected function getFileUrl($response, $attempt = 0) { Logger::log("debug", "Retrieving report file URL (attempt = {$attempt}).", Utils::to_assoc($response)); switch ($response->status) { case "REPORT_AVAILABLE": Logger::log("debug", "Report {$this->config["reportId"]} is ready! URL: {$response->urls->apiUrl}", Utils::to_assoc($response)); if ($response->format != "CSV") { throw new UserException("[415] The report {$this->config["reportId"]} export format is not CSV."); } $result = $response->urls->apiUrl; break; case "PROCESSING": if ($attempt > 10) { throw new UserException("The report export timed out."); } $attempt++; $sleep = pow(2, $attempt); Logger::log("debug", "Report {$this->config["reportId"]} is still not ready (status = {$response->status}). Waiting for {$sleep}s."); sleep($sleep); $result = $this->getFileUrl($this->download($this->client->createRequest("GET", "userprofiles/{$this->config["profileId"]}/reports/{$this->config["reportId"]}/files/{$response->id}")), $attempt); break; default: throw new SyrupComponentException(500, "Unknown report status {$response->status}"); } return $result; }
/** * Parse the data * @param array|object $data shall be the response body * @param string $type is a WSDL data type (has to be obtained from the WSDL definition) * @param string $path a path to the results list(the array containing each record) within the response * @param string $parent used internally for naming child arrays/columns * @param string $parentId used internally to link child objects to parent */ public function parse($data, $type, $path = null, $parent = null, $parentId = null) { if (!empty($path)) { $data = Utils::getDataFromPath($path, $data); } $fileName = $type; if (empty($this->csvFiles[$fileName])) { $header = array_keys($this->struct[$type]); if ($parentId) { array_push($header, "WSDL_parentId"); } $this->csvFiles[$fileName] = Table::create($fileName, $header, $this->getTemp()); } $handle = $this->csvFiles[$fileName]; $struct = $this->struct[$type]; foreach (Utils::to_assoc($data) as $record) { $row = []; foreach ($struct as $key => $valueType) { if (empty($record[$key])) { $row[$key] = null; } elseif (in_array($valueType, $this->stdTypes)) { $row[$key] = (string) $record[$key]; } elseif (array_key_exists($valueType, $this->struct)) { // Walk through the data type and parse children foreach ($this->struct[$valueType] as $attr => $attrType) { $childId = $type . "_" . $attrType . "_" . (!empty($row["id"]) ? $row["id"] : uniqid()); $row[$key] = $childId; $childPath = "{$key}/{$attr}"; $this->parse($record, $attrType, $childPath, $type, $childId); } } else { $row[$key] = null; } } // FIXME set this in the data before actually caling the fn if ($parentId) { $row["WSDL_parentId"] = $parentId; } $handle->writeRow($row); } }