Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services;
- use Carbon\Carbon;
- use Illuminate\Cache\Repository as Cache;
- use Symfony\Component\HttpFoundation\Session\Session;
- class StatisticsService
- {
- protected $orders_range = [500, 800];
- protected $distribution = [
- 0 => 4.90,
- 1 => 3.41,
- 2 => 2.39,
- 3 => 1.97,
- 4 => 2.00,
- 5 => 2.46,
- 6 => 2.92,
- 7 => 3.51,
- 8 => 3.69,
- 9 => 3.87,
- 10 => 4.01,
- 11 => 4.07,
- 12 => 4.19,
- 13 => 4.56,
- 14 => 4.90,
- 15 => 5.06,
- 16 => 5.07,
- 17 => 5.01,
- 18 => 4.73,
- 19 => 4.40,
- 20 => 4.67,
- 21 => 5.55,
- 22 => 6.32,
- 23 => 6.35
- ];
- protected $increases = [
- 0 => 5,
- 1 => 5,
- 2 => 5,
- 3 => 5,
- 4 => 5,
- 5 => 5,
- 6 => 15,
- 7 => 15,
- 8 => 17,
- 9 => 15,
- 10 => 15,
- 11 => 30,
- 12 => 30,
- 13 => 30,
- 14 => 30,
- 15 => 30,
- ];
- /**
- * @var Session
- */
- protected $session;
- /**
- * @var Cache
- */
- protected $cache;
- /**
- * @var Carbon
- */
- protected $carbon;
- /**
- * Orders num count by day
- *
- * @var array
- */
- protected $orders;
- public function __construct(Session $session, Cache $cache, Carbon $carbon)
- {
- $this->session = $session;
- $this->cache = $cache;
- $this->carbon = $carbon;
- $this->getCache();
- }
- /**
- * Get orders count
- *
- * @return int
- */
- public function getOrders()
- {
- // Get orders from today - based on hour and minute
- $today = $this->orders[date('Y-m-d')];
- $today_orders = 0;
- $hour = date('H');
- for($i=0;$i<$hour;++$i) {
- $today_orders += floor($today * $this->distribution[$i] / 100);
- }
- $minute = date('m');
- $today_orders = floor($today * $this->distribution[$hour] * ($minute/60) / 100);
- $num = $today_orders;
- // Get orders from last 6 days
- foreach($this->orders as $day => $orders) {
- if ($day != date('Y-m-d')) {
- $num += $orders;
- }
- }
- $num = $this->fakeIncrease($num);
- return $num;
- }
- public function getCache()
- {
- $this->orders = $this->cache->get('statistics.orders');
- if (count($this->orders) < 7) {
- for($i=0;$i<7;++$i) {
- $date = $this->carbon->today()->subDays($i);
- $this->orders[$date->format('Y-m-d')] = $this->getRandomOrdersNum();
- }
- }
- if (!isset($this->orders[date('Y-m-d')])) {
- $this->orders[date('Y-m-d')] = $this->getRandomOrdersNum();
- array_pop($this->orders);
- }
- $this->cache->forever('statistics.orders', $this->orders);
- }
- protected function fakeIncrease($num)
- {
- $session = $this->session->get('statistics.orders', false);
- if (!$session) {
- $sum = count($this->increases);
- $session = collect([
- 'increases' => 0,
- 'last' => $this->carbon->now()->subSeconds(10),
- 'current_num' => $num-$sum,
- ]);
- }
- $increases = $session->get('increases');
- $last = $session->get('last');
- $current_num = $session->get('current_num');
- if ($session->get('increases') < last($this->increases)) {
- $offset = $this->increases[$session->get('increases')];
- if ($this->carbon->now()->diffInSeconds($last) > $offset) {
- $increases += 1;
- $last = $this->carbon->now();
- ++$current_num;
- }
- }
- $this->session->set('statistics.orders', collect([
- 'increases' => $increases,
- 'last' => $last,
- 'current_num' => $current_num,
- ]));
- return $current_num;
- }
- protected function getRandomOrdersNum()
- {
- return rand($this->orders_range[0], $this->orders_range[1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment