/** * @test */ public function get_report_status_will_return_a_status_object() { $client = $this->prophesize(Auth::class); $fakeResponse = $this->prophesize(Response::class); $stream = $this->prophesize(Stream::class); $stream->getContents()->willReturn($this->getReportResponse()); $fakeResponse->getBody()->willReturn($stream->reveal()); $client->request('GET', Report::BASE_URL_DOWNLOAD . 'a_url', Argument::any())->willReturn($fakeResponse); $report = new Report($client->reveal()); $reportStatus = new ReportStatus(); $reportStatus->setReportId(10); $reportStatus->setStatus(ReportStatus::STATUS_READY); $reportStatus->setUrl('a_url'); $report = $report->getReport($reportStatus); $this->assertEquals([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], $report[1]); }
/** * @param ReportStatus $reportStatus * * @return array * @throws ReportException */ public function getReport(ReportStatus $reportStatus) { if (!$reportStatus->isReady()) { throw ReportException::validation('report status not ready'); } if (!$reportStatus->getUrl()) { throw ReportException::validation('missing url in the report status'); } $cacheKey = self::CACHE_NAMESPACE . sha1($reportStatus->getUrl()); if ($this->isCacheEnabled()) { if ($this->cache->contains($cacheKey)) { return $this->cache->fetch($cacheKey); } } $compiledUrl = $this->baseUrlDownload . $reportStatus->getUrl(); $response = $this->client->request('GET', $compiledUrl); $lines = explode(PHP_EOL, $response->getBody()->getContents()); $result = []; foreach ($lines as $line) { if (!empty($line)) { $result[] = str_getcsv($line); } } if ($this->isCacheEnabled()) { $this->cache->save($cacheKey, $result, self::CACHE_EXPIRATION); } return $result; }