PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/googleads-php-lib/src/Google/Api/Ads/Dfp/Util/v201608/ReportDownloader.php

https://github.com/markvince/CakePHP-GAStats-Plugin
PHP | 141 lines | 60 code | 14 blank | 67 comment | 9 complexity | bc88a14ab7514ea61c057f6bc0469413 MD5 | raw file
  1. <?php
  2. /**
  3. * Retrieves reports using a {@link ReportService}.
  4. *
  5. * PHP version 5
  6. *
  7. * Copyright 2016, Google Inc. All Rights Reserved.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * @package GoogleApiAdsDfp
  22. * @subpackage Util
  23. * @category WebServices
  24. * @copyright 2016, Google Inc. All Rights Reserved.
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
  26. * Version 2.0
  27. */
  28. require_once 'Google/Api/Ads/Common/Util/CurlUtils.php';
  29. /**
  30. * Retrieves reports using a {@link ReportService}.
  31. * @package GoogleApiAdsDfp
  32. * @subpackage Util
  33. */
  34. class ReportDownloader {
  35. /** The time to sleep in seconds before each request to the service. */
  36. const SLEEP_DURATION = 30;
  37. private $reportService;
  38. private $reportJobId;
  39. /**
  40. * Constructs a {@code ReportDownloader} object for a {@link ReportService}
  41. * and a report job id that the the class works on.
  42. *
  43. * @param ReportService $reportService an instance of the report service
  44. * @param float $reportJobId the report job ID
  45. */
  46. public function __construct($reportService, $reportJobId) {
  47. $this->reportService = $reportService;
  48. $this->reportJobId = $reportJobId;
  49. }
  50. /**
  51. * Blocks and waits for a report to be ready. When a report's job status is
  52. * received that is not 'PENDING' or 'IN_PROGRESS', the report is considered
  53. * finished, and the method is returned with a {@code true} if the report was
  54. * successful, or an {@code false} otherwise.
  55. *
  56. * @return bool {@code true} if the report was successful, {@code false}
  57. * otherwise
  58. */
  59. public function waitForReportReady() {
  60. $reportJobStatus =
  61. $this->reportService->getReportJobStatus($this->reportJobId);
  62. while ($reportJobStatus === 'IN_PROGRESS') {
  63. sleep(self::SLEEP_DURATION);
  64. $reportJobStatus =
  65. $this->reportService->getReportJobStatus($this->reportJobId);
  66. }
  67. return $reportJobStatus === 'COMPLETED';
  68. }
  69. /**
  70. * Downloads a Gzip report from an URL. to file located at {@code fileName}.
  71. * If the {@code filePath} is specified the report will be downloaded to the
  72. * file at that path, otherwise it will be downloaded to memory and
  73. * returned as a string.
  74. *
  75. * @param string $exportFormat the export format of the report
  76. * @param string $filePath an optional file path to download the report to
  77. * @return mixed report contents as a string if {@code filePath} isn't
  78. * specified, otherwise the size of the downloaded report in bytes
  79. * @throws InvalidArgumentException if the report download URL is invalid
  80. */
  81. public function downloadReport($exportFormat, $filePath = null) {
  82. $downloadUrl = $this->getDownloadUrl($exportFormat);
  83. $curlUtils = new CurlUtils();
  84. $ch = $curlUtils->CreateSession($downloadUrl);
  85. if (isset($filePath)) {
  86. $file = fopen($filePath, 'w');
  87. $curlUtils->SetOpt($ch, CURLOPT_FILE, $file);
  88. } else {
  89. $curlUtils->SetOpt($ch, CURLOPT_RETURNTRANSFER, 1);
  90. }
  91. $result = $curlUtils->Exec($ch);
  92. $httpCode = $curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE);
  93. $error = $curlUtils->Error($ch);
  94. $downloadSize = $curlUtils->GetInfo($ch, CURLINFO_SIZE_DOWNLOAD);
  95. $curlUtils->Close($ch);
  96. if (isset($file)) {
  97. fclose($file);
  98. }
  99. if ($httpCode != 200) {
  100. $message = sprintf('Invalid report download URL: %s', $downloadUrl);
  101. throw new InvalidArgumentException($message, $httpCode);
  102. }
  103. if (isset($filePath)) {
  104. return $downloadSize;
  105. } else {
  106. return $result;
  107. }
  108. }
  109. /**
  110. * Gets the download URL for a GZip or plain-text format report.
  111. *
  112. * @param string $exportFormat the export format of the report
  113. * @return string the URL for the report download
  114. * @throws ValidationException if the report is not completed
  115. */
  116. private function getDownloadUrl($exportFormat) {
  117. $reportJobStatus =
  118. $this->reportService->getReportJobStatus($this->reportJobId);
  119. if ($reportJobStatus !== 'COMPLETED') {
  120. throw new ValidationException('reportJobStatus', $reportJobStatus,
  121. sprintf('Report %d must be completed before downloading.',
  122. $this->reportJobId));
  123. }
  124. return $this->reportService->getReportDownloadURL($this->reportJobId,
  125. $exportFormat);
  126. }
  127. }