Test Failed
Push — dev ( d9ea74...46b33e )
by Nico
09:52
created

KnCommentArchivRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Orm\Entity\KnCommentArchiv;
9
use Stu\Orm\Entity\KnCommentArchivInterface;
10
use Stu\Orm\Entity\KnPostArchivInterface;
11
12
/**
13
 * @extends EntityRepository<KnCommentArchiv>
14
 */
15
final class KnCommentArchivRepository extends EntityRepository implements KnCommentArchivRepositoryInterface
16
{
17
    public function getByPost(int $postId): array
18
    {
19
        return $this->findBy(
20
            ['post_id' => $postId],
21
            ['id' => 'desc'],
22
        );
23
    }
24
25
    public function getAmountByPost(KnPostArchivInterface $post): int
26
    {
27
        return $this->count(['post_id' => $post, 'deleted' => null]);
28
    }
29
30
    public function prototype(): KnCommentArchivInterface
31
    {
32
        return new KnCommentArchiv();
33
    }
34
35
    public function save(KnCommentArchivInterface $comment): void
36
    {
37
        $em = $this->getEntityManager();
38
39
        $em->persist($comment);
40
        $em->flush();
41
    }
42
43
    public function delete(KnCommentArchivInterface $comment): void
44
    {
45
        $em = $this->getEntityManager();
46
47
        $em->remove($comment);
48
        $em->flush();
49
    }
50
51
    public function truncateByUser(int $userId): void
52
    {
53
        $this->getEntityManager()
54
            ->createQuery(
55
                sprintf(
56
                    'DELETE FROM %s c WHERE c.user_id = :userId',
57
                    KnCommentArchiv::class
58
                )
59
            )
60
            ->setParameters(['userId' => $userId])
61
            ->execute();
62
    }
63
}
64