resq/src/Controller/ResqAPI/ServiceController.php

67 lines
1.8 KiB
PHP

<?php
namespace App\Controller\ResqAPI;
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\Service;
use App\Service\MobileAPIHandler;
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
class ServiceController extends APIController
{
protected $acl_gen;
public function __construct(ACLGenerator $acl_gen)
{
$this->acl_gen = $acl_gen;
}
public function listServices(Request $req, EntityManagerInterface $em,
MobileAPIHandler $mah)
{
$this->denyAccessUnlessGranted('mobile_service.list', null, 'No access.');
// check required parameters
$required_params = [];
$msg = $this->checkRequiredParameters($req, $required_params);
if ($msg)
return new APIResponse(false, $msg);
// get capi user to link to mobile user
$user_id = $this->getUser()->getID();
// get mobile user
$mobile_user = $mah->findMobileUser($user_id);
if ($mobile_user == null)
return new APIResponse(false, 'No mobile user found.');
// services
$results = $em->getRepository(Service::class)->findAll();
if (empty($results))
return new APIResponse(false, 'No services available');
$services = [];
foreach ($results as $result)
{
$services[] = [
'id' => $result->getID(),
'name' => $result->getName(),
];
}
$data['services'] = $services;
return new APIResponse(true, 'Services found', $data);
}
}