Romanovich095

Code Example for Project 3803972

Sep 18th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.84 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: r.kozin
  5.  * Date: 11.09.2018
  6.  * Time: 11:51
  7.  */
  8.  
  9. namespace App\Traits;
  10.  
  11.  
  12. use Carbon\Carbon;
  13. use Exception;
  14. use Illuminate\Support\Facades\Cache;
  15.  
  16. /**
  17.  * Trait MwsTrait
  18.  * @package App\Traits
  19.  */
  20. trait MwsTrait
  21. {
  22.  
  23.     /**
  24.      * @var string|null
  25.      */
  26.     private $nextToken = null;
  27.  
  28.     /** @var string|null */
  29.     private $sellerId = null;
  30.  
  31.     /** @var string|null */
  32.     private $marketPlaceId = null;
  33.  
  34.     /** @var null|string|array */
  35.     private $asinList = null;
  36.  
  37.     /** @var null|array|string */
  38.     private $idList = null;
  39.  
  40.     /** @var null|string */
  41.     private $idType = null;
  42.  
  43.     /**
  44.      * @var array
  45.      */
  46.     private $coolDowns = [
  47.         'SingleOrder' => 10,
  48.         'ProductsServiceStatus' => 0,
  49.         'OrdersServiceStatus' => 0,
  50.         'OrderItemsListByToken' => 10,
  51.         'OrderItemsList' => 10,
  52.         'OrdersListByToken' => 10,
  53.         'OrdersList' => 10,
  54.         'FinancialEventsList' => 2,
  55.         'FinancialGroupsListByToken' => 0,
  56.         'FinancialGroupsList' => 0,
  57.         'FinancialEventsListByToken' => 0,
  58.         'PriceForSku' => 0,
  59.         'PriceForAsin' => 0,
  60.         'FeesEstimate' => 0,
  61.         'MatchingProduct' => 0.5,
  62.         'MatchingProductForId' => 0.2,
  63.         'SkuProductCategories' => 0,
  64.         'ListMatchingProducts' => 0,
  65.         'CompetitiveSkuPricing' => 0,
  66.         'LowestSkuPricedOffers' => 0,
  67.         'AsinProductCategories' => 0,
  68.         'CompetitiveAsinPricing' => 0,
  69.         'LowestAsinPricedOffers' => 0,
  70.         'LowestSkuOffersListings' => 0,
  71.         'LowestAsinOffersListings' => 0,
  72.     ];
  73.  
  74.     /**
  75.      * @var string
  76.      */
  77.     private $lastCallCacheKey = 'application-last-call';
  78.  
  79.     /**
  80.      * @param $method
  81.      * @param array $configs
  82.      * @return mixed
  83.      */
  84.     public function call($method, $configs = [])
  85.     {
  86.  
  87.         $response = $this->client->{$method}(
  88.             array_filter(
  89.                 array_merge(
  90.                     [
  91.                         'MarketplaceId' => $this->marketPlaceId ?? null,
  92.                         'SellerId' => $this->sellerId ?? null,
  93.                         'NextToken' => $this->nextToken ?? null,
  94.                         'ASINList' => $this->asinList ?? null,
  95.                         'IdList' => $this->idList ?? null,
  96.                         'IdType' => $this->idType ?? null
  97.                     ],
  98.                     $configs
  99.                 )
  100.             )
  101.         );
  102.  
  103.         return $response;
  104.     }
  105.  
  106.     /**
  107.      * @param null $finder
  108.      * @return $this
  109.      * @throws Exception
  110.      */
  111.     public function setFinder($finder = null)
  112.     {
  113.         if (array_key_exists($finder, $this->finders)) {
  114.             $this->finder = new $this->finders[$finder];
  115.             return $this;
  116.         }
  117.  
  118.         throw new Exception('Finder does not exists in a list of accessible finders');
  119.     }
  120.  
  121.     /**
  122.      * @param null $asinList
  123.      * @return $this
  124.      */
  125.     public function setAsinList($asinList = null)
  126.     {
  127.         $this->asinList = $asinList;
  128.         return $this;
  129.     }
  130.  
  131.     /**
  132.      * @param null $idList
  133.      * @return $this
  134.      */
  135.     public function setIdList($idList = null)
  136.     {
  137.         $this->idList = $idList;
  138.         return $this;
  139.     }
  140.  
  141.     public function setIdType($type = null)
  142.     {
  143.         $this->IdType = $type;
  144.         return $this;
  145.     }
  146.  
  147.     /**
  148.      * @param void $finder
  149.      * @return mixed
  150.      */
  151.     public function setLastCallTimestamp($finder = null)
  152.     {
  153.         $lastCallCache = Cache::get($this->lastCallCacheKey, false);
  154.  
  155.         if ($lastCallCache) {
  156.             $lastCallCache[$finder] = Carbon::now()->getTimestamp();
  157.  
  158.             Cache::forever($this->lastCallCacheKey, $lastCallCache);
  159.             return $lastCallCache[$finder];
  160.         }
  161.  
  162.         $lastCallCache = [
  163.             $finder => Carbon::now()->getTimestamp()
  164.         ];
  165.  
  166.         Cache::forever($this->lastCallCacheKey, $lastCallCache);
  167.         return $lastCallCache[$finder];
  168.     }
  169.  
  170.     /**
  171.      * @param null|string $finder
  172.      * @return mixed
  173.      */
  174.     public function getLastTimeCallTimestamp($finder = null)
  175.     {
  176.         $lastCallCache = Cache::get($this->lastCallCacheKey, false);
  177.  
  178.         if (array_key_exists($finder, $lastCallCache)) {
  179.             return $lastCallCache[$finder];
  180.         }
  181.  
  182.         return $this->setLastCallTimestamp($finder);
  183.     }
  184.  
  185.     /**
  186.      * @param null $finder
  187.      * @return int
  188.      */
  189.     public function getCoolDown($finder = null)
  190.     {
  191.         $lastCall = $this->getLastTimeCallTimestamp($finder);
  192.         $coolDown = $this->coolDowns[$finder] - Carbon::now()
  193.                 ->diffInSeconds(
  194.                     Carbon::createFromTimestamp($lastCall), true
  195.                 );
  196.  
  197.         return $coolDown >= 0 ? $coolDown : 1;
  198.     }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment