Passed
Pull Request — master (#1918)
by Janko
25:50
created

UserTutorialRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 54
ccs 0
cts 35
cp 0
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findByUserAndViewContext() 0 18 1
A truncateByUser() 0 13 1
A delete() 0 5 1
A prototype() 0 3 1
A save() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Module\Control\ViewContext;
9
use Stu\Orm\Entity\TutorialStep;
10
use Stu\Orm\Entity\UserTutorial;
11
use Stu\Orm\Entity\UserTutorialInterface;
12
use Stu\Orm\Entity\UserInterface;
13
14
/**
15
 * @extends EntityRepository<UserTutorial>
16
 */
17
final class UserTutorialRepository extends EntityRepository implements UserTutorialRepositoryInterface
18
{
19
    public function prototype(): UserTutorialInterface
20
    {
21
        return new UserTutorial();
22
    }
23
24
    public function save(UserTutorialInterface $userTutorial): void
25
    {
26
        $em = $this->getEntityManager();
27
        $em->persist($userTutorial);
28
        $em->flush();
29
    }
30
31
    public function delete(UserTutorialInterface $userTutorial): void
32
    {
33
        $em = $this->getEntityManager();
34
        $em->remove($userTutorial);
35
        $em->flush();
36
    }
37
38
    public function truncateByUser(UserInterface $user): void
39
    {
40
        $this->getEntityManager()
41
            ->createQuery(
42
                sprintf(
43
                    'DELETE FROM %s ut WHERE ut.user = :user',
44
                    UserTutorial::class
45
                )
46
            )
47
            ->setParameters([
48
                'user' => $user
49
            ])
50
            ->execute();
51
    }
52
53
    public function findByUserAndViewContext(UserInterface $user, ViewContext $viewContext): ?UserTutorial
54
    {
55
        return $this->getEntityManager()->createQuery(
56
            sprintf(
57
                'SELECT ut FROM %s ut
58
                    JOIN %s ts
59
                    WITH ts.id = ut.tutorial_step_id
60
                    WHERE ut.user = :user
61
                    AND ts.module = :module
62
                    AND ts.view = :view',
63
                UserTutorial::class,
64
                TutorialStep::class
65
            )
66
        )->setParameters([
67
            'user' => $user,
68
            'module' => $viewContext->getModule()->value,
69
            'view' => $viewContext->getViewIdentifier(),
70
        ])->getOneOrNullResult();
71
    }
72
}
73