src/Http/Controller/Api/V2/Analytics/CollectAnalyticsAction.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Http\Controller\Api\V2\Analytics;
  3. use App\Infrastructure\Messenger\CommandBus\CommandBusInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. /**
  8.  * @Route("/api/v2/analytics/collect", name="api_v2_analytics_collect", methods={"PUT"}, requirements={"id":"[a-f0-9-]+"})
  9.  */
  10. class CollectAnalyticsAction extends AbstractController
  11. {
  12.     public function __invoke(
  13.         // CreateToastSessionLinkCommand $command,
  14.         // CommandBusInterface $commandBus
  15.     ): JsonResponse {
  16.         // $this->denyAccessUnlessGranted(ToastSessionVoter::ACCESS_WITH_COLLABORATORS, $session);
  17.         // $command->session = $session->getId();
  18.         // $siteParser    = new SiteParser($command->url);
  19.         // $command->name = $siteParser->getPageTitle();
  20.         // $command->icon = $siteParser->getFavicon();
  21.         // $commandBus->execute($command);
  22.         // $link = $toastSessionLinkRepository->find($command->id);
  23.         if ($_SERVER['REQUEST_METHOD'] != 'PUT') {
  24.             exit;
  25.         }
  26.         // helpers
  27.         function db_request($request$db_Name '')
  28.         {
  29.             // constants
  30.             $host 'localhost';
  31.             $host_user 'bmrmcbxy_toast';
  32.             $host_pass 'kaphaf-zeXjac-tugge3';
  33.             $dbname 'bmrmcbxy_toast';
  34.             // setting time zone
  35.             $default_timezone 'Etc/GMT';
  36.             $kyiv_timezone 'Europe/Kiev';
  37.             date_default_timezone_set($default_timezone);
  38.             // analytics table data
  39.             $analytics_table 'usage_analytics';
  40.             if ($db_Name == '') {
  41.                 $db_Name $dbname;
  42.             }
  43.             $link mysqli_connect($host$host_user$host_pass$db_Name);
  44.             mysqli_set_charset($link'utf8');
  45.             if (!$link) {
  46.                 echo "Can't connect to the database. Error code: %s\n".mysqli_connect_error();
  47.                 exit;
  48.             }// if connection error
  49.             if ($result mysqli_query($link$request)) {
  50.                 return $result;
  51.             } else {
  52.                 error_log('msqlERROR1:'.mysqli_error($link));
  53.                 error_log('msqlERRORrequest1: From '.$table_name.' '.$request);
  54.             }
  55.             /* Close connection */
  56.             mysqli_close($link);
  57.         }
  58.         function buildEventSavingSQLQuery($eventParameters)
  59.         {
  60.             // constants
  61.             $analytics_table 'usage_analytics';
  62.             $autoFilledParameters = [
  63.                 'id',
  64.                 'timestamp',
  65.             ];
  66.             $keysPart 'INSERT INTO `'.$analytics_table.'` (`id`';
  67.             $valuesPart ') VALUES (NULL';
  68.             foreach ($eventParameters as $parameter => $value) {
  69.                 if (!in_array($parameter$autoFilledParameters)) {
  70.                     $keysPart .= ', `'.$parameter.'`';
  71.                     $valuesPart .= ", '".$value."'";
  72.                 }
  73.             }
  74.             $sqlString $keysPart.$valuesPart.');';
  75.             return $sqlString;
  76.         }
  77.         // operating functions
  78.         /*
  79.             $requestHeaders = [];
  80.             foreach (getallheaders() as $name => $value) {
  81.                 // echo "$name: $value\n";
  82.                 $requestHeaders[$name] = $value;
  83.             };
  84.         */
  85.         function saveEvent()
  86.         {
  87.             $requestJSONBody file_get_contents('php://input');
  88.             $eventParameters json_decode($requestJSONBody);
  89.             // var_dump($eventParameters);
  90.             $query buildEventSavingSQLQuery($eventParameters);
  91.             // echo $query;
  92.             db_request($query);
  93.         }
  94.         saveEvent();
  95.         return $this->json(['success' => true]);
  96.     }
  97. }