<?php/** * Created by PhpStorm. * User: oleksandrarhat * Date: 2020-03-25 * Time: 06:28 */namespace App\Zet\ContentBundle\Util;use App\Zet\ContentBundle\Entity\ContentBlock;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;use Symfony\Contracts\Translation\TranslatorInterface;class ContentBlockHandler{ private $em; private $translator; private $containerBag; function __construct( EntityManagerInterface $em, TranslatorInterface $translator, ContainerBagInterface $containerBag ) { $this->em = $em; $this->translator = $translator; $this->containerBag = $containerBag; } private function getOrCreateBlockText($code){ /* @var $repo \App\Zet\ContentBundle\Repository\ContentBlockRepository */ $repo = $this->em->getRepository(ContentBlock::class); if(! $entity = $repo->finByCode($code)){ $entity = new ContentBlock(); $entity->setCode($code); $this->em->persist($entity); $this->em->flush(); } return $entity->getBlockText(); } public function getCopyright(){ return $this->getOrCreateBlockText('copyright'); } public function getFacebookLink(){ return $this->getOrCreateBlockText('facebookLink'); } public function getInstagramLink(){ return $this->getOrCreateBlockText('instagramLink'); } public function getTelegramLink(){ return $this->getOrCreateBlockText('telegramLink'); } public function getYouTubeLink(){ return $this->getOrCreateBlockText('youTubeLink'); } public function getGooglePlusLink(){ return $this->getOrCreateBlockText('googlePlusLink'); } /** * @return string[] */ function getPhones(){ $data = []; $cont = $this->getOrCreateBlockText('phones'); foreach (explode(',', $cont) as $row){ $data[] = trim($row); } return $data; } function getEmail(){ return $this->getOrCreateBlockText('email'); } function getEmailFooter(){ return $this->getOrCreateBlockText('emailFooter'); } /** * @return array */ function getMessengers(){ $cont = $this->getOrCreateBlockText('messengers'); $data = []; foreach (explode(', ', $cont) as $row){ $pair = explode(":", $row); if(count($pair) == 2){ $data[] = [trim($pair[0]) => trim($pair[1])]; } } return $data; } function getApiWelcome(){ return $this->getOrCreateBlockText('api_welcome'); } function getThankBlock(){ return $this->getOrCreateBlockText('thank'); } function getScheduleInfo(){ return $this->getOrCreateBlockText('scheduleInfo'); } function getUploadCartInfo(){ return $this->getOrCreateBlockText('uploadCartInfo'); } function getUrl(){ return $this->getOrCreateBlockText('url'); } function getProjectName(){ if(! $name = $this->getOrCreateBlockText('project_name')){ $name = 'B2B Store'; } return $name; } function getOrderInfo(){ return $this->getOrCreateBlockText('order_info'); }}