Lines
77.77%
7 / 9
Methods
50.00%
1 / 2
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| create | 75.00% | 6 / 8 | 0.00% | 0 / 1 | 3.14 | ||
| 15 | class AgentStatService | |
| 16 | { | |
| 17 | public function __construct( | |
| 18 | private readonly EntityManagerInterface $entityManager, | |
| 19 | private readonly AgentStatRepository $agentStatRepository, | |
| 20 | ) { | |
| 21 | } | |
| 22 | ||
| 23 | /** | |
| 24 | * @throws StatsAlreadyAddedException if the owner already imported stats with this datetime | |
| 25 | */ | |
| 26 | public function create(User $owner, AgentStatInput $input): AgentStat | |
| 27 | { | |
| 28 | $agentStat = new AgentStat($owner, $input); | |
| 29 | ||
| 30 | if ($this->agentStatRepository->has($owner, $agentStat->datetime)) { | |
| 31 | throw new StatsAlreadyAddedException('Stat entry already added!'); | |
| 32 | } | |
| 33 | ||
| 34 | $this->entityManager->persist($agentStat); | |
| 35 | ||
| 36 | try { | |
| 37 | $this->entityManager->flush(); | |
| 38 | } catch (UniqueConstraintViolationException $uniqueConstraintViolationException) { | |
| 39 | // Same stats sent twice at the same moment | |
| 40 | throw new StatsAlreadyAddedException('Stat entry already added!', $uniqueConstraintViolationException->getCode(), previous: $uniqueConstraintViolationException); | |
| 41 | } | |
| 42 | ||
| 43 | return $agentStat; | |
| 44 | } | |
| 45 | } |