|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2018 Alma / Nabla SAS |
| 4 | + * |
| 5 | + * THE MIT LICENSE |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and |
| 10 | + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 11 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 12 | + * Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 15 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 17 | + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 18 | + * IN THE SOFTWARE. |
| 19 | + * |
| 20 | + * @author Alma / Nabla SAS <contact@getalma.eu> |
| 21 | + * @copyright Copyright (c) 2018 Alma / Nabla SAS |
| 22 | + * @license https://opensource.org/licenses/MIT The MIT License |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +namespace Alma\API\Endpoints; |
| 27 | + |
| 28 | +use Alma\API\Entities\DataExport; |
| 29 | +use Alma\API\Exceptions\ParametersException; |
| 30 | +use Alma\API\Exceptions\RequestException; |
| 31 | +use Alma\API\ParamsError; |
| 32 | +use Alma\API\RequestError; |
| 33 | + |
| 34 | +class DataExports extends Base |
| 35 | +{ |
| 36 | + const DATA_EXPORTS_PATH = '/v1/data-exports'; |
| 37 | + const ACCEPTED_FORMAT = ['csv', 'xlsx']; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param $data |
| 41 | + * |
| 42 | + * @return DataExport |
| 43 | + * |
| 44 | + * @throws RequestException|RequestError |
| 45 | + */ |
| 46 | + public function create($data) |
| 47 | + { |
| 48 | + $res = $this->request(self::DATA_EXPORTS_PATH)->setRequestBody($data)->post(); |
| 49 | + |
| 50 | + if ($res->isError()) { |
| 51 | + throw new RequestException($res->errorMessage, null, $res); |
| 52 | + } |
| 53 | + |
| 54 | + return new DataExport($res->json); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param $reportId |
| 59 | + * |
| 60 | + * @return DataExport |
| 61 | + * |
| 62 | + * @throws RequestException|RequestError |
| 63 | + */ |
| 64 | + public function fetch($reportId) |
| 65 | + { |
| 66 | + $res = $this->request(self::DATA_EXPORTS_PATH . '/' . $reportId)->get(); |
| 67 | + |
| 68 | + if ($res->isError()) { |
| 69 | + throw new RequestException($res->errorMessage, null, $res); |
| 70 | + } |
| 71 | + |
| 72 | + return new DataExport($res->json); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param $reportId |
| 77 | + * |
| 78 | + * @param string $format only csv or xlsx |
| 79 | + * |
| 80 | + * @return mixed |
| 81 | + * |
| 82 | + * @throws RequestException|RequestError|ParametersException |
| 83 | + */ |
| 84 | + public function download($reportId, $format) |
| 85 | + { |
| 86 | + if (!in_array($format, self::ACCEPTED_FORMAT)) { |
| 87 | + throw new ParametersException("Invalid format: $format. Accepted format are: " . implode(', ', self::ACCEPTED_FORMAT)); |
| 88 | + } |
| 89 | + |
| 90 | + $res = $this->request(self::DATA_EXPORTS_PATH . '/' . $reportId) |
| 91 | + ->setQueryParams(['format' => $format]) |
| 92 | + ->get(); |
| 93 | + |
| 94 | + if ($res->isError()) { |
| 95 | + throw new RequestException($res->errorMessage, null, $res); |
| 96 | + } |
| 97 | + |
| 98 | + return $res->responseFile; |
| 99 | + } |
| 100 | +} |
0 commit comments