Examples for presentation.

This commit is contained in:
giedrius
2024-11-19 13:57:03 +02:00
commit 41f2b5eec1
23 changed files with 3351 additions and 0 deletions

View 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;
}
}

View 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;
}
}

View 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);
}
}

View 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;
}
}

View 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);
}
}