Test Setup Failed
Push — main ( abdcb9...2e5f68 )
by Slawomir
04:37
created

createPostHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
rs 9.9332
1
<?php
2
3
namespace App\Modules\Security\Persistence\Doctrine\Repository;
4
5
use App\Modules\Security\Domain\Dto\CreateNewUserPostHeaderDto;
6
use App\Modules\Security\Domain\Dto\DeleteExistingUserPostHeaderDto;
7
use App\Modules\Security\Domain\Dto\UpdateExistingUserPostHeaderDto;
8
use App\Modules\Security\Domain\Repository\SecurityPostEventsHandlingRepositoryInterface;
9
use App\Modules\Security\Persistence\Doctrine\Entity\User;
10
use App\Modules\Security\Persistence\Doctrine\Entity\UserPostHeader;
11
12
class DoctrineSecurityPostEventsHandlingRepository extends DoctrineSecurityRepository implements SecurityPostEventsHandlingRepositoryInterface
13
{
14
    /**
15
     * @param CreateNewUserPostHeaderDto $newPostHeader
16
     * @throws \Doctrine\ORM\ORMException
17
     */
18
    function createPostHeader(CreateNewUserPostHeaderDto $newPostHeader): void
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20
        $post = new UserPostHeader();
21
        $post->setId($newPostHeader->getId());
22
        $post->setTitle($newPostHeader->getTitle());
23
        $post->setSummary($newPostHeader->getSummary());
24
        $post->setVersion($newPostHeader->getVersion());
25
        $post->setTags($newPostHeader->getTags());
26
        $post->setCreatedAt($newPostHeader->getCreatedAt());
27
        $post->setCommentsCount($newPostHeader->getCommentsCount());
28
        $post->setUser($this->getEntityManager()->getReference(User::class, $newPostHeader->getCreatedById()));
1 ignored issue
show
Bug introduced by
It seems like $this->getEntityManager(...ader->getCreatedById()) can also be of type null; however, parameter $user of App\Modules\Security\Per...erPostHeader::setUser() does only seem to accept App\Modules\Security\Per...ce\Doctrine\Entity\User, maybe add an additional type check? ( Ignorable by Annotation )

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

28
        $post->setUser(/** @scrutinizer ignore-type */ $this->getEntityManager()->getReference(User::class, $newPostHeader->getCreatedById()));
Loading history...
29
        $this->getEntityManager()->persist($post);
30
    }
31
32
    /**
33
     * @param UpdateExistingUserPostHeaderDto $updatedPostHeader
34
     */
35
    function updatePostHeader(UpdateExistingUserPostHeaderDto $updatedPostHeader): void
36
    {
37
        $this->getEntityManager()
38
            ->createQueryBuilder()
39
            ->update(UserPostHeader::class, 'p')
40
            ->set('p.title', ':title')
41
            ->set('p.summary', ':summary')
42
            ->set('p.tags', ':tags')
43
            ->set('p.version', ':version')
44
            ->where('p.id = :id and p.version <= :version')
45
            ->setParameter("id", $updatedPostHeader->getId(), 'ulid')
46
            ->setParameter("version", $updatedPostHeader->getVersion(),)
47
            ->setParameter("title", $updatedPostHeader->getTitle())
48
            ->setParameter("summary", $updatedPostHeader->getSummary())
49
            ->setParameter("tags", json_encode($updatedPostHeader->getTags()))
50
            ->getQuery()
51
            ->execute();
52
    }
53
54
    /**
55
     * @param DeleteExistingUserPostHeaderDto $deletedPostHeader
56
     */
57
    function deletePostHeader(DeleteExistingUserPostHeaderDto $deletedPostHeader): void
58
    {
59
        $entityManager = $this->getEntityManager();
60
        $entityManager
61
            ->createQueryBuilder()
62
            ->delete(UserPostHeader::class, 'p')
63
            ->where('p.id = :id')
64
            ->setParameter('id', $deletedPostHeader->getId(), 'ulid')
65
            ->getQuery()
66
            ->execute();
67
68
69
    }
70
}