Passed
Push — master ( 2338dd...fbaf94 )
by Julito
09:45
created

CForumThreadRepository::increaseView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ResourceInterface;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Repository\ResourceRepository;
13
use Chamilo\CourseBundle\Entity\CForumThread;
14
use Doctrine\ORM\QueryBuilder;
15
use Doctrine\Persistence\ManagerRegistry;
16
17
class CForumThreadRepository extends ResourceRepository
18
{
19
    public function __construct(ManagerRegistry $registry)
20
    {
21
        parent::__construct($registry, CForumThread::class);
22
    }
23
24
    public function getForumThread(string $title, Course $course, Session $session = null): ?CForumThread
25
    {
26
        $qb = $this->getResourcesByCourse($course, $session);
27
        $qb
28
            ->andWhere('resource.threadTitle = :title')
29
            ->setParameter('title', $title)
30
        ;
31
32
        return $qb->getQuery()->getOneOrNullResult();
33
    }
34
35
    public function findAllByCourse(
36
        Course $course,
37
        Session $session = null,
38
        ?string $title = null
39
    ): QueryBuilder {
40
        $qb = $this->getResourcesByCourse($course, $session);
41
42
        $this->addTitleQueryBuilder($title, $qb);
43
44
        return $qb;
45
    }
46
47
    public function increaseView(CForumThread $thread): void
48
    {
49
        $thread->setThreadViews($thread->getThreadViews() + 1);
50
        $em = $this->getEntityManager();
51
        $em->persist($thread);
52
        $em->flush();
53
    }
54
55
    public function delete(ResourceInterface $resource): void
56
    {
57
        /** @var CForumThread $resource */
58
        $posts = $resource->getPosts();
59
        foreach ($posts as $post) {
60
            parent::delete($post);
61
        }
62
63
        parent::delete($resource);
64
    }
65
}
66