Examples for presentation.
This commit is contained in:
38
sources/Example/Good/ApiClient.php
Normal file
38
sources/Example/Good/ApiClient.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Good;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class ApiClient
|
||||
{
|
||||
|
||||
private Client $client;
|
||||
|
||||
public function __construct(Client $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Product[]
|
||||
*/
|
||||
public function getProducts(string $filter): array
|
||||
{
|
||||
$response = $this->client->request('GET', 'api_endpoint/products', [
|
||||
'query' => ['filter' => $filter]
|
||||
]);
|
||||
|
||||
$json = $response->getBody()->getContents();
|
||||
|
||||
|
||||
$data = json_decode($json, true);
|
||||
|
||||
$products = [];
|
||||
foreach ($data as $productArray) {
|
||||
$products[] = (new Product())->setData($productArray);
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
}
|
||||
43
sources/Example/Good/Controller.php
Normal file
43
sources/Example/Good/Controller.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Good;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class Controller
|
||||
{
|
||||
private ApiClient $apiClient;
|
||||
private Repository $repository;
|
||||
private VATCalculator $vatCalculator;
|
||||
|
||||
public function __construct(ApiClient $apiClient, Repository $repository, VATCalculator $vatCalculator)
|
||||
{
|
||||
$this->apiClient = $apiClient;
|
||||
$this->repository = $repository;
|
||||
$this->vatCalculator = $vatCalculator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Product[]
|
||||
*/
|
||||
public function getProducts(RequestInterface $request) : array {
|
||||
$params = $request->getBody();
|
||||
|
||||
$products = $this->apiClient->getProducts($params);
|
||||
|
||||
$skus = [];
|
||||
foreach ($products as $product) {
|
||||
$skus[] = $product->sku;
|
||||
}
|
||||
|
||||
$dbProducts = $this->repository->getProducts($skus);
|
||||
|
||||
Product::copyQuantities($dbProducts, $products);
|
||||
|
||||
$this->vatCalculator->applyVat($dbProducts);
|
||||
|
||||
return $dbProducts;
|
||||
}
|
||||
|
||||
}
|
||||
58
sources/Example/Good/Product.php
Normal file
58
sources/Example/Good/Product.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Good;
|
||||
|
||||
class Product
|
||||
{
|
||||
public string $sku = '';
|
||||
public int $quantity = 0;
|
||||
public float $price = 0;
|
||||
|
||||
public function setSku(string $sku): Product
|
||||
{
|
||||
$this->sku = $sku;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setQuantity(int $quantity): Product
|
||||
{
|
||||
$this->quantity = $quantity;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPrice(float $price): Product
|
||||
{
|
||||
$this->price = $price;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Instead of this better to use some DAO lib like JMSSerializer, or cuyz/valinor.
|
||||
*/
|
||||
public function setData(array $data): Product {
|
||||
$this->sku = $data['sku'] ?? '';
|
||||
$this->quantity = $data['amount'] ?? 0;
|
||||
$this->price = $data['price'] ?? 0;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product[] $destinationProducts
|
||||
* @param Product[] $sourceProducts
|
||||
*/
|
||||
public static function copyQuantities(array $destinationProducts, array $sourceProducts ) {
|
||||
$indexedBySku = [];
|
||||
foreach ($destinationProducts as $destinationProduct) {
|
||||
$indexedBySku[$destinationProduct->sku] = $destinationProduct;
|
||||
}
|
||||
|
||||
foreach ($sourceProducts as $sourceProduct) {
|
||||
if ( !array_key_exists($sourceProduct->sku, $indexedBySku) ) {
|
||||
continue;
|
||||
}
|
||||
$indexedBySku[$sourceProduct->sku]->setQuantity($sourceProduct->quantity);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
sources/Example/Good/Repository.php
Normal file
29
sources/Example/Good/Repository.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Good;
|
||||
|
||||
use PDO;
|
||||
|
||||
class Repository
|
||||
{
|
||||
private PDO $pdo;
|
||||
|
||||
public function __construct(PDO $pdo)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Product[]
|
||||
*/
|
||||
public function getProducts($skus) : array{
|
||||
$quotedSkus = [];
|
||||
foreach($skus as $sku){
|
||||
$quotedSkus[] = $this->pdo->quote($sku);
|
||||
}
|
||||
$skusStr = implode(',', $quotedSkus);
|
||||
$sql = "SELECT * FROM products where id in ($skusStr)";
|
||||
|
||||
return $this->pdo->query($sql)->fetchAll(PDO::FETCH_CLASS, Product::class);
|
||||
}
|
||||
}
|
||||
25
sources/Example/Good/VATCalculator.php
Normal file
25
sources/Example/Good/VATCalculator.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Good;
|
||||
|
||||
class VATCalculator
|
||||
{
|
||||
private float $vat;
|
||||
|
||||
/**
|
||||
* @param float $vat
|
||||
*/
|
||||
public function __construct(float $vat)
|
||||
{
|
||||
$this->vat = $vat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product[] $products
|
||||
*/
|
||||
public function applyVat(array $products) : void {
|
||||
foreach ($products as $product) {
|
||||
$product->setPrice( $product->price * (1+$this->vat/100) );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user