Passed
Push — master ( 6a2455...6d3a7a )
by Julito
09:25
created

CForumPostRepository   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
B findAllInCourseByThread() 0 46 11
A delete() 0 9 3
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\Entity\Resource\AbstractResource;
9
use Chamilo\CoreBundle\Repository\ResourceRepository;
10
use Chamilo\CourseBundle\Entity\CForumPost;
11
use Chamilo\CourseBundle\Entity\CForumThread;
12
use Chamilo\CourseBundle\Entity\CGroupInfo;
13
use Chamilo\UserBundle\Entity\User;
14
15
/**
16
 * Class CForumPostRepository.
17
 */
18
class CForumPostRepository extends ResourceRepository
19
{
20
    /**
21
     * @param bool   $onlyVisibles
22
     * @param bool   $isAllowedToEdit
23
     * @param string $orderDirection
24
     */
25
    public function findAllInCourseByThread(
26
        $onlyVisibles,
27
        $isAllowedToEdit,
28
        CForumThread $thread,
29
        Course $course,
30
        User $currentUser = null,
31
        CGroupInfo $group = null,
32
        $orderDirection = 'ASC'
33
    ): array {
34
        $conditionVisibility = $onlyVisibles ? 'p.visible = 1' : 'p.visible != 2';
35
        $conditionModetared = '';
36
        $filterModerated = true;
37
38
        if (
39
            (empty($group) && $isAllowedToEdit) ||
40
            (
41
                ($group ? $group->userIsTutor($currentUser) : false) ||
42
                !$onlyVisibles
43
            )
44
        ) {
45
            $filterModerated = false;
46
        }
47
48
        if ($filterModerated && $onlyVisibles && $thread->getForum()->isModerated()) {
49
            $userId = $currentUser ? $currentUser->getId() : 0;
50
51
            $conditionModetared = 'AND p.status = 1 OR
52
                (p.status = '.CForumPost::STATUS_WAITING_MODERATION." AND p.posterId = $userId) OR
53
                (p.status = ".CForumPost::STATUS_REJECTED." AND p.poster = $userId) OR
54
                (p.status IS NULL AND p.posterId = $userId)";
55
        }
56
57
        $dql = "SELECT p
58
            FROM ChamiloCourseBundle:CForumPost p
59
            WHERE
60
                p.thread = :thread AND
61
                p.cId = :course AND
62
                $conditionVisibility
63
                $conditionModetared
64
            ORDER BY p.iid $orderDirection";
65
66
        return $this
67
            ->getEntityManager()
68
            ->createQuery($dql)
69
            ->setParameters(['thread' => $thread, 'course' => $course])
70
            ->getResult();
71
    }
72
73
    public function delete(AbstractResource $resource)
74
    {
75
        $attachments = $resource->getAttachments();
0 ignored issues
show
Bug introduced by
The method getAttachments() does not exist on Chamilo\CoreBundle\Entit...source\AbstractResource. It seems like you code against a sub-type of Chamilo\CoreBundle\Entit...source\AbstractResource such as Chamilo\CourseBundle\Entity\CForumPost or Chamilo\CourseBundle\Entity\CAnnouncement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        /** @scrutinizer ignore-call */ 
76
        $attachments = $resource->getAttachments();
Loading history...
76
        if (!empty($attachments)) {
77
            foreach ($attachments as $attachment) {
78
                $this->getEntityManager()->remove($attachment);
79
            }
80
        }
81
        parent::delete($resource);
82
    }
83
}
84