Passed
Push — master ( 5330e2...bd7dad )
by Yannick
09:55
created

CDropboxFeedbackRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Repository\ResourceRepository;
10
use Chamilo\CourseBundle\Entity\CDropboxFeedback;
11
use DateTime;
12
use Doctrine\Persistence\ManagerRegistry;
13
14
/** Custom queries for Dropbox feedback */
15
final class CDropboxFeedbackRepository extends ResourceRepository
16
{
17
    public function __construct(ManagerRegistry $registry)
18
    {
19
        parent::__construct($registry, CDropboxFeedback::class);
20
    }
21
22
    public function listByFile(int $cid, int $fileId): array
23
    {
24
        return $this->createQueryBuilder('f')
25
            ->andWhere('f.cId = :cid')->setParameter('cid', $cid)
26
            ->andWhere('f.fileId = :fid')->setParameter('fid', $fileId)
27
            ->orderBy('f.feedbackDate', 'ASC')
28
            ->getQuery()->getResult();
29
    }
30
31
    /**
32
     * Create a feedback row for a given file.
33
     */
34
    public function createForFile(int $cid, int $fileId, int $authorUserId, string $text): CDropboxFeedback
35
    {
36
        $em   = $this->getEntityManager();
37
        $conn = $em->getConnection();
38
39
        $nextId = (int) $conn->fetchOne(
40
            'SELECT COALESCE(MAX(feedback_id), 0) + 1 FROM c_dropbox_feedback WHERE c_id = :cid AND file_id = :fid',
41
            ['cid' => $cid, 'fid' => $fileId]
42
        );
43
44
        $f = (new CDropboxFeedback())
45
            ->setCId($cid)
46
            ->setFileId($fileId)
47
            ->setAuthorUserId($authorUserId)
48
            ->setFeedback($text)
49
            ->setFeedbackDate(new DateTime())
50
            ->setFeedbackId($nextId);
51
52
        $em->persist($f);
53
        $em->flush();
54
55
        return $f;
56
    }
57
58
    /**
59
     * Convenience creator to avoid colliding with ResourceRepository::create(AbstractResource).
60
     */
61
    public function createFeedback(int $cid, int $fileId, int $authorUserId, string $text): CDropboxFeedback
62
    {
63
        $em = $this->getEntityManager();
64
65
        $fb = new CDropboxFeedback();
66
        $fb->setCId($cid);
67
        $fb->setFileId($fileId);
68
        $fb->setAuthorUserId($authorUserId);
69
        $fb->setFeedback($text);
70
        $fb->setFeedbackDate(new \DateTime());
71
        $fb->setFeedbackId(0); // Will be aligned to iid after first flush
72
73
        // 1st flush: get autoincrement iid
74
        $em->persist($fb);
75
        $em->flush();
76
77
        // Align legacy feedback_id = iid without raw SQL or touching protected props
78
        $fb->setFeedbackId((int) $fb->getIid());
79
80
        // 2nd flush: persist alignment
81
        $em->flush();
82
83
        return $fb;
84
    }
85
}
86