src/Controller/PaymentStatisticController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\PaymentStatisticFormType;
  4. use App\Gateway\BiReport\PaymentStatisticGateway;
  5. use App\Report\ReportOlap;
  6. use Doctrine\DBAL\Connection;
  7. use Exception;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class PaymentStatisticController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/payment-statistic", name="payment_statistic")
  16.      * @throws Exception
  17.      */
  18.     public function paymentStatistic(Request $requestConnection $connection): Response
  19.     {
  20.         $form $this->get('form.factory')->createNamed(''PaymentStatisticFormType::class, null, [
  21.             'clear_group_by_default' => !! $request->get('groupBy'),
  22.         ]);
  23.         $form->handleRequest($request);
  24.         if ($form->isSubmitted() && $form->isValid()) {
  25.             $data $form->getData();
  26.             $groupingFields $data['groupBy'];
  27.             $paymentStatisticGateway = new PaymentStatisticGateway($connection);
  28.             $userStat $paymentStatisticGateway->getUserStat(
  29.                 $data['dateFrom'],
  30.                 $data['dateTo'],
  31.                 $data['timeGroup'],
  32.                 $data['groupBy'],
  33.                 $data['affiliatePlatform'],
  34.                 $data['utmSource']
  35.             );
  36.             $transactionsStat $paymentStatisticGateway->getTransactionsStat(
  37.                 $data['additionalDateFrom'],
  38.                 $data['additionalDateTo'],
  39.                 $data['dateFrom'],
  40.                 $data['dateTo'],
  41.                 $data['filterTransactionsByUser'],
  42.                 $data['timeGroup'],
  43.                 $data['groupBy'],
  44.                 $data['affiliatePlatform'],
  45.                 $data['utmSource']
  46.             );
  47.             $eventsStat $paymentStatisticGateway->getEventsStat(
  48.                 $data['additionalDateFrom'],
  49.                 $data['additionalDateTo'],
  50.                 $data['dateFrom'],
  51.                 $data['dateTo'],
  52.                 $data['filterTransactionsByUser'],
  53.                 $data['timeGroup'],
  54.                 $data['groupBy'],
  55.                 $data['affiliatePlatform'],
  56.                 $data['utmSource']
  57.             );
  58.             $rows $this->getUniqueByKeysFromStats([$userStat$transactionsStat], $groupingFields);
  59.             $olap = new ReportOlap($rows);
  60.             $userOlap = new ReportOlap($userStat);
  61.             $eventOlap = new ReportOlap($eventsStat);
  62.             $transactionOlap = new ReportOlap($transactionsStat);
  63.             $columnsPeriods array_unique(array_column(
  64.                 $this->getUniqueByKeysFromStats([$eventsStat$transactionsStat], ['event_date'], ['-']),
  65.                 'event_date'
  66.             ));
  67.             sort($columnsPeriods);
  68.         } else {
  69.             $olap $userOlap $eventOlap $transactionOlap $groupingFields null;
  70.             $columnsPeriods $rows = [];
  71.         }
  72.         return $this->render('payment_statistic.html.twig', [
  73.             'olap' => $olap,
  74.             'userOlap' => $userOlap,
  75.             'eventOlap' => $eventOlap,
  76.             'transactionOlap' => $transactionOlap,
  77.             'form' => $form->createView(),
  78.             'rows' => $rows,
  79.             'columnsPeriods' => $columnsPeriods,
  80.             'groupingFields' => $groupingFields,
  81.         ]);
  82.     }
  83.     /**
  84.      * @param array $stats
  85.      * @param array $columns
  86.      * @param array $filter
  87.      * @return array
  88.      */
  89.     private function getUniqueByKeysFromStats(array $stats, array $columns, array $filter = []): array
  90.     {
  91.         $result = [];
  92.         $columns array_flip($columns);
  93.         $key 0;
  94.         if (empty ($filter)) {
  95.             foreach ($stats as $stat) {
  96.                 foreach ($stat as $val) {
  97.                     $result[$key++] = array_intersect_key($val$columns);
  98.                 }
  99.             }
  100.         } else {
  101.             foreach ($stats as $stat) {
  102.                 foreach ($stat as $val) {
  103.                     if (! in_array($val$filter)) {
  104.                         $result[$key++] = array_intersect_key($val$columns);
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         return $result;
  110.     }
  111. }