/**
  * Return the right vendor for an operation
  *
  * @param OperationInterface $operation
  *
  * @return VendorInterface|null
  */
 protected function getVendor(OperationInterface $operation)
 {
     if ($operation->getMiraklId()) {
         return $this->vendorManager->findByMiraklId($operation->getMiraklId());
     }
     return $this->operator;
 }
 /**
  * Create the vendor operation
  * dispatch <b>after.operation.create</b>.
  *
  * @param int $amount
  * @param DateTime $cycleDate
  * @param string $paymentVoucher
  * @param bool|int $miraklId false if it an operator operation
  *
  * @return OperationInterface|null
  */
 public function createOperation($amount, DateTime $cycleDate, $paymentVoucher, $miraklId = null)
 {
     if ($amount <= 0) {
         $this->logger->notice("Operation wasn't created du to null amount");
         return null;
     }
     //Call implementation function
     $operation = $this->operationManager->create($amount, $cycleDate, $paymentVoucher, $miraklId);
     //Set hipay id
     $hipayId = null;
     if ($miraklId) {
         $vendor = $this->vendorManager->findByMiraklId($miraklId);
         if ($vendor) {
             $hipayId = $vendor->getHiPayId();
         }
     } else {
         $hipayId = $this->operator->getHiPayId();
     }
     $operation->setHiPayId($hipayId);
     //Sets mandatory values
     $operation->setMiraklId($miraklId);
     $operation->setStatus(new Status(Status::CREATED));
     $operation->setUpdatedAt(new DateTime());
     $operation->setAmount($amount);
     $operation->setCycleDate($cycleDate);
     $operation->setPaymentVoucher($paymentVoucher);
     return $operation;
 }
 /**
  * Transfer the files from Mirakl to HiPay using REST endpoint.
  *
  * @param array $shopIds
  * @param $tmpFilePath
  * @throws Exception
  */
 public function transferFiles(array $shopIds, $tmpFilePath)
 {
     if (count($shopIds) > 0) {
         // Fetches all Mirakl file names
         $allMiraklFiles = array();
         foreach (array_chunk($shopIds, 50) as $someShopIds) {
             $allMiraklFiles = array_merge($allMiraklFiles, $this->mirakl->getFiles($someShopIds));
         }
         $docTypes = $this->documentTypes;
         // We only keep the files with types we know
         $files = array_filter($allMiraklFiles, function ($aFile) use($docTypes) {
             return in_array($aFile['type'], array_keys($docTypes));
         });
         foreach ($shopIds as $shopId) {
             $this->logger->info('Will check files for Mirakl shop ' . $shopId);
             // Fetches documents already sent to HiPay Wallet
             $vendor = $this->vendorManager->findByMiraklId($shopId);
             $documents = $this->documentManager->findByVendor($vendor);
             // Keep Mirakl files for this shop only
             $theFiles = array_filter($files, function ($file) use($shopId) {
                 return $file['shop_id'] == $shopId;
             });
             $this->logger->info('Found ' . count($theFiles) . ' files on Mirakl for shop ' . $shopId);
             // Check all files for current shop
             foreach ($theFiles as $theFile) {
                 $filesAlreadyUploaded = array_values(array_filter($documents, function (DocumentInterface $document) use($theFile) {
                     return $document->getDocumentType() == $theFile['type'] && $document->getMiraklDocumentId() == $theFile['id'];
                 }));
                 // File not uploaded (or outdated)
                 if (count($filesAlreadyUploaded) === 0) {
                     $this->logger->info('Document ' . $theFile['id'] . ' (type: ' . $theFile['type'] . ') for Mirakl for shop ' . $shopId . ' is not uploaded or not up to date. Will upload');
                     $validityDate = null;
                     if (in_array($this->documentTypes[$theFile['type']], array(HiPay::DOCUMENT_SOLE_BUS_IDENTITY, HiPay::DOCUMENT_INDIVIDUAL_IDENTITY, HiPay::DOCUMENT_LEGAL_IDENTITY_OF_REPRESENTATIVE))) {
                         $validityDate = new DateTime('+1 year');
                     }
                     $tmpFile = $tmpFilePath . '/mirakl_kyc_downloaded_file.tmp';
                     file_put_contents($tmpFile, $this->mirakl->downloadDocuments(array($theFile['id'])));
                     try {
                         $this->hipay->uploadDocument($vendor->getHiPayUserSpaceId(), $this->documentTypes[$theFile['type']], $tmpFile, $validityDate);
                         $newDocument = $this->documentManager->create($theFile['id'], new \DateTime($theFile['date_uploaded']), $theFile['type'], $vendor);
                         $this->documentManager->save($newDocument);
                         $this->logger->info('Upload done. Document saved with ID: ' . $newDocument->getId());
                     } catch (ClientErrorResponseException $e) {
                         try {
                             $message = 'The document ' . $theFile['type'] . ' for Mirakl shop ' . $shopId . ' could not be uploaded to HiPay Wallet for the following reason: ';
                             $this->logger->critical($message . $e->getMessage() . ' - ' . ($e->getResponse() !== null ? $e->getResponse()->getBody(true) : ''));
                         } catch (\Exception $ex) {
                             throw $ex;
                         }
                     }
                 } else {
                     $this->logger->info('Document ' . $theFile['id'] . ' (type: ' . $theFile['type'] . ') for Mirakl for shop ' . $shopId . ' is already uploaded with ID ' . $filesAlreadyUploaded[0]->getId());
                 }
             }
         }
     }
 }