issues/27: prod baseline without task branch
This commit is contained in:
@@ -2,26 +2,148 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Promo;
|
||||
use App\Repository\PromoRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* Импорт акций из материализованного представления (Bitrix view).
|
||||
*
|
||||
* См. PromoController + CrudResponder для CRUD; этот сервис — только syncFromView*.
|
||||
*/
|
||||
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,
|
||||
@@ -78,6 +200,6 @@ final class PromoCrudService
|
||||
$viewName
|
||||
);
|
||||
|
||||
return (int) $this->em->getConnection()->executeStatement($sql);
|
||||
return (int) $connection->executeStatement($sql);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user