81 lines
2.2 KiB
PHP
Executable File
81 lines
2.2 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Copyright © JR All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
|
|
namespace JR\ReportIntegration\Model;
|
|
|
|
use JR\ReportIntegration\Helper\Data as Helper;
|
|
use JR\ReportIntegration\Service\RequestService;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Customer\Api\CustomerRepositoryInterface;
|
|
|
|
class SetOfficeManagement implements \JR\ReportIntegration\Api\Model\SetOfficeManagementInterface
|
|
{
|
|
/**
|
|
* @var RequestService
|
|
*/
|
|
protected $requestService;
|
|
|
|
/**
|
|
* @var Helper
|
|
*/
|
|
protected $helper;
|
|
|
|
/**
|
|
* @var CustomerRepositoryInterface
|
|
*/
|
|
protected $customerRepository;
|
|
|
|
/**
|
|
* @param RequestService
|
|
* @param Helper
|
|
* @param CustomerRepositoryInterface
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(
|
|
RequestService $requestService,
|
|
Helper $helper,
|
|
CustomerRepositoryInterface $customerRepository
|
|
) {
|
|
$this->requestService = $requestService;
|
|
$this->helper = $helper;
|
|
$this->customerRepository = $customerRepository;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function postSetOffice($email, $secret, $id, $office)
|
|
{
|
|
return $this->requestService->post(
|
|
$email,
|
|
$secret,
|
|
[$this, 'setOffice'],
|
|
['id' => $id, 'office' => $office]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function setOffice($id, $office)
|
|
{
|
|
$result = [];
|
|
try {
|
|
$customer = $this->customerRepository->getById($id);
|
|
if ($this->helper->hasOfficeName($office)) {
|
|
$customer->setCustomAttribute('lts_office', $this->helper->getOfficeValueByShortName($office));
|
|
$this->customerRepository->save($customer);
|
|
$result[] = array('account' => $customer->getEmail(), 'error' => NULL);
|
|
} else {
|
|
throw new LocalizedException(__("'$office' is an invalid office."));
|
|
}
|
|
} catch (\Exception $e) {
|
|
$result[] = array('account' => isset($customer) ? $customer->getEmail() : $id, 'error' => $e->getMessage());
|
|
}
|
|
return $result;
|
|
}
|
|
} |