Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
7 / 7 |
|
83.33% |
5 / 6 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| NickService | |
100.00% |
17 / 17 |
|
100.00% |
7 / 7 |
|
83.33% |
5 / 6 |
|
100.00% |
5 / 5 |
6.17 | |
100.00% |
1 / 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 | |||
| getNick | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| incrementUsageCount | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOrCreate | |
100.00% |
12 / 12 |
|
100.00% |
3 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Service\Data; |
| 4 | |
| 5 | use App\Entity\Nick; |
| 6 | use App\Entity\Qualifier; |
| 7 | use App\Entity\Subject; |
| 8 | use App\Enum\OffenseLevel; |
| 9 | use App\Enum\WordGender; |
| 10 | use App\Repository\NickRepositoryInterface; |
| 11 | use Doctrine\ORM\EntityManagerInterface; |
| 12 | use Psr\Clock\ClockInterface; |
| 13 | |
| 14 | /** |
| 15 | * @author Wilhelm Zwertvaegher |
| 16 | */ |
| 17 | readonly class NickService implements NickServiceInterface |
| 18 | { |
| 19 | public function __construct( |
| 20 | private NickRepositoryInterface $repository, |
| 21 | private EntityManagerInterface $entityManager, |
| 22 | private ClockInterface $clock, |
| 23 | ) { |
| 24 | } |
| 25 | |
| 26 | public function save(Nick $nick): void |
| 27 | { |
| 28 | $this->entityManager->persist($nick); |
| 29 | } |
| 30 | |
| 31 | public function getNick(int $id): ?Nick |
| 32 | { |
| 33 | return $this->repository->getById($id); |
| 34 | } |
| 35 | |
| 36 | public function incrementUsageCount(Nick $nick): void |
| 37 | { |
| 38 | $nick->incrementUsageCount(); |
| 39 | $this->save($nick); |
| 40 | } |
| 41 | |
| 42 | public function getOrCreate(Subject $subject, Qualifier $qualifier, WordGender $targetGender, OffenseLevel $offenseLevel, string $label): Nick |
| 43 | { |
| 44 | if (!($nick = $this->repository->getByProperties($subject, $qualifier, $targetGender))) { |
| 45 | $nick = new Nick( |
| 46 | $label, |
| 47 | $subject, |
| 48 | $qualifier, |
| 49 | $targetGender, |
| 50 | $offenseLevel, |
| 51 | $now = $this->clock->now(), |
| 52 | $now |
| 53 | ); |
| 54 | $this->save($nick); |
| 55 | } |
| 56 | |
| 57 | return $nick; |
| 58 | } |
| 59 | } |