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

CLpItemRepository::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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