Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: r.kozin
- * Date: 11.09.2018
- * Time: 11:51
- */
- namespace App\Traits;
- use Carbon\Carbon;
- use Exception;
- use Illuminate\Support\Facades\Cache;
- /**
- * Trait MwsTrait
- * @package App\Traits
- */
- trait MwsTrait
- {
- /**
- * @var string|null
- */
- private $nextToken = null;
- /** @var string|null */
- private $sellerId = null;
- /** @var string|null */
- private $marketPlaceId = null;
- /** @var null|string|array */
- private $asinList = null;
- /** @var null|array|string */
- private $idList = null;
- /** @var null|string */
- private $idType = null;
- /**
- * @var array
- */
- private $coolDowns = [
- 'SingleOrder' => 10,
- 'ProductsServiceStatus' => 0,
- 'OrdersServiceStatus' => 0,
- 'OrderItemsListByToken' => 10,
- 'OrderItemsList' => 10,
- 'OrdersListByToken' => 10,
- 'OrdersList' => 10,
- 'FinancialEventsList' => 2,
- 'FinancialGroupsListByToken' => 0,
- 'FinancialGroupsList' => 0,
- 'FinancialEventsListByToken' => 0,
- 'PriceForSku' => 0,
- 'PriceForAsin' => 0,
- 'FeesEstimate' => 0,
- 'MatchingProduct' => 0.5,
- 'MatchingProductForId' => 0.2,
- 'SkuProductCategories' => 0,
- 'ListMatchingProducts' => 0,
- 'CompetitiveSkuPricing' => 0,
- 'LowestSkuPricedOffers' => 0,
- 'AsinProductCategories' => 0,
- 'CompetitiveAsinPricing' => 0,
- 'LowestAsinPricedOffers' => 0,
- 'LowestSkuOffersListings' => 0,
- 'LowestAsinOffersListings' => 0,
- ];
- /**
- * @var string
- */
- private $lastCallCacheKey = 'application-last-call';
- /**
- * @param $method
- * @param array $configs
- * @return mixed
- */
- public function call($method, $configs = [])
- {
- $response = $this->client->{$method}(
- array_filter(
- array_merge(
- [
- 'MarketplaceId' => $this->marketPlaceId ?? null,
- 'SellerId' => $this->sellerId ?? null,
- 'NextToken' => $this->nextToken ?? null,
- 'ASINList' => $this->asinList ?? null,
- 'IdList' => $this->idList ?? null,
- 'IdType' => $this->idType ?? null
- ],
- $configs
- )
- )
- );
- return $response;
- }
- /**
- * @param null $finder
- * @return $this
- * @throws Exception
- */
- public function setFinder($finder = null)
- {
- if (array_key_exists($finder, $this->finders)) {
- $this->finder = new $this->finders[$finder];
- return $this;
- }
- throw new Exception('Finder does not exists in a list of accessible finders');
- }
- /**
- * @param null $asinList
- * @return $this
- */
- public function setAsinList($asinList = null)
- {
- $this->asinList = $asinList;
- return $this;
- }
- /**
- * @param null $idList
- * @return $this
- */
- public function setIdList($idList = null)
- {
- $this->idList = $idList;
- return $this;
- }
- public function setIdType($type = null)
- {
- $this->IdType = $type;
- return $this;
- }
- /**
- * @param void $finder
- * @return mixed
- */
- public function setLastCallTimestamp($finder = null)
- {
- $lastCallCache = Cache::get($this->lastCallCacheKey, false);
- if ($lastCallCache) {
- $lastCallCache[$finder] = Carbon::now()->getTimestamp();
- Cache::forever($this->lastCallCacheKey, $lastCallCache);
- return $lastCallCache[$finder];
- }
- $lastCallCache = [
- $finder => Carbon::now()->getTimestamp()
- ];
- Cache::forever($this->lastCallCacheKey, $lastCallCache);
- return $lastCallCache[$finder];
- }
- /**
- * @param null|string $finder
- * @return mixed
- */
- public function getLastTimeCallTimestamp($finder = null)
- {
- $lastCallCache = Cache::get($this->lastCallCacheKey, false);
- if (array_key_exists($finder, $lastCallCache)) {
- return $lastCallCache[$finder];
- }
- return $this->setLastCallTimestamp($finder);
- }
- /**
- * @param null $finder
- * @return int
- */
- public function getCoolDown($finder = null)
- {
- $lastCall = $this->getLastTimeCallTimestamp($finder);
- $coolDown = $this->coolDowns[$finder] - Carbon::now()
- ->diffInSeconds(
- Carbon::createFromTimestamp($lastCall), true
- );
- return $coolDown >= 0 ? $coolDown : 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment