51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Infrastructure\Persistence\Repository;
|
||
|
|
|
||
|
|
use App\Domain\Order\Customer;
|
||
|
|
use App\Domain\Order\Repository\CustomerRepositoryInterface;
|
||
|
|
use Doctrine\ORM\EntityManagerInterface;
|
||
|
|
use Symfony\Component\Uid\Uuid;
|
||
|
|
|
||
|
|
final class DoctrineCustomerRepository implements CustomerRepositoryInterface
|
||
|
|
{
|
||
|
|
public function __construct(private readonly EntityManagerInterface $em) {}
|
||
|
|
|
||
|
|
public function findById(Uuid $id): ?Customer
|
||
|
|
{
|
||
|
|
return $this->em->find(Customer::class, $id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function findByPlatformId(string $platform, string $platformUserId): ?Customer
|
||
|
|
{
|
||
|
|
return $this->em->getRepository(Customer::class)
|
||
|
|
->createQueryBuilder('c')
|
||
|
|
->where("JSON_UNQUOTE(JSON_EXTRACT(c.platformIds, :path)) = :id")
|
||
|
|
->setParameter('path', '$."'.$platform.'"')
|
||
|
|
->setParameter('id', $platformUserId)
|
||
|
|
->setMaxResults(1)
|
||
|
|
->getQuery()
|
||
|
|
->getOneOrNullResult();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function findByMatchingKey(string $matchingKey): ?Customer
|
||
|
|
{
|
||
|
|
$customers = $this->em->getRepository(Customer::class)->findAll();
|
||
|
|
foreach ($customers as $customer) {
|
||
|
|
if ($customer->getMatchingKey() === $matchingKey) {
|
||
|
|
return $customer;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function save(Customer $customer): void
|
||
|
|
{
|
||
|
|
$this->em->persist($customer);
|
||
|
|
$this->em->flush();
|
||
|
|
}
|
||
|
|
}
|