Passed
Push — master ( 159572...890e9f )
by Jan
04:56 queued 10s
created

AttachmentRepository::getExternalAttachments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Repository;
23
24
25
use Doctrine\ORM\EntityRepository;
26
27
class AttachmentRepository extends EntityRepository
28
{
29
    /**
30
     * Gets the count of all private/secure attachments.
31
     * @return int
32
     */
33
    public function getPrivateAttachmentsCount(): int
34
    {
35
        $qb = $this->createQueryBuilder('attachment');
36
        $qb->select('COUNT(attachment)')
37
            ->where('attachment.path LIKE :like');
38
        $qb->setParameter('like', '\\%SECURE\\%%');
39
        $query = $qb->getQuery();
40
        return (int) $query->getSingleScalarResult();
41
    }
42
43
    /**
44
     * Gets the count of all external attachments (attachments only containing an URL)
45
     * @return int
46
     * @throws \Doctrine\ORM\NoResultException
47
     * @throws \Doctrine\ORM\NonUniqueResultException
48
     */
49
    public function getExternalAttachments(): int
50
    {
51
        $qb = $this->createQueryBuilder('attachment');
52
        $qb->select('COUNT(attachment)')
53
            ->where('attachment.path LIKE :http')
54
            ->orWhere('attachment.path LIKE :https');
55
        $qb->setParameter('http', 'http://%');
56
        $qb->setParameter('https', 'https://%');
57
        $query = $qb->getQuery();
58
        return (int) $query->getSingleScalarResult();
59
    }
60
61
    /**
62
     * Gets the count of all attachments where an user uploaded an file.
63
     * @return int
64
     * @throws \Doctrine\ORM\NoResultException
65
     * @throws \Doctrine\ORM\NonUniqueResultException
66
     */
67
    public function getUserUploadedAttachments(): int
68
    {
69
        $qb = $this->createQueryBuilder('attachment');
70
        $qb->select('COUNT(attachment)')
71
            ->where('attachment.path LIKE :base')
72
            ->orWhere('attachment.path LIKE :media')
73
            ->orWhere('attachment.path LIKE :secure');
74
        $qb->setParameter('secure', '\\%SECURE\\%%');
75
        $qb->setParameter('base', '\\%BASE\\%%');
76
        $qb->setParameter('media', '\\%MEDIA\\%%');
77
        $query = $qb->getQuery();
78
        return (int) $query->getSingleScalarResult();
79
    }
80
}