Passed
Push — master ( cf75f9...66ca98 )
by Julito
09:35
created

CForumForumRepository::__construct()   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 1
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\Course;
7
use Chamilo\CoreBundle\Entity\Session;
8
use Chamilo\CourseBundle\Entity\CForumCategory;
9
use Chamilo\CourseBundle\Entity\CForumForum;
10
use Chamilo\CourseBundle\Entity\CItemProperty;
11
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
12
use Doctrine\Common\Persistence\ManagerRegistry;
13
14
/**
15
 * Class CForumForumRepository.
16
 *
17
 * @package Chamilo\CourseBundle\Repository
18
 */
19
class CForumForumRepository extends ServiceEntityRepository
20
{
21
    /**
22
     * CForumForumRepository constructor.
23
     *
24
     * @param ManagerRegistry $registry
25
     */
26
    public function __construct(ManagerRegistry $registry)
27
    {
28
        parent::__construct($registry, CForumForum::class);
29
    }
30
31
    /**
32
     * @param bool           $isAllowedToEdit
33
     * @param CForumCategory $category
34
     * @param Course         $course
35
     * @param Session|null   $session
36
     * @param bool           $includeGroupsForums
37
     *
38
     * @return array
39
     *
40
     * @todo Remove api_get_session_condition
41
     */
42
    public function findAllInCourseByCategory(
43
        $isAllowedToEdit,
44
        CForumCategory $category,
45
        Course $course,
46
        Session $session = null,
47
        $includeGroupsForums = true
48
    ): array {
49
        $conditionSession = api_get_session_condition(
50
            $session ? $session->getId() : 0,
51
            true,
52
            true,
53
            'f.sessionId'
54
        );
55
        $conditionVisibility = $isAllowedToEdit ? 'ip.visibility != 2' : 'ip.visibility = 1';
56
        $conditionGroups = $includeGroupsForums
57
            ? 'AND (f.forumOfGroup = 0 OR f.forumOfGroup IS NULL)'
58
            : '';
59
60
        $dql = "SELECT f, ip
61
            FROM ChamiloCourseBundle:CForumForum AS f
62
            INNER JOIN ChamiloCourseBundle:CItemProperty AS ip
63
                WITH (f.iid = ip.ref AND f.cId = ip.course)
64
            WHERE
65
                f.forumCategory = :category AND
66
                ip.tool = :tool AND
67
                f.cId = :course
68
                $conditionSession AND
69
                $conditionVisibility
70
                $conditionGroups
71
            ORDER BY f.forumOrder ASC";
72
73
        $result = $this
74
            ->_em
75
            ->createQuery($dql)
76
            ->setParameters(['category' => $category, 'course' => $course, 'tool' => TOOL_FORUM])
77
            ->getResult();
78
79
        $forums = [];
80
81
        for ($i = 0; $i < count($result); $i += 2) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
82
            /** @var CForumForum $f */
83
            $f = $result[$i];
84
            /** @var CItemProperty $ip */
85
            $ip = $result[$i + 1];
86
            $f->setItemProperty($ip);
87
88
            $forums[] = $f;
89
        }
90
91
        return $forums;
92
    }
93
94
    /**
95
     * @param int    $id
96
     * @param Course $course
97
     *
98
     * @return CForumForum
99
     */
100
    public function findOneInCourse($id, Course $course)
101
    {
102
        $dql = "SELECT f, ip
103
            FROM ChamiloCourseBundle:CForumForum AS f
104
            INNER JOIN ChamiloCourseBundle:CItemProperty AS ip
105
                WITH (f.iid = ip.ref AND f.cId = ip.course)
106
            WHERE
107
                f.iid = :id AND
108
                ip.tool = :tool AND
109
                f.cId = :course AND
110
                ip.visibility != 2";
111
112
        $result = $this
113
            ->_em
114
            ->createQuery($dql)
115
            ->setParameters(['id' => (int) $id, 'course' => $course, 'tool' => TOOL_FORUM])
116
            ->getResult();
117
118
        if (empty($result)) {
119
            return null;
120
        }
121
122
        /** @var CForumForum $f */
123
        $f = $result[0];
124
        /** @var CItemProperty $ip */
125
        $ip = $result[1];
126
        $f->setItemProperty($ip);
127
128
        return $f;
129
    }
130
}
131