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

TutorialStepRepository::findAllFirstSteps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Stu\Orm\Repository;
4
5
use Couchbase\View;
6
use Doctrine\ORM\EntityRepository;
7
use Stu\Module\Control\ViewContext;
8
use Stu\Orm\Entity\TutorialStep;
9
use Stu\Orm\Entity\UserInterface;
10
use Stu\Orm\Entity\UserTutorial;
11
12
/**
13
 * @extends EntityRepository<TutorialStep>
14
 */
15
final class TutorialStepRepository extends EntityRepository implements TutorialStepRepositoryInterface
16
{
17
    public function findByUserAndViewContext(UserInterface $user, ViewContext $viewContext): array
18
    {
19
        $subquery = $this->getEntityManager()->createQuery(
20
            sprintf(
21
                'SELECT ts.id
22
             FROM %s ts
23
             JOIN %s ut WITH ts.id = ut.tutorial_step_id
24
             WHERE ut.user = :user
25
             AND ts.module = :module
26
             AND ts.view = :view',
27
                TutorialStep::class,
28
                UserTutorial::class
29
            )
30
        )->setParameters([
31
            'user' => $user,
32
            'module' => $viewContext->getModule()->value,
33
            'view' => $viewContext->getViewIdentifier(),
34
        ]);
35
36
        $result = $subquery->getOneOrNullResult();
37
38
        if (!$result) {
39
            return [];
40
        }
41
42
43
        return $this->getEntityManager()->createQuery(
44
            sprintf(
45
                'SELECT ts FROM %s ts
46
             WHERE ts.module = :module
47
             AND ts.view = :view',
48
                TutorialStep::class
49
            )
50
        )->setParameters([
51
            'module' => $viewContext->getModule()->value,
52
            'view' => $viewContext->getViewIdentifier(),
53
        ])->getResult();
54
    }
55
56
    public function findAllFirstSteps(): array
57
    {
58
        return $this->getEntityManager()
59
            ->createQuery(
60
                sprintf(
61
                    'SELECT ts FROM %1$s ts
62
                    WHERE NOT EXISTS (SELECT ts2.id FROM %1$s ts2
63
                                        WHERE ts2.next_step_id = ts.id)',
64
                    TutorialStep::class
65
                )
66
            )
67
            ->getResult();
68
    }
69
}
70