Skip to content

Instantly share code, notes, and snippets.

@128keaton
Created October 2, 2017 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 128keaton/fe3d6fea1e30d15b4140d21618de1f4e to your computer and use it in GitHub Desktop.
Save 128keaton/fe3d6fea1e30d15b4140d21618de1f4e to your computer and use it in GitHub Desktop.
<?php
namespace TdatBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use TdatBundle\Entity\Category;
use TdatBundle\Entity\Device;
use TdatBundle\Entity\Manufacturer;
use TdatBundle\Entity\TestedDevice;
/**
* Class iOSController
*
* @package TdatBundle\Controller
* @Route("/ios")
*/
class iOSController extends Controller
{
/**
* @Route("/upload")
* @Method({"POST"})
*
* @param Request $request
*
* @return JsonResponse
*/
public function uploadAction(Request $request): JsonResponse
{
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
return $this->buildResponse($data);
}
return new JsonResponse(
[
'success' => 'false',
'reason' =>
'improper header',
]
);
}
/**
* @param array $json
*
* @return JsonResponse
* Builds a tested device from a json array
*/
public function buildResponse(array $json): JsonResponse
{
$em = $this->getDoctrine()->getManager();
$testedDevice = new TestedDevice();
/* Checks for Serial Number */
if (array_key_exists('serialNumber', $json)) {
$testedDevice->setSerialNumber($json['serialNumber']);
} else {
return new JsonResponse(
[
'success' => 'false',
'reason' =>
'no serial number',
]
);
}
/* Checks for Model Number */
if (array_key_exists('modelNumber', $json)) {
$testedDevice->setDevice($this->findDevice($json['modelNumber']));
} else {
return new JsonResponse(
[
'success' => 'false',
'reason' =>
'no model number',
]
);
}
/* Checks for Grade */
if (array_key_exists('grade', $json)) {
switch ($json['grade']) {
case 'a':
$testedDevice->setMethod(TestedDevice::TESTING_METHOD_FULL);
break;
case 'b':
$testedDevice->setMethod(TestedDevice::TESTING_METHOD_KEY);
break;
case 'c':
$testedDevice->setMethod(TestedDevice::TESTING_METHOD_REPAIR);
break;
case 'd':
$testedDevice->setMethod(TestedDevice::TESTING_METHOD_FAILED);
break;
}
} else {
$testedDevice->setMethod(TestedDevice::TESTING_METHOD_NONE);
}
/* Checks for any notes */
if (array_key_exists('listingNotes', $json)) {
$testedDevice->setListingNotes($json['listingNotes']);
}
if (array_key_exists('notes', $json)) {
$testedDevice->setNotes($json['notes']);
}
$em->persist($testedDevice);
$em->flush();
return new JsonResponse(['success' => 'true']);
}
/**
* @Route("/upload")
*
* @Method({"GET"})
*
* Redirects to devices when iOS page is accessed in browser.
*/
public function redirectToDevicesAction()
{
return $this->redirect($this->generateUrl('tdat_device_view'));
}
/**
* @return Manufacturer
*
* Finds the 'Apple' Manufacturer
*/
public function getManufacturer(): Manufacturer
{
$repository = $this->getDoctrine()->getRepository(Manufacturer::class);
$manufacturer = $repository->findBy(['name' => 'Apple']);
return $manufacturer[0];
}
/**
* @return Category
*
* Finds the 'Wireless Phone' Category
*/
public function getCategory(): Category
{
$repository = $this->getDoctrine()->getRepository(Category::class);
$category = $repository->findBy(['name' => 'Wireless Phone']);
return $category[0];
}
/**
* @param String $model
*
* @return Device
*/
public function findDevice(String $model): Device
{
$repository = $this->getDoctrine()->getRepository(Device::class);
$devices = $repository->findBy(['model' => $model]);
if (count($devices) > 0) {
return $devices[0];
}
return $this->buildDevice($model);
}
/**
* @param String $model
*
* @return Device
*/
public function buildDevice(String $model): Device
{
$em = $this->getDoctrine()->getManager();
$device = new Device();
$device->setManufacturer($this->getManufacturer());
$device->setModel($model);
$device->setCategory($this->getCategory());
$device->setTestingDocument('Please use the TOOT iOS Application for testing iOS devices. - Keaton Burleson');
$em->persist($device);
$em->flush();
return $device;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment