Add warranty entity, refactor controller, and fix bugs #164
This commit is contained in:
parent
4309fea954
commit
959d2c6efc
8 changed files with 284 additions and 35 deletions
|
|
@ -32,9 +32,20 @@ class TestCommand extends Command
|
||||||
$api_key = $input->getArgument('api_key');
|
$api_key = $input->getArgument('api_key');
|
||||||
$secret_key = $input->getArgument('secret_key');
|
$secret_key = $input->getArgument('secret_key');
|
||||||
|
|
||||||
|
// api client
|
||||||
$api = new APIClient($server, $api_key, $secret_key);
|
$api = new APIClient($server, $api_key, $secret_key);
|
||||||
$api->setProtocol($protocol);
|
$api->setProtocol($protocol);
|
||||||
|
|
||||||
|
// test
|
||||||
$api->get('/capi/test');
|
$api->get('/capi/test');
|
||||||
|
|
||||||
|
// warranty register
|
||||||
|
$params = [
|
||||||
|
];
|
||||||
|
$api->post('/capi/warranty', $params);
|
||||||
|
|
||||||
|
// warranty find
|
||||||
|
$api->get('/capi/warranty/LJ34LJADR12SDLKJL');
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,23 +47,35 @@ class Client
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($url, $params = [])
|
protected function getDateString()
|
||||||
{
|
{
|
||||||
$date = new DateTime();
|
$date = new DateTime();
|
||||||
$date_string = $date->format(self::DATE_FORMAT);
|
return $date->format(self::DATE_FORMAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($url, $params = [])
|
||||||
|
{
|
||||||
|
curl_reset($this->curl);
|
||||||
|
|
||||||
|
$date_string = $this->getDateString();
|
||||||
$headers = $this->generateHeaders('GET', $url, $date_string);
|
$headers = $this->generateHeaders('GET', $url, $date_string);
|
||||||
|
|
||||||
|
// build query string
|
||||||
|
if (count($params) > 0)
|
||||||
|
$query_string = '?' . http_build_query($params);
|
||||||
|
else
|
||||||
|
$query_string = '';
|
||||||
|
|
||||||
// build url
|
// build url
|
||||||
if ($this->port == null)
|
if ($this->port == null)
|
||||||
$full_url = $this->protocol . '://' . $this->server . $url;
|
$full_url = $this->protocol . '://' . $this->server . $url . $query_string;
|
||||||
else
|
else
|
||||||
$full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url;
|
$full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url . $query_string;
|
||||||
|
|
||||||
error_log($full_url);
|
error_log($full_url);
|
||||||
|
|
||||||
// curl
|
// curl
|
||||||
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
|
// curl_setopt($this->curl, CURLOPT_VERBOSE, true);
|
||||||
curl_setopt($this->curl, CURLOPT_URL, $full_url);
|
curl_setopt($this->curl, CURLOPT_URL, $full_url);
|
||||||
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
|
||||||
curl_setopt($this->curl, CURLOPT_USERAGENT, self::USER_AGENT);
|
curl_setopt($this->curl, CURLOPT_USERAGENT, self::USER_AGENT);
|
||||||
|
|
@ -74,8 +86,36 @@ class Client
|
||||||
error_log($res);
|
error_log($res);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function post($params)
|
public function post($url, $params = [])
|
||||||
{
|
{
|
||||||
|
curl_reset($this->curl);
|
||||||
|
|
||||||
|
$date_string = $this->getDateString();
|
||||||
|
$headers = $this->generateHeaders('POST', $url, $date_string);
|
||||||
|
|
||||||
|
// build query string
|
||||||
|
$query_string = http_build_query($params);
|
||||||
|
|
||||||
|
// build url
|
||||||
|
if ($this->port == null)
|
||||||
|
$full_url = $this->protocol . '://' . $this->server . $url;
|
||||||
|
else
|
||||||
|
$full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url;
|
||||||
|
|
||||||
|
error_log($full_url);
|
||||||
|
|
||||||
|
// curl
|
||||||
|
// curl_setopt($this->curl, CURLOPT_VERBOSE, true);
|
||||||
|
curl_setopt($this->curl, CURLOPT_URL, $full_url);
|
||||||
|
curl_setopt($this->curl, CURLOPT_POST, true);
|
||||||
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $query_string);
|
||||||
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
curl_setopt($this->curl, CURLOPT_USERAGENT, self::USER_AGENT);
|
||||||
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, 0);
|
||||||
|
|
||||||
|
$res = curl_exec($this->curl);
|
||||||
|
error_log($res);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateSignature($method, $url, $date_string)
|
protected function generateSignature($method, $url, $date_string)
|
||||||
|
|
@ -88,6 +128,8 @@ class Client
|
||||||
];
|
];
|
||||||
$sig_source = implode('|', $creds);
|
$sig_source = implode('|', $creds);
|
||||||
|
|
||||||
|
error_log('SIG SOURCE - ' . $sig_source);
|
||||||
|
|
||||||
$raw_sig = hash_hmac('sha1', $sig_source, $this->secret_key, true);
|
$raw_sig = hash_hmac('sha1', $sig_source, $this->secret_key, true);
|
||||||
$enc_sig = base64_encode($raw_sig);
|
$enc_sig = base64_encode($raw_sig);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ class APIKeyAuthenticator implements SimplePreAuthenticatorInterface, Authentica
|
||||||
'date' => $hdate_string,
|
'date' => $hdate_string,
|
||||||
'signature' => $sig,
|
'signature' => $sig,
|
||||||
'method' => $req->getRealMethod(),
|
'method' => $req->getRealMethod(),
|
||||||
'uri' => $req->getRequestUri(),
|
'uri' => $req->getPathInfo(),
|
||||||
];
|
];
|
||||||
|
|
||||||
return new PreAuthenticatedToken(
|
return new PreAuthenticatedToken(
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Controller;
|
|
||||||
|
|
||||||
use App\Ramcar\BaseController;
|
|
||||||
use App\Entity\VehicleManufacturer;
|
|
||||||
|
|
||||||
use Doctrine\ORM\Query;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
||||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
||||||
|
|
||||||
use App\Menu\Generator as MenuGenerator;
|
|
||||||
use App\Access\Generator as ACLGenerator;
|
|
||||||
|
|
||||||
use Catalyst\APIBundle\Controller\APIController;
|
|
||||||
|
|
||||||
class APITestController extends BaseController implements APIController
|
|
||||||
{
|
|
||||||
public function test()
|
|
||||||
{
|
|
||||||
$data = [
|
|
||||||
'success' => true,
|
|
||||||
];
|
|
||||||
return new JsonResponse($data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
38
src/Controller/CAPI/BatteryController.php
Normal file
38
src/Controller/CAPI/BatteryController.php
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller\CAPI;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
use Catalyst\APIBundle\Controller\APIController;
|
||||||
|
|
||||||
|
use App\Entity\Battery;
|
||||||
|
|
||||||
|
class BatteryController extends Controller implements APIController
|
||||||
|
{
|
||||||
|
public function find($serial, EntityManagerInterface $em)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function claim($serial)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'success' => true,
|
||||||
|
];
|
||||||
|
return new JsonResponse($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/Controller/CAPI/TestController.php
Normal file
20
src/Controller/CAPI/TestController.php
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller\CAPI;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
|
use Catalyst\APIBundle\Controller\APIController;
|
||||||
|
use Catalyst\APIBundle\Response\APIResponse;
|
||||||
|
|
||||||
|
class TestController extends Controller implements APIController
|
||||||
|
{
|
||||||
|
public function test()
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'status' => 'Test successful.',
|
||||||
|
];
|
||||||
|
return new APIResponse(true, 'Test successful.', $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
85
src/Controller/CAPI/WarrantyController.php
Normal file
85
src/Controller/CAPI/WarrantyController.php
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller\CAPI;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Catalyst\APIBundle\Controller\APIController;
|
||||||
|
use Catalyst\APIBundle\Response\APIResponse;
|
||||||
|
use App\Entity\Warranty;
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
class WarrantyController extends Controller implements APIController
|
||||||
|
{
|
||||||
|
protected function cleanSerial($serial)
|
||||||
|
{
|
||||||
|
return trim(strtoupper($serial));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find($serial, EntityManagerInterface $em)
|
||||||
|
{
|
||||||
|
$clean_serial = $this->cleanSerial($serial);
|
||||||
|
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]);
|
||||||
|
|
||||||
|
if ($warr == null)
|
||||||
|
return new APIResponse(false, 'No warranty found with that serial number.', null, 404);
|
||||||
|
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'warranty' => [
|
||||||
|
'id' => (int) $warr->getID(),
|
||||||
|
'serial' => (string) $warr->getSerial(),
|
||||||
|
'date_create' => (string) $warr->getDateCreate()->format('YmdHis'),
|
||||||
|
'date_expire' => (string) $warr->getDateExpire()->format('Ymd'),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
return new APIResponse(true, 'Warranty found.', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register(Request $req, EntityManagerInterface $em)
|
||||||
|
{
|
||||||
|
// required parameters
|
||||||
|
$serial = $req->request->get('serial');
|
||||||
|
$date_expire_string = $req->request->get('date_expire');
|
||||||
|
|
||||||
|
// missing serial
|
||||||
|
if ($serial == null)
|
||||||
|
return new APIResponse(false, 'Required parameter missing: serial.');
|
||||||
|
|
||||||
|
// missing date expire
|
||||||
|
if ($date_expire_string == null)
|
||||||
|
return new APIResponse(false, 'Required parameter missing: date_expire.');
|
||||||
|
|
||||||
|
// wrong date format
|
||||||
|
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
|
||||||
|
if ($date_expire === false)
|
||||||
|
return new APIResponse(false, 'Wrong date format: date_expire.');
|
||||||
|
|
||||||
|
// warranty
|
||||||
|
$warr = new Warranty();
|
||||||
|
$warr->setSerial($serial)
|
||||||
|
->setDateExpire($date_expire);
|
||||||
|
|
||||||
|
$em->persist($warr);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
// data
|
||||||
|
$data = [
|
||||||
|
'warranty' => [
|
||||||
|
'id' => (int) $warr->getID(),
|
||||||
|
'serial' => (string) $warr->getSerial(),
|
||||||
|
'date_create' => (string) $warr->getDateCreate(),
|
||||||
|
'date_expire' => (string) $warr->getDateExpire(),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
return new APIResponse(true, 'Warranty registered.', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function claim($serial)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
81
src/Entity/Warranty.php
Normal file
81
src/Entity/Warranty.php
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Entity;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Entity
|
||||||
|
* @ORM\Table(
|
||||||
|
* name="warranty",
|
||||||
|
* uniqueConstraints={
|
||||||
|
* @ORM\UniqueConstraint(columns={"serial"})
|
||||||
|
* }
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class Warranty
|
||||||
|
{
|
||||||
|
// unique id
|
||||||
|
/**
|
||||||
|
* @ORM\Id
|
||||||
|
* @ORM\Column(type="integer")
|
||||||
|
* @ORM\GeneratedValue(strategy="AUTO")
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
|
// serial number
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=50)
|
||||||
|
*/
|
||||||
|
protected $serial;
|
||||||
|
|
||||||
|
// date created
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="datetime")
|
||||||
|
*/
|
||||||
|
protected $date_create;
|
||||||
|
|
||||||
|
// date expires
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="date")
|
||||||
|
*/
|
||||||
|
protected $date_expire;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->date_create = new DateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getID()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSerial($serial)
|
||||||
|
{
|
||||||
|
$this->serial = $serial;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSerial()
|
||||||
|
{
|
||||||
|
return $this->serial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDateCreate()
|
||||||
|
{
|
||||||
|
return $this->date_create;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDateExpire(DateTime $date)
|
||||||
|
{
|
||||||
|
$this->date_expire = $date;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDateExpire()
|
||||||
|
{
|
||||||
|
return $this->date_expire;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue