- <?php
- namespace Bidcoz\Bundle\CoreBundle\Twig\Extension;
- use Bidcoz\Bundle\CoreBundle\BidcozCoreServices;
- use Bidcoz\Bundle\CoreBundle\Util\TextManipulator;
- use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
- use Symfony\Contracts\Service\ServiceSubscriberInterface;
- use Twig\Extension\AbstractExtension;
- use Twig\Extension\GlobalsInterface;
- use Twig\TwigFunction;
- abstract class CoreExtension extends AbstractExtension implements GlobalsInterface, ServiceSubscriberInterface
- {
-     use BidcozCoreServices;
-     public function getGlobals(): array
-     {
-         return [
-             'orgParams'      => $this->getOrgParams(),
-             'campaignParams' => $this->getCampaignParams(),
-             'now'            => $this->getNow(),
-             'numMessages'    => $this->getNumberOfFlashMessages(),
-         ];
-     }
-     public function getFunctions()
-     {
-         return [
-             new TwigFunction('validate', [$this, 'validateObject']),
-             new TwigFunction('truncateText', [$this, 'truncateText']),
-         ];
-     }
-     public function getName()
-     {
-         return 'bidcoz.core';
-     }
-     public function validateObject($object, $groups = null)
-     {
-         return $this->getValidator()->validate($object, null, $groups);
-     }
-     public function getNow(): \DateTime
-     {
-         return new \DateTime();
-     }
-     public function getNumberOfFlashMessages(): int
-     {
-         $flashes = $this->getFlashes();
-         return count($flashes, COUNT_RECURSIVE) - count($flashes);
-     }
-     public function truncateText(string $text, int $maxLen, string $additionalText = '...')
-     {
-         return TextManipulator::truncateText($text, $maxLen, $additionalText);
-     }
-     protected function getOrgParams()
-     {
-         if ($request = $this->getRequestStack()->getCurrentRequest()) {
-             if ($request->attributes->has('organization')) {
-                 $organization = $request->attributes->get('organization');
-                 return [
-                     'orgSlug' => $organization->getSlug(),
-                 ];
-             }
-         }
-     }
-     protected function getCampaignParams()
-     {
-         if ($orgParams = $this->getOrgParams()) {
-             if ($request = $this->getRequestStack()->getCurrentRequest()) {
-                 if ($request->attributes->has('campaign')) {
-                     $campaign = $request->attributes->get('campaign');
-                     return array_merge($orgParams, [
-                         'campaignSlug' => $campaign->getSlug(),
-                     ]);
-                 }
-             }
-         }
-     }
-     private function getFlashes(): array
-     {
-         try {
-             return $this->getSession()->getFlashBag()->peekAll();
-         } catch (SessionNotFoundException $e) {
-             return [];
-         }
-     }
- }
-