custom/plugins/CbaxModulAnalytics/src/Subscriber/BackendSubscriber.php line 98

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cbax\ModulAnalytics\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Cbax\ModulAnalytics\Components\ConfigReaderHelper;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\Checkout\Order\OrderDefinition;
  9. use Shopware\Core\Checkout\Order\OrderEvents;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. class BackendSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var ConfigReaderHelper
  15.      */
  16.     private $config;
  17.     /**
  18.      * @var EntityRepositoryInterface
  19.      */
  20.     private $searchRepository;
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $orderRepository;
  25.     const DEFAULT_DEVICES = [
  26.         'desktop',
  27.         'tablet',
  28.         'mobile'
  29.     ];
  30.     public function __construct(
  31.         ConfigReaderHelper $config,
  32.         EntityRepositoryInterface $searchRepository,
  33.         EntityRepositoryInterface $orderRepository
  34.     )
  35.     {
  36.         $this->config $config->getConfig();
  37.         $this->orderRepository $orderRepository;
  38.         $this->searchRepository $searchRepository;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return[
  43.             ProductSearchResultEvent::class => 'onProductSearch',
  44.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
  45.         ];
  46.     }
  47.     public function onOrderWritten(EntityWrittenEvent $event)
  48.     {
  49.         if (empty($_SERVER)) return;
  50.         if (empty($_SERVER['HTTP_USER_AGENT'])) return;
  51.         if ($event->getEntityName() !== OrderDefinition::ENTITY_NAME) return;
  52.         $httpUserAgent $_SERVER['HTTP_USER_AGENT'];
  53.         $context $event->getContext();
  54.         foreach ($event->getWriteResults() as $writeResult) {
  55.             if ($writeResult->getExistence() !== null && $writeResult->getExistence()->exists()) continue;
  56.             if ($writeResult->getOperation() !== 'insert') continue;
  57.             $payload $writeResult->getPayload();
  58.             if (empty($payload)) continue;
  59.             $customFields = !empty($payload['customFields']) ? $payload['customFields'] : [];
  60.             if (empty($payload['versionId'])) continue;
  61.             if ($payload['versionId'] !== Defaults::LIVE_VERSION) continue;
  62.             if (!empty($customFields) && !empty($customFields['cbaxExternalOrderOrdernumber'])) continue;
  63.             $customFields['cbaxStatistics'] = [
  64.                 'device' => $this->getDeviceType($httpUserAgent),
  65.                 'os' => $this->getOS($httpUserAgent),
  66.                 'browser' => $this->getBrowser($httpUserAgent)
  67.             ];
  68.             $data = [
  69.                 [
  70.                     'id' => $payload['id'],
  71.                     'customFields' => $customFields
  72.                 ]
  73.             ];
  74.             $this->orderRepository->update($data$context);
  75.         }
  76.     }
  77.     public function onProductSearch(ProductSearchResultEvent $event)
  78.     {
  79.         if (empty($this->config['recordSearch'])) return;
  80.         $requestUri $event->getRequest()->attributes->get('sw-original-request-uri');
  81.         if (empty($requestUri)) return;
  82.         if (str_starts_with($requestUri'/widgets')) return;
  83.         $searchUriArray explode('='$requestUri);
  84.         $searchTerm count($searchUriArray) > strtolower(urldecode ($searchUriArray[1])) : '';
  85.         if (empty($searchTerm)) return;
  86.         $results $event->getResult()->getTotal();
  87.         $context $event->getContext();
  88.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  89.         $this->searchRepository->create([
  90.             [
  91.                 'searchTerm' => $searchTerm,
  92.                 'results' => $results,
  93.                 'salesChannelId' => $salesChannelId
  94.             ]
  95.         ], $context);
  96.     }
  97.     private function getDeviceType($httpUserAgent)
  98.     {
  99.         if (!empty($_COOKIE) && !empty($_COOKIE['x-ua-device']))
  100.         {
  101.             $deviceType strtolower($_COOKIE['x-ua-device']);
  102.             if (in_array($deviceTypeself::DEFAULT_DEVICES))
  103.             {
  104.                 return $deviceType;
  105.             }
  106.         }
  107.         $os $this->getOS($httpUserAgent);
  108.         $mobileOS = ['Windows Phone 10','Windows Phone 8.1','Windows Phone 8','BlackBerry','Mobile'];
  109.         if (preg_match('/mobile|phone|ipod/i'$httpUserAgent) || in_array($os$mobileOS))
  110.         {
  111.             return 'mobile';
  112.         }
  113.         $tabletOS = ['Android','iOS'];
  114.         if (preg_match('/tablet|ipad/i'$httpUserAgent) || in_array($os$tabletOS))
  115.         {
  116.             return 'tablet';
  117.         }
  118.         return 'desktop';
  119.     }
  120.     private function getOS($httpUserAgent)
  121.     {
  122.         foreach (self::OS as $key => $value) {
  123.             if (preg_match($key$httpUserAgent)) {
  124.                 return $value;
  125.             }
  126.         }
  127.         return 'Not Detected';
  128.     }
  129.     private function getBrowser($httpUserAgent)
  130.     {
  131.         foreach (self::BROWSER as $key => $value) {
  132.             if (preg_match($key$httpUserAgent)) {
  133.                 return $value;
  134.             }
  135.         }
  136.         return 'Not Detected';
  137.     }
  138.     const OS = [
  139.         '/windows nt 10/i'      =>  'Windows 10',
  140.         '/windows phone 10/i'   =>  'Windows Phone 10',
  141.         '/windows phone 8.1/i'  =>  'Windows Phone 8.1',
  142.         '/windows phone 8/i'    =>  'Windows Phone 8',
  143.         '/windows nt 6.3/i'     =>  'Windows 8.1',
  144.         '/windows nt 6.2/i'     =>  'Windows 8',
  145.         '/windows nt 6.1/i'     =>  'Windows 7',
  146.         '/windows nt 6.0/i'     =>  'Windows Vista',
  147.         '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
  148.         '/windows nt 5.1/i'     =>  'Windows XP',
  149.         '/windows xp/i'         =>  'Windows XP',
  150.         '/windows nt 5.0/i'     =>  'Windows 2000',
  151.         '/windows me/i'         =>  'Windows ME',
  152.         '/win98/i'              =>  'Windows 98',
  153.         '/win95/i'              =>  'Windows 95',
  154.         '/win16/i'              =>  'Windows 3.11',
  155.         '/macintosh|mac os x/i' =>  'Mac OS X',
  156.         '/mac_powerpc/i'        =>  'Mac OS 9',
  157.         '/iphone/i'             =>  'iOS',
  158.         '/ipod/i'               =>  'iOS',
  159.         '/ipad/i'               =>  'iOS',
  160.         '/android/i'            =>  'Android',
  161.         '/linux/i'              =>  'Linux',
  162.         '/ubuntu/i'             =>  'Ubuntu',
  163.         '/blackberry/i'         =>  'BlackBerry',
  164.         '/webos/i'              =>  'Mobile'
  165.     ];
  166.     const BROWSER = [
  167.         '/firefox/i'    =>  'Firefox',
  168.         '/msie/i'       =>  'Internet Explorer',
  169.         '/edge/i'       =>  'Edge',
  170.         '/edg/i'        =>  'Edge',
  171.         '/opera/i'      =>  'Opera',
  172.         '/chrome/i'     =>  'Chrome',
  173.         '/safari/i'     =>  'Safari',
  174.         '/mobile/i'     =>  'Handheld Browser',
  175.         '/netscape/i'   =>  'Netscape',
  176.         '/maxthon/i'    =>  'Maxthon',
  177.         '/konqueror/i'  =>  'Konqueror'
  178.     ];
  179. }