Passed
Pull Request — master (#1918)
by Janko
49:46 queued 19:04
created

UserTutorialRepository::truncateByUserAndStepId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 16
ccs 0
cts 14
cp 0
crap 2
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Orm\Entity\TutorialStep;
9
use Stu\Orm\Entity\UserTutorial;
10
use Stu\Orm\Entity\UserTutorialInterface;
11
use Stu\Orm\Entity\UserInterface;
12
use Stu\Module\Control\ViewContext;
13
use Stu\Orm\Entity\User;
14
15
/**
16
 * @extends EntityRepository<UserTutorial>
17
 */
18
final class UserTutorialRepository extends EntityRepository implements UserTutorialRepositoryInterface
19
{
20
    public function prototype(): UserTutorialInterface
21
    {
22
        return new UserTutorial();
23
    }
24
25
    public function save(UserTutorialInterface $userTutorial): void
26
    {
27
        $em = $this->getEntityManager();
28
        $em->persist($userTutorial);
29
        $em->flush();
30
    }
31
32
    public function delete(UserTutorialInterface $userTutorial): void
33
    {
34
        $em = $this->getEntityManager();
35
        $em->remove($userTutorial);
36
        $em->flush();
37
    }
38
39
    /**
40
     * @return UserTutorialInterface[]
41
     */
42
    public function findByUser(UserInterface $user): array
43
    {
44
        return $this->findBy(['user' => $user]);
45
    }
46
47
    public function truncateByUser(UserInterface $user): void
48
    {
49
        $this->getEntityManager()
50
            ->createQuery(
51
                sprintf(
52
                    'DELETE FROM %s ut WHERE ut.user = :user',
53
                    UserTutorial::class
54
                )
55
            )
56
            ->setParameters([
57
                'user' => $user
58
            ])
59
            ->execute();
60
    }
61
62
63
    public function truncateByUserAndStepId(UserInterface $user, int $stepId): void
64
    {
65
        $this->getEntityManager()
66
            ->createQuery(
67
                sprintf(
68
                    'DELETE FROM %s ut
69
                    WHERE ut.user = :user
70
                    AND ut.tutorial_step_id = :stepId',
71
                    UserTutorial::class
72
                )
73
            )
74
            ->setParameters([
75
                'user' => $user,
76
                'stepId' => $stepId
77
            ])
78
            ->execute();
79
    }
80
81
    public function findUserTutorialByUserAndViewContext(UserInterface $user, ViewContext $viewContext): ?UserTutorial
82
    {
83
        $tutorialSteps = $this->getEntityManager()->createQuery(
84
            sprintf(
85
                'SELECT ts.id FROM %s ts
86
             WHERE ts.module = :module
87
             AND ts.view = :view',
88
                TutorialStep::class
89
            )
90
        )->setParameters([
91
            'module' => $viewContext->getModule()->value,
92
            'view' => $viewContext->getViewIdentifier(),
93
        ])->getResult();
94
95
        if (empty($tutorialSteps)) {
96
            return null;
97
        }
98
99
100
        $stepIds = array_map(fn($step) => $step['id'], $tutorialSteps);
101
102
103
        return $this->getEntityManager()->createQuery(
104
            sprintf(
105
                'SELECT ut FROM %s ut
106
             JOIN %s ts WITH ut.tutorial_step_id = ts.id
107
             WHERE ut.user = :user
108
             AND ut.tutorial_step_id IN (:stepIds)',
109
                UserTutorial::class,
110
                TutorialStep::class
111
            )
112
        )->setParameters([
113
            'user' => $user,
114
            'stepIds' => $stepIds,
115
        ])->getOneOrNullResult();
116
    }
117
118
    public function findUserTutorialByUserAndView(UserInterface $user, string $view): ?UserTutorial
119
    {
120
        $tutorialSteps = $this->getEntityManager()->createQuery(
121
            sprintf(
122
                'SELECT ts.id FROM %s ts
123
             WHERE ts.view = :view',
124
                TutorialStep::class
125
            )
126
        )->setParameters([
127
            'view' => $view,
128
        ])->getResult();
129
130
        if (empty($tutorialSteps)) {
131
            return null;
132
        }
133
134
135
        $stepIds = array_map(fn($step) => $step['id'], $tutorialSteps);
136
137
138
        return $this->getEntityManager()->createQuery(
139
            sprintf(
140
                'SELECT ut FROM %s ut
141
             JOIN %s ts WITH ut.tutorial_step_id = ts.id
142
             WHERE ut.user = :user
143
             AND ut.tutorial_step_id IN (:stepIds)',
144
                UserTutorial::class,
145
                TutorialStep::class
146
            )
147
        )->setParameters([
148
            'user' => $user,
149
            'stepIds' => $stepIds,
150
        ])->getOneOrNullResult();
151
    }
152
}