Lines
83.33%
5 / 6
Functions and Methods
50.00%
1 / 2
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| TimelineEventProcessor | 83.33% | 5 / 6 | 50.00% | 1 / 2 | 3.04 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| process | 80.00% | 4 / 5 | 0.00% | 0 / 1 | 2.03 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace App\State; | |
| 6 | ||
| 7 | use ApiPlatform\Metadata\Operation; | |
| 8 | use ApiPlatform\State\ProcessorInterface; | |
| 9 | use App\Entity\TimelineEvent; | |
| 10 | use App\Entity\User; | |
| 11 | use Symfony\Bundle\SecurityBundle\Security; | |
| 12 | use Symfony\Component\DependencyInjection\Attribute\Autowire; | |
| 13 | ||
| 14 | /** | |
| 15 | * POST /api/timeline-events: the event belongs to the current user. | |
| 16 | * | |
| 17 | * @implements ProcessorInterface<TimelineEvent, TimelineEvent> | |
| 18 | */ | |
| 19 | final readonly class TimelineEventProcessor implements ProcessorInterface | |
| 20 | { | |
| 21 | /** | |
| 22 | * @param ProcessorInterface<TimelineEvent, TimelineEvent> $persistProcessor | |
| 23 | */ | |
| 24 | public function __construct( | |
| 25 | #[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')] | |
| 26 | private ProcessorInterface $persistProcessor, | |
| 27 | private Security $security, | |
| 28 | ) { | |
| 29 | } | |
| 30 | ||
| 31 | public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): TimelineEvent | |
| 32 | { | |
| 33 | $user = $this->security->getUser(); | |
| 34 | if (!$user instanceof User) { | |
| 35 | throw new \LogicException('The operation requires ROLE_USER.'); | |
| 36 | } | |
| 37 | ||
| 38 | $data->owner = $user; | |
| 39 | ||
| 40 | return $this->persistProcessor->process($data, $operation, $uriVariables, $context); | |
| 41 | } | |
| 42 | } |