Passed
Push — master ( 28df2a...f19064 )
by Julito
17:10
created

CDocumentRepository::findOneBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
7
use Chamilo\CoreBundle\Repository\ResourceRepository;
8
use Chamilo\CourseBundle\Entity\CDocument;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\EntityRepository;
11
use Gaufrette\Exception\FileNotFound;
12
use Sonata\MediaBundle\Provider\MediaProviderInterface;
13
use Sonata\MediaBundle\Provider\Pool;
14
15
/**
16
 * Class CDocumentRepository.
17
 */
18
class CDocumentRepository extends ResourceRepository
19
{
20
    /**
21
     * @var EntityRepository
22
     */
23
    private $repository;
24
25
    /**
26
     * @var Pool
27
     */
28
    private $mediaPool;
29
30
    /**
31
     * CDocumentRepository constructor.
32
     *
33
     * @param EntityManager $entityManager
34
     * @param Pool          $mediaPool
35
     */
36
    public function __construct(EntityManager $entityManager, Pool $mediaPool)
37
    {
38
        $this->repository = $entityManager->getRepository(CDocument::class);
39
        $this->mediaPool = $mediaPool;
40
        $this->entityManager = $entityManager;
41
    }
42
43
    /**
44
     * @param int $id
45
     *
46
     * @return CDocument|null
47
     */
48
    public function find(int $id): ?CDocument
49
    {
50
        return $this->repository->find($id);
51
    }
52
53
    /**
54
     * @param array      $criteria
55
     * @param array|null $orderBy
56
     *
57
     * @return CDocument|null
58
     */
59
    public function findOneBy(array $criteria, array $orderBy = null): ?CDocument
60
    {
61
        return $this->repository->findOneBy($criteria, $orderBy);
62
    }
63
64
    /**
65
     * @param int $id
66
     *
67
     * @return string
68
     */
69
    public function getDocumentPath($id): string
70
    {
71
        try {
72
            $document = $this->find($id);
73
            $resourceNode = $document->getResourceNode();
74
            $resourceFile = $resourceNode->getResourceFile();
75
            $media = $resourceFile->getMedia();
76
            $provider = $this->mediaPool->getProvider($media->getProviderName());
77
78
            $format = MediaProviderInterface::FORMAT_REFERENCE;
79
            $filename = sprintf(
80
                '%s/%s',
81
                $provider->getFilesystem()->getAdapter()->getDirectory(),
82
                $provider->generatePrivateUrl($media, $format)
83
            );
84
85
            return $filename;
86
        } catch (\Throwable $exception) {
87
            throw new FileNotFound($id);
88
        }
89
    }
90
91
    /**
92
     * @param CDocument $document
93
     *
94
     * @return CDocument|null
95
     */
96
    public function getParent(CDocument $document)
97
    {
98
        $resourceParent = $document->getResourceNode()->getParent();
99
100
        if (!empty($resourceParent)) {
101
            $resourceParentId = $resourceParent->getId();
102
            $criteria = [
103
                'resourceNode' => $resourceParentId,
104
            ];
105
106
            return $this->findOneBy($criteria);
107
        }
108
109
        return null;
110
    }
111
112
    /**
113
     * @param int    $courseId
114
     * @param string $path
115
     *
116
     * @return mixed
117
     * @throws \Doctrine\ORM\NonUniqueResultException
118
     */
119
    public function getFolderSize($courseId, $path)
120
    {
121
        $path = str_replace('_', '\_', $path);
122
        $addedSlash = $path === '/' ? '' : '/';
123
124
        $repo = $this->repository;
125
        $qb = $repo->createQueryBuilder('d');
126
        $query = $qb
127
            ->select('SUM(d.size)')
128
            ->innerJoin('d.resourceNode', 'r')
129
            ->innerJoin('r.resourceLinks', 'l')
130
            ->where('d.path LIKE :path')
131
            ->andWhere('d.path NOT LIKE :deleted')
132
            ->andWhere('d.path NOT LIKE :extra_path ')
133
            ->andWhere('l.visibility <> :visibility')
134
            ->andWhere('d.course = :course')
135
            ->setParameters([
136
                'path' => $path.$addedSlash.'%',
137
                'extra_path' => $path.$addedSlash.'%/%',
138
                'course' => $courseId,
139
                'deleted' => '%_DELETED_%',
140
                'visibility' => ResourceLink::VISIBILITY_DELETED,
141
            ])
142
            ->getQuery();
143
144
        return $query->getSingleScalarResult();
145
    }
146
147
    public function getTotalSpace($courseId, $groupId = null, $sessionId = null)
148
    {
149
        $repo = $this->repository;
150
        $qb = $repo->createQueryBuilder('d');
151
        $query = $qb
152
            ->select('SUM(d.size)')
153
            ->innerJoin('d.resourceNode', 'r')
154
            ->innerJoin('r.resourceLinks', 'l')
155
            ->where('l.course = :course')
156
            ->andWhere('l.group = :group')
157
            ->andWhere('l.session = :session')
158
            ->andWhere('l.visibility <> :visibility')
159
            ->setParameters([
160
                'course' => $courseId,
161
                'group' => $groupId,
162
                'session' => $sessionId,
163
                'visibility' => ResourceLink::VISIBILITY_DELETED,
164
            ])
165
            ->getQuery();
166
167
        return $query->getSingleScalarResult();
168
    }
169
}
170