Passed
Push — master ( e48f96...18d135 )
by Angel Fernando Quiroz
19:42 queued 01:03
created

CDocumentRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A countUserDocuments() 0 10 1
A getParent() 0 13 2
A getFolderSize() 0 3 1
A findDocumentsByAuthor() 0 16 1
A findByTitleAndParentResourceNode() 0 11 1
A addFileTypeQueryBuilder() 0 9 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\ResourceLink;
11
use Chamilo\CoreBundle\Entity\ResourceNode;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Entity\User;
14
use Chamilo\CoreBundle\Repository\ResourceRepository;
15
use Chamilo\CourseBundle\Entity\CDocument;
16
use Chamilo\CourseBundle\Entity\CGroup;
17
use Doctrine\ORM\QueryBuilder;
18
use Doctrine\Persistence\ManagerRegistry;
19
20
final class CDocumentRepository extends ResourceRepository
21
{
22
    public function __construct(ManagerRegistry $registry)
23
    {
24
        parent::__construct($registry, CDocument::class);
25
    }
26
27
    public function getParent(CDocument $document): ?CDocument
28
    {
29
        $resourceParent = $document->getResourceNode()->getParent();
30
31
        if (null !== $resourceParent) {
32
            $criteria = [
33
                'resourceNode' => $resourceParent->getId(),
34
            ];
35
36
            return $this->findOneBy($criteria);
37
        }
38
39
        return null;
40
    }
41
42
    public function getFolderSize(ResourceNode $resourceNode, Course $course, Session $session = null): int
43
    {
44
        return $this->getResourceNodeRepository()->getSize($resourceNode, $this->getResourceType(), $course, $session);
45
    }
46
47
    /**
48
     * @return CDocument[]
49
     */
50
    public function findDocumentsByAuthor(int $userId)
51
    {
52
        $qb = $this->createQueryBuilder('d');
53
        $query = $qb
54
            ->innerJoin('d.resourceNode', 'node')
55
            ->innerJoin('node.resourceLinks', 'l')
56
            ->where('l.user = :user')
57
            ->andWhere('l.visibility <> :visibility')
58
            ->setParameters([
59
                'user' => $userId,
60
                'visibility' => ResourceLink::VISIBILITY_DELETED,
61
            ])
62
            ->getQuery()
63
        ;
64
65
        return $query->getResult();
66
    }
67
68
    public function countUserDocuments(User $user, Course $course, Session $session = null, CGroup $group = null): int
69
    {
70
        $qb = $this->getResourcesByCourseLinkedToUser($user, $course, $session, $group);
71
72
        // Add "not deleted" filters.
73
        $qb->select('count(resource)');
74
75
        $this->addFileTypeQueryBuilder('file', $qb);
76
77
        return $this->getCount($qb);
78
    }
79
80
    public function findByTitleAndParentResourceNode(string $title, int $parentResourceNodeId): ?CDocument
81
    {
82
        return $this->createQueryBuilder('d')
83
            ->innerJoin('d.resourceNode', 'node')
84
            ->andWhere('d.title = :title')
85
            ->andWhere('node.parent = :parentResourceNodeId')
86
            ->setParameter('title', $title)
87
            ->setParameter('parentResourceNodeId', $parentResourceNodeId)
88
            ->setMaxResults(1)
89
            ->getQuery()
90
            ->getOneOrNullResult();
91
    }
92
93
    protected function addFileTypeQueryBuilder(string $fileType, QueryBuilder $qb = null): QueryBuilder
94
    {
95
        $qb = $this->getOrCreateQueryBuilder($qb);
96
        $qb
97
            ->andWhere('resource.filetype = :filetype')
98
            ->setParameter('filetype', $fileType)
99
        ;
100
101
        return $qb;
102
    }
103
}
104