Skip to content

Commit

Permalink
Upgrade PHP to 7.3 (#49)
Browse files Browse the repository at this point in the history
* Upgrade to PHP 7.3

* Fix phpunit.xml

* Create AbstractModel

* Make sure that all models extend AbstractModel

* Create AbstractNormalizer

* Make sure that all normalizers extend AbstractNormalizer

* Create HttpClient

* Use custom HttpClient in Client

* Create Fetch enum

* Create RawEncoder

* Create AbstractResource

* Make sure that all resources extend AbstractResource

* Fix DealNormalizerTest

* Upgrade copmposer packages to support PHP 7.3

* Remove relations from models

* Update composer.json
  • Loading branch information
Andriichello authored Mar 20, 2023
1 parent 9464fd3 commit 175a9f2
Show file tree
Hide file tree
Showing 80 changed files with 1,280 additions and 1,431 deletions.
17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"description": "Promote SDK",
"type": "library",
"require": {
"php": ">=7.1",
"guzzlehttp/guzzle": "^6.2",
"jane/open-api": "1.3",
"php-http/guzzle6-adapter": "^1.1"
"php": ">=7.3",
"guzzlehttp/guzzle": ">=7.0",
"symfony/serializer": ">=5.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "~5.7"
"phpunit/phpunit": "^9.0"
},
"license": "Apache 2",
"authors": [
Expand All @@ -28,5 +28,10 @@
"Tests\\Snagshout\\Promote\\": "php/tests/Snagshout/Promote"
}
},
"minimum-stability": "stable"
"minimum-stability": "stable",
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
145 changes: 13 additions & 132 deletions php/src/Snagshout/Promote/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@

namespace Snagshout\Promote;

use Closure;
use DateTime;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Uri;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Joli\Jane\OpenApi\Runtime\Client\Resource;
use Joli\Jane\Runtime\Encoder\RawEncoder;
use Psr\Http\Message\RequestInterface;
use Snagshout\Promote\Encoder\RawEncoder;
use Snagshout\Promote\Normalizer\NormalizerFactory;
use Snagshout\Promote\Resource\AbstractResource;
use Snagshout\Promote\Resource\DealsResource;
use Snagshout\Promote\Resource\FrontResource;
use Snagshout\Promote\Resource\UsersResource;
Expand All @@ -34,132 +25,27 @@
/**
* Class Client.
*
* @package Snagshout\Promote
*
* @author Eduardo Trujillo <[email protected]>
* @author Andrii Prykhodko <[email protected]>
* @package Snagshout\Promote
*/
class Client
{
/**
* @var string
* @var HttpClient
*/
protected $publicId;
protected $httpClient;

/**
* @var string
*/
protected $secretKey;

/**
* @var Uri
*/
protected $endpoint;

protected $baseUrl = 'http://localhost:8000';

/**
* SyndicationClient constructor.
* Client constructor.
*
* @param string $publicId
* @param string $secretKey
* @param string $baseUrl
*/
public function __construct($publicId, $secretKey)
{
$this->publicId = $publicId;
$this->secretKey = $secretKey;

$this->endpoint = new Uri($this->baseUrl);

$stack = new HandlerStack();

$stack->setHandler(new CurlHandler());

$stack->push($this->makeAuthHandler());

$this->client = new HttpClient(
[
'handler' => $stack,
]
);
}

/**
* @param string $publicId
*/
public function setPublicId($publicId)
public function __construct(string $publicId, string $secretKey, string $baseUrl)
{
$this->publicId = $publicId;
}

/**
* @param string $secretKey
*/
public function setSecretKey($secretKey)
{
$this->secretKey = $secretKey;
}

/**
* @param Uri $endpoint
*/
public function setEndpoint(Uri $endpoint)
{
$this->endpoint = $endpoint;
}

/**
* Computes the hash of a string using the secret key.
*
* @param string $content
*
* @return string
*/
protected function hash($content)
{
$timestamp = (new DateTime())->format('Y-m-d H');

return hash_hmac(
'sha512',
$content . $timestamp,
$this->secretKey
);
}

/**
* A Guzzle middleware that automatically adds the required Authorization
* and Content-Hash headers required by the Promote API.
*
* @return Closure
*/
protected function makeAuthHandler()
{
return function (callable $handler) {
return function (
RequestInterface $request,
array $options
) use ($handler) {
$contentHash = $this->hash($request->getBody());

$partialUri = $request->getUri();
$uri = $this->endpoint
->withPath(
$this->endpoint->getPath()
. $partialUri->getPath()
)
->withQuery($partialUri->getQuery())
->withFragment($partialUri->getFragment());

$request = $request
->withUri($uri)
->withHeader(
'Authorization',
vsprintf('Hash %s', [$this->publicId])
)
->withHeader('Content-Hash', $contentHash);

return $handler($request, $options);
};
};
$this->httpClient = new HttpClient($publicId, $secretKey, $baseUrl);
}

/**
Expand All @@ -169,19 +55,14 @@ protected function makeAuthHandler()
*
* @return Resource
*/
protected function buildResource($resourceClass)
protected function buildResource(string $resourceClass): AbstractResource
{
return new $resourceClass(
new GuzzleAdapter($this->client),
new GuzzleMessageFactory(),
$this->httpClient,
new Serializer(
NormalizerFactory::create(),
[
new JsonEncoder(
new JsonEncode(),
new JsonDecode()
),
new RawEncoder(),
new JsonEncoder(new JsonEncode(), new JsonDecode()),
]
)
);
Expand Down
60 changes: 60 additions & 0 deletions php/src/Snagshout/Promote/Encoder/RawEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Copyright 2016-2023, Snagshout <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This file is part of the Merchant package
*/

namespace Snagshout\Promote\Encoder;

use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;

/**
* Class RawEncoder.
*
* Encoder with no encoding (keeps the same output as the input)
*
* @author Andrii Prykhodko <[email protected]>
* @package Snagshout\Promote\Encoder
*/
class RawEncoder implements DecoderInterface, EncoderInterface
{
const FORMAT = 'raw';

/**
* {@inheritdoc}
*/
public function decode($data, $format, array $context = array())
{
return $data;
}

/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
{
return self::FORMAT === $format;
}

/**
* {@inheritdoc}
*/
public function encode($data, $format, array $context = array())
{
return $data;
}

/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
{
return self::FORMAT === $format;
}
}
25 changes: 25 additions & 0 deletions php/src/Snagshout/Promote/Enum/Fetch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Copyright 2016-2023, Snagshout <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This file is part of the Merchant package
*/

namespace Snagshout\Promote\Enum;

/**
* Class Fetch.
*
* @author Andrii Prykhodko <[email protected]>
* @package Snagshout\Promote\Resource
*/
final class Fetch
{
public const OBJECT = 'object';
public const PROMISE = 'promise';
public const RESPONSE = 'response';
}
Loading

0 comments on commit 175a9f2

Please sign in to comment.