-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.php
35 lines (32 loc) · 1.42 KB
/
product.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
require_once('vendor/autoload.php');
use FurnitureStoreApi\Products\ProductHydrator;
use FurnitureStoreApi\Services\ResponseService;
use FurnitureStoreApi\Exceptions\InvalidProductIdException;
use FurnitureStoreApi\Exceptions\InvalidUnitOfMeasureException;
use FurnitureStoreApi\Services\UnitConversionService;
use FurnitureStoreApi\Services\CurrencyConversionClass;
use FurnitureStoreApi\Exceptions\InvalidCurrencyException;
use FurnitureStoreApi\Services\HeaderService;
HeaderService::setHeaders();
try {
if ($_GET['id'] > ProductHydrator::getMaxProducts() || !is_numeric($_GET['id'])) {
throw new InvalidProductIdException();
} else {
if (isset($_GET['unit'])) {
UnitConversionService::setUnit($_GET['unit']);
}
if (isset($_GET['currency'])) {
CurrencyConversionClass::setCurrency($_GET['currency']);
}
$resultsArray = ProductHydrator::getProductById($_GET['id']);
echo ResponseService::makeResponse("Successfully retrieved product", $resultsArray, 200);
}
}
catch (InvalidProductIdException $exception) {
echo ResponseService::makeResponse($exception->getMessage(), [], 400);
} catch (InvalidUnitOfMeasureException $exception) {
echo ResponseService::makeResponse($exception->getMessage(), [], 400);
} catch (InvalidCurrencyException $exception) {
echo ResponseService::makeResponse($exception->getMessage(), [], 400);
}