Passed
Push — master ( 719760...59ab2e )
by Angel Fernando Quiroz
18:35
created

LegalRepository::checkTermCondition()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 28
rs 9.1111
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Language;
10
use Chamilo\CoreBundle\Entity\Legal;
11
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
12
use Doctrine\Common\Collections\Criteria;
13
use Doctrine\Persistence\ManagerRegistry;
14
use Exception;
15
16
class LegalRepository extends ServiceEntityRepository
17
{
18
    public function __construct(ManagerRegistry $registry)
19
    {
20
        parent::__construct($registry, Legal::class);
21
    }
22
23
    /**
24
     * Count the legal terms by language (only count one set of terms for each
25
     * language).
26
     *
27
     * @return int
28
     *
29
     * @throws Exception
30
     */
31
    public function countAllActiveLegalTerms()
32
    {
33
        $qb = $this->createQueryBuilder('l');
34
        $qb->select('l.languageId, COUNT(l.id)')
35
            ->groupBy('l.languageId')
36
        ;
37
38
        return \count($qb->getQuery()->getResult());
39
    }
40
41
    /**
42
     * Get the latest version of terms of the given type and language.
43
     *
44
     * @param int $typeId     The type of terms:
45
     *                        0 for general text,
46
     *                        1 for general HTML link,
47
     *                        101 for private data collection,
48
     *                        etc - see personal_data.php
49
     * @param int $languageId The Id of the language
50
     *
51
     * @return array The terms for those type and language
52
     */
53
    public function findOneByTypeAndLanguage(int $typeId, int $languageId)
54
    {
55
        $qb = $this->createQueryBuilder('l');
56
        $qb->select('l.content')
57
            ->where($qb->expr()->eq('l.type', $typeId))
58
            ->andWhere($qb->expr()->eq('l.languageId', $languageId))
59
        ;
60
61
        return $qb->getQuery()->getResult();
62
    }
63
64
    /**
65
     * Get type of terms and conditions.
66
     * Type 0 is HTML Text
67
     * Type 1 is a link to a different terms and conditions page.
68
     *
69
     * @return mixed The current type of terms and conditions (int) or false on error
70
     */
71
    public function getTypeOfTermsAndConditions(Legal $legal, Language $language)
72
    {
73
        $qb = $this->createQueryBuilder('l');
74
        $qb->select('l.type')
75
            ->where($qb->expr()->eq('l.id', $legal->getId()))
76
            ->andWhere($qb->expr()->eq('l.languageId', $language->getId()))
77
        ;
78
79
        return $qb->getQuery()->getResult();
80
    }
81
82
    /**
83
     * Gets the number of terms and conditions available.
84
     *
85
     * @return int
86
     */
87
    public function countTerms()
88
    {
89
        $qb = $this->createQueryBuilder('l');
90
        $qb->select('COUNT(l.id)');
91
92
        return \count($qb->getQuery()->getResult());
93
    }
94
95
    /**
96
     * Gets the last version of a Term and condition by language.
97
     *
98
     * @return bool | int the version or false if does not exist
99
     */
100
    public function getLastVersion(int $languageId)
101
    {
102
        $qb = $this->createQueryBuilder('l');
103
104
        $result = $qb
105
            ->select('l.version')
106
            ->where(
107
                $qb->expr()->eq('l.languageId', $languageId)
108
            )
109
            ->setMaxResults(1)
110
            ->orderBy('l.version', Criteria::DESC)
111
            ->getQuery()
112
            ->getOneOrNullResult()
113
        ;
114
115
        if (!empty($result['version'])) {
116
            $lastVersion = $result['version'];
117
            if (!is_numeric($lastVersion)) {
118
                $version = explode(':', $lastVersion);
119
                $lastVersion = (int) $version[0];
120
            }
121
122
            return $lastVersion;
123
        }
124
125
        return false;
126
    }
127
}
128