206 lines
5.9 KiB
PHP
206 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Entity\Promo;
|
|
use App\Repository\PromoRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
final class PromoCrudService
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $em,
|
|
private PromoRepository $promoRepository
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return Promo[]
|
|
*/
|
|
public function getList(?int $regionId = null, ?bool $active = true): array
|
|
{
|
|
$criteria = [];
|
|
if ($regionId !== null) {
|
|
$criteria['regionId'] = $regionId;
|
|
}
|
|
if ($active !== null) {
|
|
$criteria['active'] = $active;
|
|
}
|
|
|
|
return $this->promoRepository->findBy($criteria, ['id' => 'ASC']);
|
|
}
|
|
|
|
public function getShow(int $id): ?Promo
|
|
{
|
|
return $this->promoRepository->find($id);
|
|
}
|
|
|
|
public function create(array $data): Promo
|
|
{
|
|
$promo = new Promo();
|
|
$this->updateEntity($promo, $data);
|
|
|
|
$this->em->persist($promo);
|
|
$this->em->flush();
|
|
|
|
return $promo;
|
|
}
|
|
|
|
public function update(Promo $promo, array $data): Promo
|
|
{
|
|
unset($data['id']);
|
|
$this->updateEntity($promo, $data);
|
|
|
|
$this->em->flush();
|
|
return $promo;
|
|
}
|
|
|
|
public function delete(Promo $promo): void
|
|
{
|
|
$this->em->remove($promo);
|
|
$this->em->flush();
|
|
}
|
|
|
|
private function updateEntity(Promo $promo, array $data): void
|
|
{
|
|
if (array_key_exists('id', $data) && $data['id'] !== null && $data['id'] !== '') {
|
|
$promo->setId((int) $data['id']);
|
|
}
|
|
|
|
if (array_key_exists('name', $data)) {
|
|
$promo->setName($data['name']);
|
|
}
|
|
|
|
if (array_key_exists('active', $data)) {
|
|
$promo->setActive($data['active']);
|
|
}
|
|
|
|
if (array_key_exists('regionId', $data) || array_key_exists('region_id', $data)) {
|
|
$v = $data['regionId'] ?? $data['region_id'];
|
|
$promo->setRegionId($v === null || $v === '' ? null : (int) $v);
|
|
}
|
|
|
|
if (array_key_exists('alias', $data)) {
|
|
$promo->setAlias($data['alias']);
|
|
}
|
|
|
|
if (array_key_exists('anons', $data)) {
|
|
$promo->setAnons($data['anons']);
|
|
}
|
|
|
|
if (array_key_exists('content', $data)) {
|
|
$promo->setContent($data['content']);
|
|
}
|
|
|
|
if (array_key_exists('updateAt', $data) || array_key_exists('update_at', $data)) {
|
|
$raw = $data['updateAt'] ?? $data['update_at'];
|
|
if ($raw === null || $raw === '') {
|
|
$promo->setUpdateAt(null);
|
|
} elseif ($raw instanceof \DateTimeInterface) {
|
|
$promo->setUpdateAt($raw);
|
|
} elseif (is_string($raw)) {
|
|
$promo->setUpdateAt(new \DateTimeImmutable($raw));
|
|
}
|
|
}
|
|
|
|
if (array_key_exists('clinics', $data)) {
|
|
$promo->setClinics($data['clinics']);
|
|
}
|
|
|
|
if (array_key_exists('timer', $data)) {
|
|
$promo->setTimer($data['timer']);
|
|
}
|
|
|
|
if (array_key_exists('timerBg', $data) || array_key_exists('timer_bg', $data)) {
|
|
$promo->setTimerBg($data['timerBg'] ?? $data['timer_bg']);
|
|
}
|
|
|
|
if (array_key_exists('shortName', $data) || array_key_exists('short_name', $data)) {
|
|
$promo->setShortName($data['shortName'] ?? $data['short_name']);
|
|
}
|
|
|
|
if (array_key_exists('linkServices', $data) || array_key_exists('link_services', $data)) {
|
|
$promo->setLinkServices($data['linkServices'] ?? $data['link_services']);
|
|
}
|
|
|
|
if (array_key_exists('linkStaff', $data) || array_key_exists('link_staff', $data)) {
|
|
$promo->setLinkStaff($data['linkStaff'] ?? $data['link_staff']);
|
|
}
|
|
|
|
if (array_key_exists('period', $data)) {
|
|
$promo->setPeriod($data['period']);
|
|
}
|
|
|
|
if (array_key_exists('photos', $data)) {
|
|
$promo->setPhotos($data['photos']);
|
|
}
|
|
}
|
|
|
|
public function syncFromViewPromo(string $viewName = 'public.view_promo'): int
|
|
{
|
|
if (!preg_match('/^[A-Za-z0-9_\.]+$/', $viewName)) {
|
|
throw new \InvalidArgumentException('Invalid view name');
|
|
}
|
|
|
|
$connection = $this->em->getConnection();
|
|
|
|
$sql = sprintf(
|
|
'INSERT INTO promo (
|
|
id,
|
|
name,
|
|
active,
|
|
region_id,
|
|
alias,
|
|
anons,
|
|
content,
|
|
update_at,
|
|
clinics,
|
|
timer,
|
|
timer_bg,
|
|
short_name,
|
|
link_services,
|
|
link_staff,
|
|
period,
|
|
photos
|
|
)
|
|
SELECT
|
|
id,
|
|
name,
|
|
active,
|
|
region_id,
|
|
alias,
|
|
anons,
|
|
content,
|
|
update_at,
|
|
clinics,
|
|
timer,
|
|
timer_bg,
|
|
short_name,
|
|
link_services,
|
|
link_staff,
|
|
period,
|
|
photos
|
|
FROM %s
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
active = EXCLUDED.active,
|
|
region_id = EXCLUDED.region_id,
|
|
alias = EXCLUDED.alias,
|
|
anons = EXCLUDED.anons,
|
|
content = EXCLUDED.content,
|
|
update_at = EXCLUDED.update_at,
|
|
clinics = EXCLUDED.clinics,
|
|
timer = EXCLUDED.timer,
|
|
timer_bg = EXCLUDED.timer_bg,
|
|
short_name = EXCLUDED.short_name,
|
|
link_services = EXCLUDED.link_services,
|
|
link_staff = EXCLUDED.link_staff,
|
|
period = EXCLUDED.period,
|
|
photos = EXCLUDED.photos',
|
|
$viewName
|
|
);
|
|
|
|
return (int) $connection->executeStatement($sql);
|
|
}
|
|
}
|