38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\GraphQLBundle\ApiShared\Util;
|
|
|
|
use KupShop\GraphQLBundle\Exception\GraphQLNotFoundException;
|
|
use Query\Operator;
|
|
|
|
/**
|
|
* Shared validations for API.
|
|
*/
|
|
class ApiValidator
|
|
{
|
|
public function productExists(int $productId, ?int $variationId = null, ?string $sourceFieldName = null): void
|
|
{
|
|
$equals = ['id' => $variationId ?: $productId];
|
|
if ($variationId) {
|
|
$equals['id_product'] = $productId;
|
|
}
|
|
|
|
$id = sqlQueryBuilder()
|
|
->select('id')
|
|
->from($variationId ? 'products_variations' : 'products')
|
|
->where(Operator::equals($equals))
|
|
->execute()->fetchOne();
|
|
|
|
if (!$id) {
|
|
$message = sprintf('Product with ID "%s" was not found!', $productId);
|
|
if ($variationId) {
|
|
$message = sprintf('Variation with ID "%s" and Product ID "%s" was not found!', $variationId, $productId);
|
|
}
|
|
|
|
throw new GraphQLNotFoundException($message, extensions: ['field' => $sourceFieldName]);
|
|
}
|
|
}
|
|
}
|