* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link http://www.temando.com/ */ class BatchRepository implements BatchRepositoryInterface { /** * @var BatchApiInterface */ private $apiAdapter; /** * @var ItemRequestInterfaceFactory */ private $batchRequestFactory; /** * @var BatchResponseMapper */ private $batchMapper; /** * BatchRepository constructor. * @param BatchApiInterface $apiAdapter * @param ItemRequestInterfaceFactory $batchRequestFactory * @param BatchResponseMapper $batchMapper */ public function __construct( BatchApiInterface $apiAdapter, ItemRequestInterfaceFactory $batchRequestFactory, BatchResponseMapper $batchMapper ) { $this->apiAdapter = $apiAdapter; $this->batchRequestFactory = $batchRequestFactory; $this->batchMapper = $batchMapper; } /** * @param string $batchId * @return BatchInterface * @throws NoSuchEntityException * @throws LocalizedException */ public function getById($batchId) { if (!$batchId) { throw new LocalizedException(__('An error occurred while loading data.')); } try { $request = $this->batchRequestFactory->create(['entityId' => $batchId]); $apiBatch = $this->apiAdapter->getBatch($request); $batch = $this->batchMapper->map($apiBatch); } catch (AdapterException $e) { if ($e->getCode() === 404) { throw NoSuchEntityException::singleField('batchId', $batchId); } throw new LocalizedException(__('An error occurred while loading data.'), $e); } return $batch; } }