Passed
Pull Request — master (#7182)
by
unknown
10:12
created

LegalRepository::findTermsByLanguageAndVersion()   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
nc 1
nop 2
dl 0
loc 10
rs 10
c 1
b 0
f 0
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\ORM\NonUniqueResultException;
14
use Doctrine\ORM\NoResultException;
15
use Doctrine\Persistence\ManagerRegistry;
16
use Exception;
17
18
class LegalRepository extends ServiceEntityRepository
19
{
20
    public function __construct(ManagerRegistry $registry)
21
    {
22
        parent::__construct($registry, Legal::class);
23
    }
24
25
    /**
26
     * Count the legal terms by language (only count one set of terms for each
27
     * language).
28
     *
29
     * @throws Exception
30
     */
31
    public function countAllActiveLegalTerms(): int
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
    public function getLastVersionByLanguage(int $languageId): ?int
129
    {
130
        try {
131
            $result = $this->createQueryBuilder('l')
132
                ->select('MAX(l.version)')
133
                ->andWhere('l.languageId = :languageId')
134
                ->setParameter('languageId', $languageId)
135
                ->getQuery()
136
                ->getSingleScalarResult();
137
138
            return null !== $result ? (int) $result : null;
139
        } catch (NoResultException|NonUniqueResultException) {
140
            return null;
141
        }
142
    }
143
144
    /**
145
     * @return Legal[]
146
     */
147
    public function findTermsByLanguageAndVersion(int $languageId, int $version): array
148
    {
149
        return $this->createQueryBuilder('l')
150
            ->andWhere('l.languageId = :languageId')
151
            ->andWhere('l.version = :version')
152
            ->setParameter('languageId', $languageId)
153
            ->setParameter('version', $version)
154
            ->orderBy('l.type', 'ASC')
155
            ->getQuery()
156
            ->getResult();
157
    }
158
159
    /**
160
     * IMPORTANT: keep this returning the main section (type=0),
161
     * otherwise it might return one of the GDPR sections.
162
     */
163
    public function getLastConditionByLanguage(int $languageId): ?Legal
164
    {
165
        return $this->createQueryBuilder('l')
166
            ->andWhere('l.languageId = :languageId')
167
            ->andWhere('l.type = 0')
168
            ->setParameter('languageId', $languageId)
169
            ->orderBy('l.version', 'DESC')
170
            ->addOrderBy('l.date', 'DESC')
171
            ->setMaxResults(1)
172
            ->getQuery()
173
            ->getOneOrNullResult();
174
    }
175
176
    public function findLastConditionByLanguage(int $languageId): ?Legal
177
    {
178
        return $this->createQueryBuilder('l')
179
            ->andWhere('l.languageId = :languageId')
180
            ->andWhere('l.type = 0')
181
            ->setParameter('languageId', $languageId)
182
            ->orderBy('l.version', 'DESC')
183
            ->setMaxResults(1)
184
            ->getQuery()
185
            ->getOneOrNullResult();
186
    }
187
188
    /**
189
     * Replace tags in content.
190
     *
191
     * @param string $content the content with tags
192
     *
193
     * @return string the content with tags replaced
194
     */
195
    private function replaceTags(string $content): string
0 ignored issues
show
Unused Code introduced by
The method replaceTags() 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...
196
    {
197
        // Replace tags logic goes here
198
        // For example: return str_replace('[SITE_NAME]', 'YourSiteName', $content);
199
        return $content;
200
    }
201
202
    /**
203
     * Get a term and condition based on version and language.
204
     */
205
    public function findOneByVersionAndLanguage(int $versionId, int $languageId): ?Legal
206
    {
207
        $qb = $this->createQueryBuilder('l');
208
        $qb->where('l.languageId = :languageId')
209
            ->andWhere('l.version = :versionId')
210
            ->andWhere('l.type = 0')
211
            ->setParameters([
212
                'languageId' => $languageId,
213
                'versionId' => $versionId,
214
            ])
215
            ->setMaxResults(1);
216
217
        return $qb->getQuery()->getOneOrNullResult();
218
    }
219
220
    public function findLatestVersionByLanguage(int $languageId): int
221
    {
222
        $qb = $this->createQueryBuilder('l');
223
        $qb->select('MAX(l.version) AS maxVersion')
224
            ->andWhere('l.languageId = :languageId')
225
            ->setParameter('languageId', $languageId);
226
227
        $row = $qb->getQuery()->getOneOrNullResult();
228
229
        return (int) ($row['maxVersion'] ?? 0);
230
    }
231
232
    /**
233
     * @return Legal[]
234
     */
235
    public function findByLanguageAndVersion(int $languageId, int $version): array
236
    {
237
        return $this->createQueryBuilder('l')
238
            ->andWhere('l.languageId = :languageId')
239
            ->andWhere('l.version = :version')
240
            ->setParameters([
241
                'languageId' => $languageId,
242
                'version' => $version,
243
            ])
244
            ->orderBy('l.type', 'ASC')
245
            ->getQuery()
246
            ->getResult();
247
    }
248
}
249