Passed
Push — master ( ff47af...4fe9ef )
by Julito
08:21
created

CForumPostRepository::findAllInCourseByThread()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 23
nc 12
nop 7
dl 0
loc 46
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CourseBundle\Repository;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Repository\ResourceRepository;
9
use Chamilo\CourseBundle\Entity\CForumPost;
10
use Chamilo\CourseBundle\Entity\CForumThread;
11
use Chamilo\CourseBundle\Entity\CGroupInfo;
12
use Chamilo\UserBundle\Entity\User;
13
14
/**
15
 * Class CForumPostRepository.
16
 */
17
class CForumPostRepository extends ResourceRepository
18
{
19
    /**
20
     * @param bool   $onlyVisibles
21
     * @param bool   $isAllowedToEdit
22
     * @param string $orderDirection
23
     */
24
    public function findAllInCourseByThread(
25
        $onlyVisibles,
26
        $isAllowedToEdit,
27
        CForumThread $thread,
28
        Course $course,
29
        User $currentUser = null,
30
        CGroupInfo $group = null,
31
        $orderDirection = 'ASC'
32
    ): array {
33
        $conditionVisibility = $onlyVisibles ? 'p.visible = 1' : 'p.visible != 2';
34
        $conditionModetared = '';
35
        $filterModerated = true;
36
37
        if (
38
            (empty($group) && $isAllowedToEdit) ||
39
            (
40
                ($group ? $group->userIsTutor($currentUser) : false) ||
41
                !$onlyVisibles
42
            )
43
        ) {
44
            $filterModerated = false;
45
        }
46
47
        if ($filterModerated && $thread->getForum()->isModerated() && $onlyVisibles) {
48
            $userId = $currentUser ? $currentUser->getId() : 0;
49
50
            $conditionModetared = 'AND p.status = 1 OR
51
                (p.status = '.CForumPost::STATUS_WAITING_MODERATION." AND p.posterId = $userId) OR
52
                (p.status = ".CForumPost::STATUS_REJECTED." AND p.poster = $userId) OR
53
                (p.status IS NULL AND p.posterId = $userId)";
54
        }
55
56
        $dql = "SELECT p
57
            FROM ChamiloCourseBundle:CForumPost p
58
            WHERE
59
                p.thread = :thread AND
60
                p.cId = :course AND
61
                $conditionVisibility
62
                $conditionModetared
63
            ORDER BY p.iid $orderDirection";
64
65
        return $this
66
            ->_em
67
            ->createQuery($dql)
68
            ->setParameters(['thread' => $thread, 'course' => $course])
69
            ->getResult();
70
    }
71
}
72