2020-03-16 14:19:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use App\Validator\ApiValidator;
|
|
|
|
use App\Entity\Hygrometry;
|
2020-03-24 23:47:46 +01:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2020-06-14 22:32:01 +02:00
|
|
|
use App\Repository\HygrometryRepository;
|
2020-03-16 14:19:25 +01:00
|
|
|
|
|
|
|
class HygrometryApiController extends AbstractController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Route("/api/hygrometry/create", name="api_hygrometry_create", methods={"POST"})
|
|
|
|
*/
|
2020-06-14 22:32:01 +02:00
|
|
|
public function create(Request $request, ApiValidator $validator, \Swift_Mailer $mailer, HygrometryRepository $repository): JsonResponse
|
2020-03-16 14:19:25 +01:00
|
|
|
{
|
|
|
|
if ('application/json' === $request->getContentType()) {
|
|
|
|
return $this->json([], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $request->getContent();
|
|
|
|
|
|
|
|
if (!$validator->isValidContent($content)) {
|
|
|
|
return $this->json([], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = json_decode($content, true);
|
|
|
|
|
|
|
|
$entity = new Hygrometry();
|
|
|
|
$entity
|
|
|
|
->setDate(new \DateTime($data['date']))
|
|
|
|
->setValue((float) $data['value']);
|
|
|
|
|
|
|
|
$manager = $this->getDoctrine()->getManager();
|
|
|
|
$manager->persist($entity);
|
|
|
|
$manager->flush();
|
|
|
|
|
2020-06-14 22:32:01 +02:00
|
|
|
if ($entity->getValue() >= 53) {
|
2020-03-21 15:26:36 +01:00
|
|
|
$message = (new \Swift_Message('[Terrarium] Alerte hygométrie'))
|
|
|
|
->setFrom('terrarium@deblan.fr')
|
|
|
|
->setTo(['simon@deblan.fr', 'jennifer@derago.fr'])
|
|
|
|
->setBody(
|
|
|
|
$this->renderView(
|
|
|
|
'alert/hygrometry.txt.twig',
|
|
|
|
[
|
|
|
|
'entity' => $entity,
|
2020-06-14 22:32:01 +02:00
|
|
|
'entities' => $repository->findByDateRange(
|
|
|
|
new \DateTime('now - 1 day'), new \DateTime('now'),
|
|
|
|
6,
|
|
|
|
'DESC'
|
|
|
|
),
|
2020-03-21 15:26:36 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$mailer->send($message);
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:19:25 +01:00
|
|
|
return $this->json([], 201);
|
|
|
|
}
|
|
|
|
}
|