Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
87.50% |
14 / 16 |
|
71.43% |
5 / 7 |
|
60.00% |
3 / 5 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ReportService | |
87.50% |
14 / 16 |
|
71.43% |
5 / 7 |
|
60.00% |
3 / 5 |
|
66.67% |
2 / 3 |
6.60 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| save | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
85.71% |
12 / 14 |
|
60.00% |
3 / 5 |
|
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
5.67 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Service\Data; |
| 4 | |
| 5 | use App\Dto\Command\CreateReportCommand; |
| 6 | use App\Entity\Report; |
| 7 | use App\Exception\NickNotFoundException; |
| 8 | use App\Repository\NickRepositoryInterface; |
| 9 | use App\Repository\ReportRepositoryInterface; |
| 10 | use Doctrine\ORM\EntityManagerInterface; |
| 11 | use Psr\Clock\ClockInterface; |
| 12 | |
| 13 | /** |
| 14 | * @author Wilhelm Zwertvaegher |
| 15 | */ |
| 16 | readonly class ReportService implements ReportServiceInterface |
| 17 | { |
| 18 | public function __construct( |
| 19 | private NickRepositoryInterface $nickRepository, |
| 20 | private ReportRepositoryInterface $reportRepository, |
| 21 | private EntityManagerInterface $entityManager, |
| 22 | private ClockInterface $clock, |
| 23 | ) { |
| 24 | } |
| 25 | |
| 26 | public function save(Report $report): void |
| 27 | { |
| 28 | $this->entityManager->persist($report); |
| 29 | } |
| 30 | |
| 31 | public function create(CreateReportCommand $command): Report |
| 32 | { |
| 33 | $nick = $this->nickRepository->getById($command->getNickId()); |
| 34 | if (null === $nick) { |
| 35 | throw new NickNotFoundException(); |
| 36 | } |
| 37 | |
| 38 | $existingReport = $this->reportRepository->getByNickIdAndSenderEmail($nick->getId(), $command->getSenderEmail()); |
| 39 | if (null !== $existingReport) { |
| 40 | return $existingReport; |
| 41 | } |
| 42 | |
| 43 | $report = new Report( |
| 44 | senderEmail: $command->getSenderEmail(), |
| 45 | reason: $command->getReason(), |
| 46 | nick: $nick, |
| 47 | createdAt: $this->clock->now() |
| 48 | ); |
| 49 | |
| 50 | $this->save($report); |
| 51 | |
| 52 | return $report; |
| 53 | } |
| 54 | } |