Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Action/Patient/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public function __invoke(Request $request, PatientManager $pm, QuestionManager $
$parameters->get('upload_files_to_server') === 'true' ? true : false
), GenericEvents::FILE_UPLOAD);


return $this->jsonResponse(
Response::HTTP_CREATED,
'Patient resource add success'
Expand Down
5 changes: 4 additions & 1 deletion src/Action/Sms/SendVerificationSMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Action\BaseAction;
use App\Dto\Phone;
use App\Form\PhoneType;
use App\Manager\SMSManager;
use App\Service\TTSMSing;
use App\Util\Tools;
use Exception;
Expand Down Expand Up @@ -49,7 +50,7 @@ class SendVerificationSMS extends BaseAction
* @param TTSMSing $ttSMSing
* @return View|FormInterface
*/
public function __invoke(Request $request, TTSMSing $ttSMSing)
public function __invoke(Request $request, TTSMSing $ttSMSing, SMSManager $sm)
{
$phone = new Phone();

Expand All @@ -70,6 +71,8 @@ public function __invoke(Request $request, TTSMSing $ttSMSing)
);
}

$sm->newSMS($verificationCode, $phone->getNumber());

return $this->jsonResponse(
Response::HTTP_OK,
'Verification SMS successfully sent to patient',
Expand Down
86 changes: 86 additions & 0 deletions src/Action/Sms/VerifySMS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Action\Sms;

use App\Action\BaseAction;
use App\Dto\Phone;
use App\Dto\VerificationCodeDto;
use App\Form\PhoneType;
use App\Form\VerificationCodeType;
use App\Manager\SMSManager;
use App\Service\TTSMSing;
use App\Util\Tools;
use Exception;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Class VerifySMS
*
* @author Ghassen Karray <ghassen.karray@epfl.ch>
*/
class VerifySMS extends BaseAction
{
/**
* Check if verification sms entered by the patient is valid
*
*
* @Rest\Post("/api/v1/sms/authentication/check")
*
* @SWG\Parameter(
* name="verificationCode",
* in="body",
* required=true,
* @Model(type=VerificationCodeType::class)
* )
*
* @SWG\Response(response=200, description="Verification code check success")
* @SWG\Response(response=400, description="Validation Failed")
* @SWG\Response(response=500, description="No SMS found for this phone number")
*
* @SWG\Tag(name="SMS")
*
* @Rest\View()
* @param Request $request
* @param SMSManager $sm
* @return View|FormInterface
*/
public function __invoke(Request $request, SMSManager $sm)
{
$verificationCode = new VerificationCodeDto();

$form = $this->createForm(VerificationCodeType::class, $verificationCode);
$form->submit($request->request->all());
if (!$form->isValid()) {
return $form;
}

try{
$sms = $sm->getByPhoneNumber($verificationCode->getNumber());
} catch (Exception $exception) {
return $this->jsonResponse(
Response::HTTP_INTERNAL_SERVER_ERROR,
$exception->getMessage()
);
}

if($sms->getVerificationCode() != $verificationCode->getCode()) {
return $this->jsonResponse(
Response::HTTP_BAD_REQUEST,
"Validation Failed"
);
}

$sm->remove($sms);

return $this->jsonResponse(
Response::HTTP_OK,
'Verification code check success'
);
}
}
74 changes: 74 additions & 0 deletions src/Dto/VerificationCodeDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Dto;

use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;

/**
* Class VerificationCodeDto
*
* @author Karray Ghassen <ghassen.karray@epfl.ch>
*/
class VerificationCodeDto
{
/**
* @var string content
*
* @ORM\Column(type="string")
*
* @Assert\NotBlank
* @Assert\Length(
* min = 6,
* max = 6,
* exactMessage="The verification code should have exactly {{ limit }} characters"
* )
*/
private $code;

/**
* @var int number
*
* @ORM\Column(type="integer")
*
* @Assert\NotBlank
* @Assert\Length(
* min = 8,
* max = 8,
* exactMessage="The verification code should have exactly {{ limit }} characters"
* )
*/
private $number;

/**
* @return string|null
*/
public function getCode(): ?string
{
return $this->code;
}

/**
* @param string $content
*/
public function setCode(string $code): void
{
$this->code = $code;
}

/**
* @return int|null
*/
public function getNumber(): ?int
{
return $this->number;
}

/**
* @param int $number
*/
public function setNumber(int $number): void
{
$this->number = $number;
}
}
67 changes: 67 additions & 0 deletions src/Entity/VerificationSMS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity(repositoryClass="App\Repository\VerificationSMSRepository")
*/
class VerificationSMS
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;

/**
* @var int The patient phone number
*
* @ORM\Column(type="integer")
*
* @Assert\NotBlank
* @Assert\Length(
* min = 8,
* max = 8,
* exactMessage="The phone number should have exactly {{ limit }} characters"
* )
*/
private $phoneNumber;

/**
* @ORM\Column(type="string", length=6)
*/
private $verificationCode;

public function getId(): ?int
{
return $this->id;
}

public function getPhoneNumber(): ?int
{
return $this->phoneNumber;
}

public function setPhoneNumber(int $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;

return $this;
}

public function getVerificationCode(): ?string
{
return $this->verificationCode;
}

public function setVerificationCode(string $verificationCode): self
{
$this->verificationCode = $verificationCode;

return $this;
}
}
31 changes: 31 additions & 0 deletions src/Form/VerificationCodeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Form;

use App\Dto\VerificationCodeDto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class VerificationCodeType
*
* @author Karray Ghassen <ghassen.karray@epfl.ch>
*/
class VerificationCodeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code')
->add('number', IntegerType::class);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => VerificationCodeDto::class,
]);
}
}
65 changes: 65 additions & 0 deletions src/Manager/SMSManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Manager;

use App\Entity\VerificationSMS;
use Doctrine\ORM\EntityManagerInterface;

/**
* Class SMSManager
*
* @author Karray Ghassen <ghassen.karray@epfl.ch>
*/
class SMSManager
{
/**
* @var EntityManagerInterface
*/
private $em;

/**
* SMSManager constructor.
*
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

/**
* create a new verification sms entry
*
* @param string $code
* @param int $phone
*/
public function newSMS(string $code, int $phone): void
{
$verificationSms = new VerificationSMS();
$verificationSms->setPhoneNumber($phone);
$verificationSms->setVerificationCode($code);

$this->em->persist($verificationSms);
$this->em->flush();
}

public function getByPhoneNumber(int $number): VerificationSMS
{
$sms = $this->em->getRepository(VerificationSMS::class)->findOneBy([
"phoneNumber"=>$number
]);

if($sms) {
return $sms ;
} else {
throw new \Exception("No SMS found for this phone number");
}
}

public function remove(VerificationSMS $sms): void
{
$this->em->remove($sms);
$this->em->flush();
}

}
Loading