src/Utils/Uniqskills/MixPanel/MixPanelService.php line 72

Open in your IDE?
  1. <?php
  2. namespace App\Utils\Uniqskills\MixPanel;
  3. use App\Entity\Gos\OrderPart;
  4. use App\Entity\Gos\OrderProductVariant;
  5. use App\Entity\Gos\Orders;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class MixPanelService
  10. {
  11.     private ?Request $request;
  12.     public function __construct(RequestStack $requestStack)
  13.     {
  14.         $this->request $requestStack->getCurrentRequest();
  15.     }
  16.     public function sendSearchQueryEvent(string $queryUserInterface $user null): void
  17.     {
  18.         $mp $this->initialize();
  19.         $this->setPeople($mp$user);
  20.         $this->sendEvent($mpMixPanelEvent::SEARCH, [
  21.             'query'       => $query,
  22.             'distinct_id' => $this->setDistinctId($user),
  23.         ]);
  24.     }
  25.     public function sendCartRenderEvent(array $parametersUserInterface $user null): void
  26.     {
  27.         $mp $this->initialize();
  28.         $this->setPeople($mp$user);
  29.         $this->sendEvent($mpMixPanelEvent::CARTarray_merge($parameters, [
  30.             'distinct_id' => $this->setDistinctId($user)
  31.         ]));
  32.     }
  33.     public function initialize(): \Mixpanel
  34.     {
  35.         return \Mixpanel::getInstance('64a0f4f4e3e2f61d17fa2dbcfa5cb8e2', [
  36.             'host' => 'api-eu.mixpanel.com',
  37.         ]);
  38.     }
  39.     public function sendEvent(\Mixpanel $mpstring $event, array $parameters = []): void
  40.     {
  41.         $mp->track(
  42.             $event,
  43.             array_merge($parameters)
  44.         );
  45.     }
  46.     public function setPeople(\Mixpanel $mpUserInterface $user null): void
  47.     {
  48.         if ($user instanceof UserInterface)
  49.         {
  50.             $username $user->getUsername();
  51.             $mp->people->set($username, [
  52.                 '$email' => $username,
  53.                 '$name'  => strtok($username'@'),
  54.             ], $ip 0$ignore_time true);
  55.         }
  56.         else
  57.         {
  58.             $session $this->request->getSession();
  59.             $mp->people->set($session->getId(), [
  60.                 'tempUser'      => $session->get('tempUser'),
  61.                 'registerFrom'  => $session->get('registerFrom'),
  62.                 'myCountryCode' => $session->get('myCountryCode'),
  63.             ]);
  64.         }
  65.     }
  66.     public function setDistinctId(UserInterface $user null): string
  67.     {
  68.         if ($user !== null)
  69.         {
  70.             return $user->getUsername();
  71.         }
  72.         return $this->request->getSession()->getId();
  73.     }
  74.     public function prepareOrderData(Orders $order): array
  75.     {
  76.         $orderParts           = [];
  77.         $orderProductVariants = [];
  78.         /** @var OrderPart $orderPart */
  79.         foreach ($order->getOrderPart() as $orderPart)
  80.         {
  81.             /** @var OrderProductVariant $orderProductVariant */
  82.             foreach ($orderPart->getOrderProductVariant() as $orderProductVariant)
  83.             {
  84.                 array_push($orderProductVariants, [
  85.                     'productVariantNoComplete' => $orderProductVariant->getProductVariantNoComplete(),
  86.                     'title'                    => $orderProductVariant->getTitle(),
  87.                     'priceNet'                 => $orderProductVariant->getPriceNet(),
  88.                     'priceGross'               => $orderProductVariant->getPriceGross(),
  89.                     'quantity'                 => $orderProductVariant->getQuantity(),
  90.                 ]);
  91.             }
  92.             array_push($orderParts, [
  93.                 'ordTran'              => $orderPart->getOrdTran(),
  94.                 'totalPriceNet'        => $orderPart->getTotalPriceNet(),
  95.                 'totalPriceGross'      => $orderPart->getTotalPriceGross(),
  96.                 'orderProductVariants' => $orderProductVariants,
  97.             ]);
  98.         }
  99.         return [
  100.             'ordTran'         => $order->getOrdTran(),
  101.             'totalPriceNet'   => $order->getTotalPriceNet(),
  102.             'totalPriceGross' => $order->getTotalPriceGross(),
  103.             'actionNumber'    => $order->getActionNumber(),
  104.             'fromSource'      => $order->getFromSource(),
  105.             'paymentSystem'   => $order->getPaymentSystem() ? $order->getPaymentSystem()->getName() : null,
  106.             'orderParts'      => $orderParts,
  107.         ];
  108.     }
  109. }