-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mrt1m
committed
Jul 7, 2021
1 parent
418b04f
commit 4268f9d
Showing
15 changed files
with
360 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PlaystationStoreApi\Actions; | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
use PlaystationStoreApi\ApiClients\GraphQL; | ||
use PlaystationStoreApi\Query\CatalogProducts; | ||
|
||
class Catalog | ||
{ | ||
protected GraphQL $graphQLApiClient; | ||
|
||
public function __construct(GraphQL $graphQLApiClient) | ||
{ | ||
$this->graphQLApiClient = $graphQLApiClient; | ||
} | ||
|
||
/** | ||
* @throws \Psr\Http\Client\ClientExceptionInterface | ||
* @throws \JsonException | ||
*/ | ||
public function products(CatalogProducts $query) : string | ||
{ | ||
$params = [ | ||
'operationName' => 'categoryGridRetrieve', | ||
'variables' => [ | ||
'id' => $query->categoryId(), | ||
'pageArgs' => [ | ||
'size' => $query->pagination()->size(), | ||
'offset' => $query->pagination()->offset(), | ||
], | ||
'sortBy' => [ | ||
'name' => $query->sorting()->fieldName(), | ||
'isAscending' => $query->sorting()->isAscending(), | ||
], | ||
'filterBy' => [], | ||
'facetOptions' => [], | ||
], | ||
'extensions' => [ | ||
'persistedQuery' => [ | ||
'version' => 1, | ||
'sha256Hash' => $query->sha256Hash(), | ||
], | ||
], | ||
]; | ||
|
||
$params['variables'] = json_encode($params['variables'], JSON_THROW_ON_ERROR); | ||
$params['extensions'] = json_encode($params['extensions'], JSON_THROW_ON_ERROR); | ||
|
||
return $this->graphQLApiClient->get(new Request('GET', '/op?' . http_build_query($params))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PlaystationStoreApi\ApiClients; | ||
|
||
use GuzzleHttp\Psr7\Uri; | ||
use PlaystationStoreApi\Enum\Region; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
abstract class BaseApiClient | ||
{ | ||
protected ClientInterface $client; | ||
protected string $region; | ||
protected string $language; | ||
protected UriInterface $uri; | ||
protected array $headers = []; | ||
protected Uri $basePath; | ||
|
||
public function __construct(Region $region, ClientInterface $client) | ||
{ | ||
$this->parseRegion($region); | ||
|
||
$this->client = $client; | ||
$this->uri = new Uri(); | ||
$this->basePath = new Uri(); | ||
} | ||
|
||
protected function parseRegion(Region $region) : void | ||
{ | ||
if ($result = explode('-', $region->getValue())) { | ||
$this->region = array_pop($result); | ||
$this->language = implode('-', $result); | ||
} else { | ||
throw new \UnexpectedValueException('The value [' . $region->getValue() . '] is not valid'); | ||
} | ||
} | ||
|
||
protected function mergeBasePath(UriInterface $uri) : UriInterface | ||
{ | ||
return $uri | ||
->withScheme($this->basePath->getScheme()) | ||
->withHost($this->basePath->getHost()) | ||
->withPath($this->basePath->getPath() . $uri->getPath()); | ||
} | ||
|
||
abstract public function get(RequestInterface $request) : string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PlaystationStoreApi\ApiClients; | ||
|
||
use GuzzleHttp\Psr7\Uri; | ||
use PlaystationStoreApi\Enum\Region; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class Chihiro extends BaseApiClient | ||
{ | ||
public function __construct(Region $region, ClientInterface $client) | ||
{ | ||
parent::__construct($region, $client); | ||
|
||
$this->basePath = new Uri('https://store.playstation.com/store/api/chihiro/00_09_000/container/' . strtoupper($this->region) . '/' . $this->language . '/999/'); | ||
} | ||
|
||
/** | ||
* @throws ClientExceptionInterface | ||
*/ | ||
public function get(RequestInterface $request) : string | ||
{ | ||
$response = $this->client->sendRequest( | ||
$request->withUri($this->mergeBasePath($request->getUri())) | ||
); | ||
|
||
return $response->getBody()->getContents(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PlaystationStoreApi\ApiClients; | ||
|
||
use GuzzleHttp\Psr7\Uri; | ||
use PlaystationStoreApi\Enum\Region; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class GraphQL extends BaseApiClient | ||
{ | ||
|
||
public function __construct(Region $region, ClientInterface $client) | ||
{ | ||
parent::__construct($region, $client); | ||
|
||
$this->basePath = new Uri('https://web.np.playstation.com/api/graphql/v1/'); | ||
} | ||
|
||
/** | ||
* @throws ClientExceptionInterface | ||
*/ | ||
public function get(RequestInterface $request) : string | ||
{ | ||
$response = $this->client->sendRequest( | ||
$request | ||
->withUri($this->mergeBasePath($request->getUri())) | ||
->withAddedHeader('x-psn-store-locale-override', $this->region . '-' . strtoupper($this->language)) | ||
); | ||
|
||
return $response->getBody()->getContents(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PlaystationStoreApi\Enum; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
class Category extends Enum | ||
{ | ||
public const PS4_GAMES = '44d8bb20-653e-431e-8ad0-c0a365f68d2f'; | ||
public const PS5_GAMES = '4cbf39e2-5749-4970-ba81-93a489e4570c'; | ||
public const PS_PLUS = '038b4df3-bb4c-48f8-8290-3feb35f0f0fd'; | ||
public const SALES = '803cee19-e5a1-4d59-a463-0b6b2701bf7c'; | ||
public const EA_GAMES = '74d4e266-5c64-4c61-a7e3-1b6e78f643e6'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PlaystationStoreApi\Enum; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
class SortingDirection extends Enum | ||
{ | ||
public const ASC = true; | ||
public const DESC = false; | ||
} |
Oops, something went wrong.