60 lines
1.1 KiB
PHP
60 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
|
||
|
|
namespace App\Service;
|
||
|
|
|
||
|
|
|
||
|
|
use App\Entity\Sms;
|
||
|
|
use App\Entity\User;
|
||
|
|
use Doctrine\ORM\EntityManager;
|
||
|
|
|
||
|
|
class SmsStoreService
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* @var EntityManager
|
||
|
|
*/
|
||
|
|
private $em;
|
||
|
|
|
||
|
|
public function __construct(EntityManager $em)
|
||
|
|
{
|
||
|
|
$this->em = $em;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function storeSms(string $hashKey, int $direction, array $requestData): bool
|
||
|
|
{
|
||
|
|
$user = $this->ensureUserExists($hashKey);
|
||
|
|
$sms = new Sms();
|
||
|
|
$sms->setDirection($direction)
|
||
|
|
->setContactName($requestData['contactName'])
|
||
|
|
->setContactNumber($requestData['contactNumber'])
|
||
|
|
->setWhen($requestData['when'])
|
||
|
|
->setOwner($user)
|
||
|
|
->setText($requestData['text']);
|
||
|
|
$this->em->persist($sms);
|
||
|
|
$this->em->flush();
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param string $hashKey
|
||
|
|
* @return User
|
||
|
|
*/
|
||
|
|
private function ensureUserExists(string $hashKey): User
|
||
|
|
{
|
||
|
|
/** @var User $user */
|
||
|
|
$user = $this->em->getRepository(User::class)->findOneBy([
|
||
|
|
'hashKey' => $hashKey
|
||
|
|
]);
|
||
|
|
|
||
|
|
if($user === null) {
|
||
|
|
$user = new User();
|
||
|
|
$user->setHashKey($hashKey);
|
||
|
|
$this->em->persist($user);
|
||
|
|
$this->em->flush();
|
||
|
|
}
|
||
|
|
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
}
|