zalazdi

Untitled

Apr 17th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services;
  4.  
  5. use Carbon\Carbon;
  6. use Illuminate\Cache\Repository as Cache;
  7. use Symfony\Component\HttpFoundation\Session\Session;
  8.  
  9. class StatisticsService
  10. {
  11.  
  12.     protected $orders_range = [500, 800];
  13.     protected $distribution = [
  14.         0 => 4.90,
  15.         1 => 3.41,
  16.         2 => 2.39,
  17.         3 => 1.97,
  18.         4 => 2.00,
  19.         5 => 2.46,
  20.         6 => 2.92,
  21.         7 => 3.51,
  22.         8 => 3.69,
  23.         9 => 3.87,
  24.         10 => 4.01,
  25.         11 => 4.07,
  26.         12 => 4.19,
  27.         13 => 4.56,
  28.         14 => 4.90,
  29.         15 => 5.06,
  30.         16 => 5.07,
  31.         17 => 5.01,
  32.         18 => 4.73,
  33.         19 => 4.40,
  34.         20 => 4.67,
  35.         21 => 5.55,
  36.         22 => 6.32,
  37.         23 => 6.35
  38.     ];
  39.     protected $increases = [
  40.         0 => 5,
  41.         1 => 5,
  42.         2 => 5,
  43.         3 => 5,
  44.         4 => 5,
  45.         5 => 5,
  46.         6 => 15,
  47.         7 => 15,
  48.         8 => 17,
  49.         9 => 15,
  50.         10 => 15,
  51.         11 => 30,
  52.         12 => 30,
  53.         13 => 30,
  54.         14 => 30,
  55.         15 => 30,
  56.     ];
  57.  
  58.     /**
  59.      * @var Session
  60.      */
  61.     protected $session;
  62.  
  63.     /**
  64.      * @var Cache
  65.      */
  66.     protected $cache;
  67.  
  68.     /**
  69.      * @var Carbon
  70.      */
  71.     protected $carbon;
  72.  
  73.     /**
  74.      * Orders num count by day
  75.      *
  76.      * @var array
  77.      */
  78.     protected $orders;
  79.  
  80.     public function __construct(Session $session, Cache $cache, Carbon $carbon)
  81.     {
  82.         $this->session = $session;
  83.         $this->cache = $cache;
  84.         $this->carbon = $carbon;
  85.  
  86.         $this->getCache();
  87.     }
  88.  
  89.     /**
  90.      * Get orders count
  91.      *
  92.      * @return int
  93.      */
  94.     public function getOrders()
  95.     {
  96.         // Get orders from today - based on hour and minute
  97.         $today = $this->orders[date('Y-m-d')];
  98.  
  99.         $today_orders = 0;
  100.         $hour = date('H');
  101.         for($i=0;$i<$hour;++$i) {
  102.             $today_orders += floor($today * $this->distribution[$i] / 100);
  103.         }
  104.  
  105.         $minute = date('m');
  106.         $today_orders = floor($today * $this->distribution[$hour] * ($minute/60) / 100);
  107.  
  108.         $num = $today_orders;
  109.  
  110.         // Get orders from last 6 days
  111.         foreach($this->orders as $day => $orders) {
  112.             if ($day != date('Y-m-d')) {
  113.                 $num += $orders;
  114.             }
  115.         }
  116.  
  117.         $num = $this->fakeIncrease($num);
  118.  
  119.         return $num;
  120.     }
  121.  
  122.     public function getCache()
  123.     {
  124.         $this->orders = $this->cache->get('statistics.orders');
  125.  
  126.         if (count($this->orders) < 7) {
  127.             for($i=0;$i<7;++$i) {
  128.                 $date = $this->carbon->today()->subDays($i);
  129.  
  130.                 $this->orders[$date->format('Y-m-d')] = $this->getRandomOrdersNum();
  131.             }
  132.         }
  133.  
  134.         if (!isset($this->orders[date('Y-m-d')])) {
  135.             $this->orders[date('Y-m-d')] = $this->getRandomOrdersNum();
  136.             array_pop($this->orders);
  137.         }
  138.  
  139.         $this->cache->forever('statistics.orders', $this->orders);
  140.     }
  141.  
  142.     protected function fakeIncrease($num)
  143.     {
  144.         $session = $this->session->get('statistics.orders', false);
  145.         if (!$session) {
  146.             $sum = count($this->increases);
  147.  
  148.             $session = collect([
  149.                 'increases' => 0,
  150.                 'last' => $this->carbon->now()->subSeconds(10),
  151.                 'current_num' => $num-$sum,
  152.             ]);
  153.         }
  154.  
  155.         $increases = $session->get('increases');
  156.         $last = $session->get('last');
  157.         $current_num = $session->get('current_num');
  158.  
  159.         if ($session->get('increases') < last($this->increases)) {
  160.             $offset = $this->increases[$session->get('increases')];
  161.  
  162.             if ($this->carbon->now()->diffInSeconds($last) > $offset) {
  163.                 $increases += 1;
  164.                 $last = $this->carbon->now();
  165.                 ++$current_num;
  166.             }
  167.         }
  168.  
  169.         $this->session->set('statistics.orders', collect([
  170.             'increases' => $increases,
  171.             'last' => $last,
  172.             'current_num' => $current_num,
  173.         ]));
  174.  
  175.         return $current_num;
  176.     }
  177.  
  178.     protected function getRandomOrdersNum()
  179.     {
  180.         return rand($this->orders_range[0], $this->orders_range[1]);
  181.     }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment