Passed
Push — master ( f14c38...cb3d07 )
by
unknown
11:36 queued 14s
created

LegalRepository::findOneByVersionAndLanguage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 18
rs 9.9
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
     * @return int
30
     *
31
     * @throws Exception
32
     */
33
    public function countAllActiveLegalTerms()
34
    {
35
        $qb = $this->createQueryBuilder('l');
36
        $qb->select('l.languageId, COUNT(l.id)')
37
            ->groupBy('l.languageId')
38
        ;
39
40
        return \count($qb->getQuery()->getResult());
41
    }
42
43
    /**
44
     * Get the latest version of terms of the given type and language.
45
     *
46
     * @param int $typeId     The type of terms:
47
     *                        0 for general text,
48
     *                        1 for general HTML link,
49
     *                        101 for private data collection,
50
     *                        etc - see personal_data.php
51
     * @param int $languageId The Id of the language
52
     *
53
     * @return array The terms for those type and language
54
     */
55
    public function findOneByTypeAndLanguage(int $typeId, int $languageId)
56
    {
57
        $qb = $this->createQueryBuilder('l');
58
        $qb->select('l.content')
59
            ->where($qb->expr()->eq('l.type', $typeId))
60
            ->andWhere($qb->expr()->eq('l.languageId', $languageId))
61
        ;
62
63
        return $qb->getQuery()->getResult();
64
    }
65
66
    /**
67
     * Get type of terms and conditions.
68
     * Type 0 is HTML Text
69
     * Type 1 is a link to a different terms and conditions page.
70
     *
71
     * @return mixed The current type of terms and conditions (int) or false on error
72
     */
73
    public function getTypeOfTermsAndConditions(Legal $legal, Language $language)
74
    {
75
        $qb = $this->createQueryBuilder('l');
76
        $qb->select('l.type')
77
            ->where($qb->expr()->eq('l.id', $legal->getId()))
78
            ->andWhere($qb->expr()->eq('l.languageId', $language->getId()))
79
        ;
80
81
        return $qb->getQuery()->getResult();
82
    }
83
84
    /**
85
     * Gets the number of terms and conditions available.
86
     *
87
     * @return int
88
     */
89
    public function countTerms()
90
    {
91
        $qb = $this->createQueryBuilder('l');
92
        $qb->select('COUNT(l.id)');
93
94
        return \count($qb->getQuery()->getResult());
95
    }
96
97
    /**
98
     * Gets the last version of a Term and condition by language.
99
     *
100
     * @return bool | int the version or false if does not exist
101
     */
102
    public function getLastVersion(int $languageId)
103
    {
104
        $qb = $this->createQueryBuilder('l');
105
106
        $result = $qb
107
            ->select('l.version')
108
            ->where(
109
                $qb->expr()->eq('l.languageId', $languageId)
110
            )
111
            ->setMaxResults(1)
112
            ->orderBy('l.version', Criteria::DESC)
113
            ->getQuery()
114
            ->getOneOrNullResult()
115
        ;
116
117
        if (!empty($result['version'])) {
118
            $lastVersion = $result['version'];
119
            if (!is_numeric($lastVersion)) {
120
                $version = explode(':', $lastVersion);
121
                $lastVersion = (int) $version[0];
122
            }
123
124
            return $lastVersion;
125
        }
126
127
        return false;
128
    }
129
130
    public function getLastConditionByLanguage(int $languageId): ?Legal
131
    {
132
        try {
133
            $qb = $this->createQueryBuilder('l');
134
            $qb->where('l.languageId = :languageId')
135
                ->setParameter('languageId', $languageId)
136
                ->orderBy('l.version', 'DESC')
137
                ->setMaxResults(1)
138
            ;
139
140
            $result = $qb->getQuery()->getSingleResult();
141
142
            if ($result->getContent()) {
143
                $result->setContent($this->replaceTags($result->getContent()));
144
            }
145
146
            return $result;
147
        } catch (NoResultException|NonUniqueResultException $e) {
148
            return null;
149
        }
150
    }
151
152
    public function findLastConditionByLanguage(int $languageId): ?Legal
153
    {
154
        return $this->createQueryBuilder('l')
155
            ->andWhere('l.languageId = :languageId')
156
            ->setParameter('languageId', $languageId)
157
            ->orderBy('l.version', 'DESC')
158
            ->setMaxResults(1)
159
            ->getQuery()
160
            ->getOneOrNullResult()
161
        ;
162
    }
163
164
    /**
165
     * Replace tags in content.
166
     *
167
     * @param string $content the content with tags
168
     *
169
     * @return string the content with tags replaced
170
     */
171
    private function replaceTags(string $content): string
172
    {
173
        // Replace tags logic goes here
174
        // For example: return str_replace('[SITE_NAME]', 'YourSiteName', $content);
175
        return $content;
176
    }
177
178
    /**
179
     * Get a term and condition based on version and language.
180
     */
181
    public function findOneByVersionAndLanguage(int $versionId, int $languageId): ?Legal
182
    {
183
        $qb = $this->createQueryBuilder('l');
184
        $qb->where('l.languageId = :languageId')
185
            ->andWhere('l.version = :versionId')
186
            ->setParameters([
187
                'languageId' => $languageId,
188
                'versionId' => $versionId,
189
            ])
190
            ->setMaxResults(1);
191
192
        $result = $qb->getQuery()->getOneOrNullResult();
193
194
        if ($result && $result->getContent()) {
195
            $result->setContent($this->replaceTags($result->getContent()));
196
        }
197
198
        return $result;
199
    }
200
}
201