Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
90.48% covered (success)
90.48%
19 / 21
87.50% covered (warning)
87.50%
14 / 16
83.33% covered (warning)
83.33%
10 / 12
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
QualifierService
90.48% covered (success)
90.48%
19 / 21
87.50% covered (warning)
87.50%
14 / 16
83.33% covered (warning)
83.33%
10 / 12
77.78% covered (warning)
77.78%
7 / 9
12.67
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createOrUpdate
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 deleteIfExists
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 save
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 findOneRandomly
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGrammaticalRole
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findByWordId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 findSimilar
80.00% covered (warning)
80.00%
4 / 5
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 incrementUsageCount
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Service\Data;
4
5use App\Dto\Properties\MaintainQualifierProperties;
6use App\Entity\GrammaticalRole;
7use App\Entity\Qualifier;
8use App\Entity\Word;
9use App\Enum\GrammaticalRoleType;
10use App\Repository\QualifierRepositoryInterface;
11use App\Specification\Criteria;
12use App\Specification\Criterion\ValueCriterion;
13use App\Specification\Criterion\ValueCriterionCheck;
14use Doctrine\ORM\EntityManagerInterface;
15use Psr\Clock\ClockInterface;
16use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
17
18/**
19 * @author Wilhelm Zwertvaegher
20 */
21#[AsTaggedItem(index: GrammaticalRoleType::QUALIFIER->value)]
22class QualifierService implements QualifierServiceInterface
23{
24    public function __construct(
25        private readonly QualifierRepositoryInterface $repository,
26        private readonly EntityManagerInterface $entityManager,
27        private readonly ClockInterface $clock,
28    ) {
29    }
30
31    public function createOrUpdate(Word $word, MaintainQualifierProperties $command): Qualifier
32    {
33        $qualifier = $this->repository->findByWordId($word->getId());
34        if ($qualifier) {
35            $qualifier->setPosition($command->getPosition());
36        } else {
37            $qualifier = new Qualifier($word, $command->getPosition());
38        }
39        $this->save($qualifier);
40
41        return $qualifier;
42    }
43
44    public function deleteIfExists(int $wordId): void
45    {
46        $qualifier = $this->repository->findByWordId($wordId);
47        if ($qualifier) {
48            $this->entityManager->remove($qualifier);
49        }
50    }
51
52    public function save(Qualifier $qualifier): void
53    {
54        $this->entityManager->persist($qualifier);
55    }
56
57    public function findOneRandomly(Criteria $criteria): ?Qualifier
58    {
59        return $this->repository->findOne($criteria);
60    }
61
62    public function getGrammaticalRole(): GrammaticalRoleType
63    {
64        return GrammaticalRoleType::QUALIFIER;
65    }
66
67    /**
68     * @return ?Qualifier
69     */
70    public function findByWordId(int $wordId): ?GrammaticalRole
71    {
72        return $this->repository->findByWordId($wordId);
73    }
74
75    public function findSimilar(GrammaticalRole $other, Criteria $criteria): ?GrammaticalRole
76    {
77        if (!$other instanceof Qualifier) {
78            throw new \LogicException('Cannot find another qualifier because $other param is not an instance of Qualifier');
79        }
80
81        $criteria->addCriterion(new ValueCriterion(Word::class, 'id', $other->getWord()->getId(), ValueCriterionCheck::NEQ));
82
83        // add qualifier position to criteria
84        $criteria->addCriterion(new ValueCriterion(Qualifier::class, 'position', $other->getPosition(), ValueCriterionCheck::EQ));
85
86        return $this->repository->findOne($criteria);
87    }
88
89    /**
90     * @param Qualifier $grammaticalRole
91     */
92    public function incrementUsageCount(GrammaticalRole $grammaticalRole): void
93    {
94        $grammaticalRole->incrementUsageCount($this->clock->now());
95        $this->save($grammaticalRole);
96    }
97}