Passed
Push — master ( 2338dd...fbaf94 )
by Julito
09:45
created

CLpRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findForumByCourse() 0 16 5
A getLink() 0 12 2
A createLp() 0 14 2
A addNotDeletedQueryBuilder() 0 7 1
A __construct() 0 3 1
A findAllByCourse() 0 19 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\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ResourceInterface;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Repository\ResourceRepository;
13
use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface;
14
use Chamilo\CourseBundle\Entity\CForum;
15
use Chamilo\CourseBundle\Entity\CLp;
16
use Chamilo\CourseBundle\Entity\CLpItem;
17
use Doctrine\ORM\QueryBuilder;
18
use Doctrine\Persistence\ManagerRegistry;
19
use Exception;
20
use Symfony\Component\Routing\RouterInterface;
21
22
final class CLpRepository extends ResourceRepository implements ResourceWithLinkInterface
23
{
24
    public function __construct(ManagerRegistry $registry)
25
    {
26
        parent::__construct($registry, CLp::class);
27
    }
28
29
    public function createLp(CLp $lp): void
30
    {
31
        if (null !== $lp->getResourceNode()) {
32
            throw new Exception('Lp should not have a resource node during creation');
33
        }
34
35
        $lpItem = (new CLpItem())
36
            ->setTitle('root')
37
            ->setPath('root')
38
            ->setLp($lp)
39
            ->setItemType('root')
40
        ;
41
        $lp->getItems()->add($lpItem);
42
        $this->create($lp);
43
    }
44
45
    public function findForumByCourse(CLp $lp, Course $course, Session $session = null): ?CForum
46
    {
47
        $forums = $lp->getForums();
48
        $result = null;
49
        foreach ($forums as $forum) {
50
            $links = $forum->getResourceNode()->getResourceLinks();
51
            foreach ($links as $link) {
52
                if ($link->getCourse() === $course && $link->getSession() === $session) {
53
                    $result = $forum;
54
55
                    break 2;
56
                }
57
            }
58
        }
59
60
        return $result;
61
    }
62
63
    public function findAllByCourse(
64
        Course $course,
65
        Session $session = null,
66
        ?string $title = null,
67
        ?int $active = null,
68
        bool $onlyPublished = true,
69
        ?int $categoryId = null
70
    ): QueryBuilder {
71
        $qb = $this->getResourcesByCourse($course, $session);
72
73
        /*if ($onlyPublished) {
74
            $this->addDateFilterQueryBuilder(new DateTime(), $qb);
75
        }*/
76
        //$this->addCategoryQueryBuilder($categoryId, $qb);
77
        //$this->addActiveQueryBuilder($active, $qb);
78
        //$this->addNotDeletedQueryBuilder($qb);
79
        $this->addTitleQueryBuilder($title, $qb);
80
81
        return $qb;
82
    }
83
84
    public function getLink(ResourceInterface $resource, RouterInterface $router, array $extraParams = []): string
85
    {
86
        $params = [
87
            'lp_id' => $resource->getResourceIdentifier(),
88
            'name' => 'lp/lp_controller.php',
89
            'action' => 'view',
90
        ];
91
        if (!empty($extraParams)) {
92
            $params = array_merge($params, $extraParams);
93
        }
94
95
        return $router->generate('legacy_main', $params);
96
    }
97
98
    private function addNotDeletedQueryBuilder(QueryBuilder $qb = null): QueryBuilder
0 ignored issues
show
Unused Code introduced by
The method addNotDeletedQueryBuilder() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
99
    {
100
        $qb = $this->getOrCreateQueryBuilder($qb);
101
102
        $qb->andWhere('resource.active <> -1');
103
104
        return $qb;
105
    }
106
}
107