src/EventListener/UserSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  4. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  5. use Doctrine\DBAL\Connection;
  6. use Twig\Environment;
  7. class UserSubscriber
  8. {
  9.     private $connection;
  10.     private $twig;
  11.     public function __construct(Connection $connectionEnvironment $twig)
  12.     {
  13.         $this->connection $connection;
  14.         $this->twig $twig;
  15.     }
  16.     public function onKernelController(ControllerEvent $event): void
  17.     {
  18.         $request $event->getRequest();
  19.         $session $request->getSession();
  20.         $user null;
  21.         if ($session->has('hash')) {
  22.             $hash $session->get('hash');
  23.             $user $this->connection->fetchAssociative('SELECT * FROM users WHERE hash = ?', [$hash]);
  24.             $sumDeposits $this->connection->fetchOne(
  25.                 'SELECT COALESCE(SUM(suma), 0) FROM deposits WHERE user_id = ? AND status = 1',
  26.                 [$user['id']]
  27.             );
  28.             $xp = (int)$sumDeposits;
  29.             $levels = [
  30.                 ['xp' => 1000,      'bonus' => 100],
  31.                 ['xp' => 5000,      'bonus' => 250],
  32.                 ['xp' => 10000,  'bonus' => 500],
  33.                 ['xp' => 25000,  'bonus' => 1000],
  34.                 ['xp' => 100000'bonus' => 1500],
  35.             ];
  36.             $oldRank $user['rank'];
  37.             $newRank 0;
  38.             $totalBonus 0;
  39.             foreach ($levels as $i => $level) {
  40.                 if ($xp >= $level['xp']) {
  41.                     $newRank $i 1
  42.                     if ($i $oldRank) {
  43.                         $totalBonus += $level['bonus'];
  44.                     }
  45.                 } else {
  46.                     break;
  47.                 }
  48.             }
  49.             if ($newRank $oldRank) {
  50.                 $this->connection->executeStatement(
  51.                     'UPDATE users SET balance = balance + :balance, wager = wager + :wager, `rank` = :rank WHERE id = :id',
  52.                     [
  53.                         'balance' => $totalBonus,
  54.                         'wager' => $totalBonus 5,
  55.                         'rank' => $newRank,
  56.                         'id' => $user['id']
  57.                     ]
  58.                 );
  59.                 
  60.                 $this->connection->insert('events', [
  61.                     'user_id' => $user['id'],
  62.                     'event' => '[Rank] Получение бонуса +' $totalBonus '',
  63.                     'time' => (new \DateTime())->format('Y-m-d H:i:s')
  64.                 ]);
  65.                 
  66.                 $user['balance'] += $totalBonus;
  67.                 $user['wager'] += $totalBonus 5;
  68.                 $user['rank'] = $newRank;
  69.             }
  70.         }
  71.         $this->twig->addGlobal('user'$user);
  72.     }
  73. }