* initial commit
This commit is contained in:
59
src/App/Service/SmsStoreService.php
Normal file
59
src/App/Service/SmsStoreService.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
18
src/App/Service/SmsStoreServiceFactory.php
Normal file
18
src/App/Service/SmsStoreServiceFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
|
||||
class SmsStoreServiceFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $container->get('doctrine.entity_manager.orm_default');
|
||||
return new SmsStoreService($em);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user