-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStatisticsProvider.php
71 lines (61 loc) · 2.7 KB
/
StatisticsProvider.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
* This file is part of the ESQL project.
*
* (c) Antoine Bluchet <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Soyuka\ESQL\Tests\Fixtures\TestBundle\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\Persistence\ManagerRegistry;
use Soyuka\ESQL\Bridge\ApiPlatform\State\DataPaginator;
use Soyuka\ESQL\ESQLInterface;
use Soyuka\ESQL\ESQLMapperInterface;
use Soyuka\ESQL\Tests\Fixtures\TestBundle\Entity\Car;
use Soyuka\ESQL\Tests\Fixtures\TestBundle\Model\CarStatistics;
use Symfony\Component\HttpFoundation\RequestStack;
final class StatisticsProvider implements ProviderInterface
{
private readonly ESQLMapperInterface $mapper;
public function __construct(private readonly RequestStack $requestStack, private readonly ManagerRegistry $managerRegistry, private readonly ESQLInterface $esql, private readonly DataPaginator $dataPaginator)
{
}
/**
* {@inheritDoc}
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$connection = $this->managerRegistry->getConnection();
$parameters = [];
$car = $this->esql->__invoke(Car::class, CarStatistics::class, 'car');
/** @var string */
$groupBy = $car->columns(['sold', 'color'], $car::AS_STRING | $car::WITHOUT_ALIASES);
$driver = $connection->getDriver()->getDatabasePlatform();
$query = match (true) {
$driver instanceof SQLServerPlatform => <<<SQL
SELECT AVG(CAST(car.price as BIGINT)) as car_totalPrice, CONCAT(CASE WHEN car.sold = '1' THEN 'sold' ELSE 'not sold' END, COALESCE(car.color, 'No color information')) as car_identifier, {$car->columns(['sold', 'color'])}
FROM {$car->table()}
GROUP BY {$groupBy}
ORDER BY AVG(CAST(car.price as BIGINT)) ASC
SQL,
default => <<<SQL
SELECT AVG(car.price) as car_totalPrice, car.sold || COALESCE(car.color, 'No color information') as car_identifier, {$car->columns(['sold', 'color'])}
FROM {$car->table()}
GROUP BY {$groupBy}
ORDER BY AVG(car.price) ASC
SQL,
};
if ($paginator = $this->dataPaginator->getPaginator($operation)) {
return $paginator($car, $query, $parameters, $context);
}
$stmt = $connection->prepare($query);
$result = $stmt->executeQuery($parameters);
$data = $result->fetchAssociative();
return $car->map($data);
}
}