Passed
Push — master ( 7dd8b7...258428 )
by Julito
07:42
created

CLpItemRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 4 1
A getTree() 0 12 1
A getRootItem() 0 5 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
    /**
42
     * @return CLpItem[]
43
     */
44
    public function getTree(int $lpId)
45
    {
46
        $qb = $this->createQueryBuilder('i');
47
        $qb
48
            ->andWhere('lp = :lp AND path = :path')
49
            ->setParameters([
50
                'lp' => $lpId,
51
                'path' => 'root',
52
            ])
53
        ;
54
55
        return $qb->getQuery()->getResult('tree');
56
    }
57
}
58