Lines
87.50%
7 / 8
Functions and Methods
50.00%
1 / 2
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AgentStatOwnerExtension | 87.50% | 7 / 8 | 50.00% | 1 / 2 | 3.02 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| applyToCollection | 85.71% | 6 / 7 | 0.00% | 0 / 1 | 2.01 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace App\Doctrine; | |
| 6 | ||
| 7 | use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; | |
| 8 | use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | |
| 9 | use ApiPlatform\Metadata\Operation; | |
| 10 | use App\Entity\AgentStat; | |
| 11 | use App\Entity\DoneBanner; | |
| 12 | use App\Entity\TimelineEvent; | |
| 13 | use Doctrine\ORM\QueryBuilder; | |
| 14 | use Symfony\Bundle\SecurityBundle\Security; | |
| 15 | ||
| 16 | /** | |
| 17 | * GET /api/agent-stats, /api/timeline-events and /api/done-banners list only the current user's | |
| 18 | * stats, events and done banners. | |
| 19 | */ | |
| 20 | final readonly class AgentStatOwnerExtension implements QueryCollectionExtensionInterface | |
| 21 | { | |
| 22 | public function __construct(private Security $security) | |
| 23 | { | |
| 24 | } | |
| 25 | ||
| 26 | public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void | |
| 27 | { | |
| 28 | if (!\in_array($resourceClass, [AgentStat::class, TimelineEvent::class, DoneBanner::class], true)) { | |
| 29 | return; | |
| 30 | } | |
| 31 | ||
| 32 | $rootAlias = $queryBuilder->getRootAliases()[0]; | |
| 33 | $parameter = $queryNameGenerator->generateParameterName('owner'); | |
| 34 | ||
| 35 | $queryBuilder | |
| 36 | ->andWhere(\sprintf('%s.owner = :%s', $rootAlias, $parameter)) | |
| 37 | ->setParameter($parameter, $this->security->getUser()); | |
| 38 | } | |
| 39 | } |