Passed
Pull Request — master (#6239)
by
unknown
08:01
created

CLpItemRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 51
rs 10
c 1
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 4 1
A getRootItem() 0 5 1
A findItemsByLearningPathAndType() 0 12 1
A findLearningPathsUsingDocument() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Repository;
8
9
use Chamilo\CoreBundle\Traits\NonResourceRepository;
10
use Chamilo\CoreBundle\Traits\Repository\ORM\NestedTreeRepositoryTrait;
11
use Chamilo\CourseBundle\Entity\CLpItem;
12
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
13
use Doctrine\Persistence\ManagerRegistry;
14
15
final class CLpItemRepository extends ServiceEntityRepository
16
{
17
    use NestedTreeRepositoryTrait;
18
    use NonResourceRepository;
19
20
    public function __construct(ManagerRegistry $registry)
21
    {
22
        parent::__construct($registry, CLpItem::class);
23
24
        $this->initializeTreeRepository($this->getEntityManager(), $this->getClassMetadata());
25
    }
26
27
    public function create(CLpItem $item): void
28
    {
29
        $this->getEntityManager()->persist($item);
30
        $this->getEntityManager()->flush();
31
    }
32
33
    public function getRootItem(int $lpId): ?CLpItem
34
    {
35
        return $this->findOneBy([
36
            'path' => 'root',
37
            'lp' => $lpId,
38
        ]);
39
    }
40
41
    public function findItemsByLearningPathAndType(int $learningPathId, string $itemType): array
42
    {
43
        $qb = $this->createQueryBuilder('i')
44
            ->where('i.lp = :learningPathId')
45
            ->andWhere('i.itemType = :itemType')
46
            ->setParameter('learningPathId', $learningPathId)
47
            ->setParameter('itemType', $itemType)
48
        ;
49
50
        $query = $qb->getQuery();
51
52
        return $query->getResult();
53
    }
54
55
    public function findLearningPathsUsingDocument(int $resourceFileId): array
56
    {
57
        return $this->createQueryBuilder('i')
58
            ->select('lp.iid AS lpId, lp.title AS lpTitle')
59
            ->join('i.lp', 'lp')
60
            ->where('i.itemType = :type')
61
            ->andWhere('i.path = :path')
62
            ->setParameter('type', 'document')
63
            ->setParameter('path', $resourceFileId)
64
            ->getQuery()
65
            ->getArrayResult();
66
    }
67
}
68