Overview

Namespaces

  • SellerLabs
    • Snagshout
      • Model
      • Normalizer
      • Resource
      • Utils

Classes

  • SellerLabs\Snagshout\Client
  • SellerLabs\Snagshout\Model\AmazonData
  • SellerLabs\Snagshout\Model\Attribute
  • SellerLabs\Snagshout\Model\BookmarkMetadata
  • SellerLabs\Snagshout\Model\Campaign
  • SellerLabs\Snagshout\Model\Category
  • SellerLabs\Snagshout\Model\Image
  • SellerLabs\Snagshout\Model\ImageMetadata
  • SellerLabs\Snagshout\Model\Links
  • SellerLabs\Snagshout\Model\Product
  • SellerLabs\Snagshout\Model\PromoCode
  • SellerLabs\Snagshout\Model\Promotion
  • SellerLabs\Snagshout\Model\Shipping
  • SellerLabs\Snagshout\Model\V1GetCampaignsResponse
  • SellerLabs\Snagshout\Model\V1GetCategoriesResponse
  • SellerLabs\Snagshout\Model\V1GetStatus
  • SellerLabs\Snagshout\Model\V1GetStatusResponse
  • SellerLabs\Snagshout\Normalizer\AmazonDataNormalizer
  • SellerLabs\Snagshout\Normalizer\AttributeNormalizer
  • SellerLabs\Snagshout\Normalizer\BookmarkMetadataNormalizer
  • SellerLabs\Snagshout\Normalizer\CampaignNormalizer
  • SellerLabs\Snagshout\Normalizer\CategoryNormalizer
  • SellerLabs\Snagshout\Normalizer\ImageMetadataNormalizer
  • SellerLabs\Snagshout\Normalizer\ImageNormalizer
  • SellerLabs\Snagshout\Normalizer\LinksNormalizer
  • SellerLabs\Snagshout\Normalizer\NormalizerFactory
  • SellerLabs\Snagshout\Normalizer\ProductNormalizer
  • SellerLabs\Snagshout\Normalizer\PromoCodeNormalizer
  • SellerLabs\Snagshout\Normalizer\PromotionNormalizer
  • SellerLabs\Snagshout\Normalizer\ShippingNormalizer
  • SellerLabs\Snagshout\Normalizer\V1GetCampaignsResponseNormalizer
  • SellerLabs\Snagshout\Normalizer\V1GetCategoriesResponseNormalizer
  • SellerLabs\Snagshout\Normalizer\V1GetStatusNormalizer
  • SellerLabs\Snagshout\Normalizer\V1GetStatusResponseNormalizer
  • SellerLabs\Snagshout\Resource\CampaignResource
  • SellerLabs\Snagshout\Resource\CategoryResource
  • SellerLabs\Snagshout\Resource\FrontResource
  • SellerLabs\Snagshout\SyndicationClient
  • SellerLabs\Snagshout\Utils\NormalizerFactory
  • SellerLabs\Snagshout\Utils\NullNormalizer
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace SellerLabs\Snagshout;
  4: 
  5: use Closure;
  6: use DateTime;
  7: use GuzzleHttp\Client;
  8: use GuzzleHttp\Handler\CurlHandler;
  9: use GuzzleHttp\HandlerStack;
 10: use GuzzleHttp\Psr7\Uri;
 11: use Psr\Http\Message\RequestInterface;
 12: use Psr\Http\Message\ResponseInterface;
 13: 
 14: /**
 15:  * Class SyndicationClient
 16:  *
 17:  * @package SellerLabs\Snagshout
 18:  *
 19:  * @author Eduardo Trujillo <ed@sellerlabs.com>
 20:  */
 21: class SyndicationClient
 22: {
 23:     /**
 24:      * @var string
 25:      */
 26:     protected $publicId;
 27: 
 28:     /**
 29:      * @var string
 30:      */
 31:     protected $secretKey;
 32: 
 33:     /**
 34:      * @var Uri
 35:      */
 36:     protected $endpoint;
 37: 
 38:     /**
 39:      * SyndicationClient constructor.
 40:      *
 41:      * @param string $publicId
 42:      * @param string $secretKey
 43:      */
 44:     public function __construct($publicId, $secretKey)
 45:     {
 46:         $this->publicId = $publicId;
 47:         $this->secretKey = $secretKey;
 48: 
 49:         $this->endpoint = new Uri('https://www.snagshout.com');
 50: 
 51:         $stack = new HandlerStack();
 52: 
 53:         $stack->setHandler(new CurlHandler());
 54: 
 55:         $stack->push($this->makeAuthHandler());
 56: 
 57:         $this->client = new Client([
 58:             'handler' => $stack,
 59:         ]);
 60:     }
 61: 
 62:     /**
 63:      * @param string $publicId
 64:      */
 65:     public function setPublicId(string $publicId)
 66:     {
 67:         $this->publicId = $publicId;
 68:     }
 69: 
 70:     /**
 71:      * @param string $secretKey
 72:      */
 73:     public function setSecretKey(string $secretKey)
 74:     {
 75:         $this->secretKey = $secretKey;
 76:     }
 77: 
 78:     /**
 79:      * @param Uri $endpoint
 80:      */
 81:     public function setEndpoint(Uri $endpoint)
 82:     {
 83:         $this->endpoint = $endpoint;
 84:     }
 85: 
 86:     /**
 87:      * Computes the hash of a string using the secret key.
 88:      *
 89:      * @param string $content
 90:      *
 91:      * @return string
 92:      */
 93:     protected function hash($content)
 94:     {
 95:         $timestamp = (new DateTime())->format('Y-m-d H');
 96: 
 97:         return hash_hmac(
 98:             'sha512',
 99:             $content . $timestamp,
100:             $this->secretKey
101:         );
102:     }
103: 
104:     /**
105:      * A Guzzle middleware that automatically adds the required Authorization
106:      * and Content-Hash headers required by the Snagshout API.
107:      *
108:      * @return Closure
109:      */
110:     protected function makeAuthHandler()
111:     {
112:         return function (callable $handler) {
113:             return function (
114:                 RequestInterface $request,
115:                 array $options
116:             ) use ($handler) {
117:                 $contentHash = $this->hash($request->getBody());
118: 
119:                 $partialUri = $request->getUri();
120:                 $uri = $this->endpoint
121:                     ->withPath(
122:                         $this->endpoint->getPath()
123:                         . $partialUri->getPath()
124:                     )
125:                     ->withQuery($partialUri->getQuery())
126:                     ->withFragment($partialUri->getFragment());
127: 
128:                 $request = $request
129:                     ->withUri($uri)
130:                     ->withHeader(
131:                         'Authorization',
132:                         vsprintf('Hash %s', [$this->publicId])
133:                     )
134:                     ->withHeader('Content-Hash', $contentHash);
135: 
136:                 return $handler($request, $options);
137:             };
138:         };
139:     }
140: 
141:     /**
142:      * Searches for campaigns/offers.
143:      *
144:      * @param array $options
145:      *
146:      * @return ResponseInterface
147:      */
148:     public function getCampaigns(array $options = [])
149:     {
150:         return $this->client->get('/api/v1/campaigns', array_merge(
151:             [
152:                 'query' => [
153:                     'embeds' => 'promotions',
154:                 ]
155:             ],
156:             $options
157:         ));
158:     }
159: }
API documentation generated by ApiGen