140 lines
3.7 KiB
PHP
140 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\CustomerAppAPI;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
|
|
|
use App\Ramcar\CustomerSource;
|
|
use App\Entity\Customer;
|
|
use App\Entity\PrivacyPolicy;
|
|
use App\Service\HashGenerator;
|
|
|
|
class CustomerController extends ApiController
|
|
{
|
|
public function getInfo(Request $req)
|
|
{
|
|
// validate params
|
|
$this->validateRequest($req);
|
|
|
|
// if no customer found
|
|
$cust = $this->session->getCustomer();
|
|
if ($cust == null) {
|
|
return new ApiResponse(true, '', [
|
|
'first_name' => '',
|
|
'last_name' => '',
|
|
'priv_third_party' => (bool) false,
|
|
'priv_promo' => (bool) false,
|
|
]);
|
|
}
|
|
|
|
// send back customer details
|
|
return new ApiResponse(true, '', [
|
|
'first_name' => $cust->getFirstName(),
|
|
'last_name' => $cust->getLastName(),
|
|
'priv_third_party' => (bool) $cust->getPrivacyThirdParty(),
|
|
'priv_promo' => (bool) $cust->getPrivacyPromo(),
|
|
]);
|
|
}
|
|
|
|
public function updateInfo(Request $req)
|
|
{
|
|
// validate params
|
|
$this->validateRequest($req, [
|
|
'first_name',
|
|
'last_name',
|
|
]);
|
|
|
|
$cust = $this->updateCustomerInfo($req);
|
|
|
|
$policy_mobile_id = $_ENV['POLICY_MOBILE'];
|
|
$mobile_policy = $this->em->getRepository(PrivacyPolicy::class)->find($policy_mobile_id);
|
|
|
|
// set policy id
|
|
if ($mobile_policy != null) {
|
|
$cust->setPrivacyPolicyMobile($mobile_policy);
|
|
}
|
|
|
|
$this->em->flush();
|
|
|
|
// response
|
|
return new ApiResponse();
|
|
}
|
|
|
|
public function getStatus(Request $req)
|
|
{
|
|
// validate params
|
|
$this->validateRequest($req);
|
|
|
|
// set data
|
|
$data = [];
|
|
if ($this->session->isConfirmed()) {
|
|
$data['status'] = 'confirmed';
|
|
} else {
|
|
$data['status'] = 'unconfirmed';
|
|
}
|
|
|
|
return new ApiResponse(true, '', $data);
|
|
}
|
|
|
|
public function updateDeviceID(Request $req)
|
|
{
|
|
// validate params
|
|
$this->validateRequest($req, [
|
|
'device_id',
|
|
]);
|
|
|
|
$device_id = $req->request->get('device_id');
|
|
$this->session->setDevicePushID($device_id);
|
|
|
|
$this->em->flush();
|
|
|
|
// response
|
|
return new ApiResponse();
|
|
}
|
|
|
|
protected function updateCustomerInfo(Request $req)
|
|
{
|
|
// create new customer if it's not there
|
|
$cust = $this->session->getCustomer();
|
|
if ($cust == null) {
|
|
$cust = new Customer();
|
|
|
|
// set customer source
|
|
$cust->setCreateSource(CustomerSource::MOBILE);
|
|
$this->em->persist($cust);
|
|
|
|
$this->session->setCustomer($cust);
|
|
}
|
|
|
|
$cust->setFirstName($req->request->get('first_name'))
|
|
->setLastName($req->request->get('last_name'))
|
|
->setEmail($req->request->get('email', ''))
|
|
->setConfirmed($this->session->isConfirmed());
|
|
|
|
// update mobile phone of customer
|
|
$cust->setPhoneMobile(substr($this->session->getPhoneNumber(), 2));
|
|
|
|
return $cust;
|
|
}
|
|
|
|
public function getCustomerHash(Request $req, HashGenerator $hash)
|
|
{
|
|
// validate params
|
|
$this->validateRequest($req);
|
|
|
|
// get customer
|
|
$cust = $this->session->getCustomer();
|
|
if ($cust == null) {
|
|
return new ApiResponse(false, 'No customer information found.');
|
|
}
|
|
|
|
// hash customer id
|
|
$hashed_id = $hash->getHash($cust->getID());
|
|
|
|
// response
|
|
return new ApiResponse(true, '', [
|
|
'cust_hash' => $hashed_id,
|
|
]);
|
|
}
|
|
}
|