Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
89.47% covered (warning)
89.47%
17 / 19
86.67% covered (warning)
86.67%
13 / 15
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
SubjectService
89.47% covered (warning)
89.47%
17 / 19
86.67% covered (warning)
86.67%
13 / 15
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%
5 / 5
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
 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
75.00% covered (warning)
75.00%
3 / 4
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\Entity\GrammaticalRole;
6use App\Entity\Subject;
7use App\Entity\Word;
8use App\Enum\GrammaticalRoleType;
9use App\Repository\SubjectRepositoryInterface;
10use App\Specification\Criteria;
11use App\Specification\Criterion\ValueCriterion;
12use App\Specification\Criterion\ValueCriterionCheck;
13use Doctrine\ORM\EntityManagerInterface;
14use Psr\Clock\ClockInterface;
15use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
16
17/**
18 * @author Wilhelm Zwertvaegher
19 */
20#[AsTaggedItem(index: GrammaticalRoleType::SUBJECT->value)]
21class SubjectService implements SubjectServiceInterface
22{
23    public function __construct(
24        private readonly SubjectRepositoryInterface $repository,
25        private readonly EntityManagerInterface $entityManager,
26        private readonly ClockInterface $clock,
27    ) {
28    }
29
30    public function createOrUpdate(Word $word): Subject
31    {
32        $subject = $this->repository->findByWordId($word->getId());
33        if (!$subject) {
34            $subject = new Subject($word);
35        }
36        $this->save($subject);
37
38        return $subject;
39    }
40
41    public function deleteIfExists(int $wordId): void
42    {
43        $subject = $this->repository->findByWordId($wordId);
44        if ($subject) {
45            $this->entityManager->remove($subject);
46        }
47    }
48
49    public function save(Subject $subject): void
50    {
51        $this->entityManager->persist($subject);
52    }
53
54    public function findOneRandomly(Criteria $criteria): ?Subject
55    {
56        return $this->repository->findOne($criteria);
57    }
58
59    public function getGrammaticalRole(): GrammaticalRoleType
60    {
61        return GrammaticalRoleType::SUBJECT;
62    }
63
64    /**
65     * @return ?Subject
66     */
67    public function findByWordId(int $wordId): ?GrammaticalRole
68    {
69        return $this->repository->findByWordId($wordId);
70    }
71
72    public function findSimilar(GrammaticalRole $other, Criteria $criteria): ?GrammaticalRole
73    {
74        if (!$other instanceof Subject) {
75            throw new \LogicException('Cannot find another subject because $other param is not an instance of Subject');
76        }
77
78        $criteria->addCriterion(new ValueCriterion(Word::class, 'id', $other->getWord()->getId(), ValueCriterionCheck::NEQ));
79
80        return $this->repository->findOne($criteria);
81    }
82
83    /**
84     * @param Subject $grammaticalRole
85     */
86    public function incrementUsageCount(GrammaticalRole $grammaticalRole): void
87    {
88        $grammaticalRole->incrementUsageCount($this->clock->now());
89        $this->save($grammaticalRole);
90    }
91}