Examples for presentation.
This commit is contained in:
57
sources/Example/Bad/APIService.php
Normal file
57
sources/Example/Bad/APIService.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Bad;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class APIService
|
||||
{
|
||||
private Repository $dbService;
|
||||
|
||||
private Client $client;
|
||||
|
||||
public function __construct(Repository $dbService, Client $client)
|
||||
{
|
||||
$this->dbService = $dbService;
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function getApiData(string $filter) : array
|
||||
{
|
||||
$response = $this->client->request('GET', 'api_endpoint/products', [
|
||||
'query' => ['filter'=>$filter]
|
||||
]);
|
||||
|
||||
$json = $response->getBody()->getContents();
|
||||
|
||||
|
||||
return json_decode($json, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Product[]
|
||||
*/
|
||||
public function getProducts(string $filter) : array {
|
||||
|
||||
$data = $this->getApiData($filter);
|
||||
|
||||
$skus = [];
|
||||
foreach ($data as $product) {
|
||||
$skus[] = $product['sku'];
|
||||
}
|
||||
$products = $this->dbService->getProducts($skus);
|
||||
|
||||
/** @var Product[] $productsMap */
|
||||
$productsMap = [];
|
||||
foreach ($products as $product) {
|
||||
$productsMap[$product->sku] = $product;
|
||||
}
|
||||
|
||||
foreach ($data as $apiProduct) {
|
||||
$productsMap[$apiProduct['sku']]->quantity = $apiProduct['amount'];
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
}
|
||||
29
sources/Example/Bad/CalculatorService.php
Normal file
29
sources/Example/Bad/CalculatorService.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Bad;
|
||||
|
||||
class CalculatorService
|
||||
{
|
||||
private float $vatPercent;
|
||||
private APIService $apiService;
|
||||
|
||||
public function __construct(float $vatPercent, APIService $apiService)
|
||||
{
|
||||
$this->vatPercent = $vatPercent;
|
||||
$this->apiService = $apiService;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Product[]
|
||||
*/
|
||||
public function getProducts(string $requestData) : array {
|
||||
$products = $this->apiService->getProducts($requestData);
|
||||
|
||||
foreach ($products as $product) {
|
||||
$product->price = $product->price * ( 1 + $this->vatPercent);
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
}
|
||||
23
sources/Example/Bad/Controller.php
Normal file
23
sources/Example/Bad/Controller.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Bad;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
class Controller
|
||||
{
|
||||
private CalculatorService $calculator;
|
||||
|
||||
public function __construct(CalculatorService $calculator)
|
||||
{
|
||||
$this->calculator = $calculator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Product[]
|
||||
*/
|
||||
public function getProducts(RequestInterface $request) : array {
|
||||
$params = $request->getBody();
|
||||
return $this->calculator->getProducts($params);
|
||||
}
|
||||
}
|
||||
30
sources/Example/Bad/Product.php
Normal file
30
sources/Example/Bad/Product.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Bad;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
29
sources/Example/Bad/Repository.php
Normal file
29
sources/Example/Bad/Repository.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Example\Bad;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user