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

InMemorySecurityRepository::findPostsByUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 28
rs 9.552
1
<?php
2
3
namespace App\Tests\Modules\Security\Unit\Repository;
4
5
use App\Infrastructure\Pagination\Page;
6
use App\Modules\Security\Domain\Dto\ChangeExistingUserPasswordDto;
7
use App\Modules\Security\Domain\Dto\CreateNewUserDto;
8
use App\Modules\Security\Domain\Dto\CreateNewUserPostHeaderDto;
9
use App\Modules\Security\Domain\Dto\DeleteExistingUserPostHeaderDto;
10
use App\Modules\Security\Domain\Dto\RenameExistingUserDto;
11
use App\Modules\Security\Domain\Dto\UpdateExistingUserPostHeaderDto;
12
use App\Modules\Security\Domain\Dto\UpdatePostsCommentsCountDto;
13
use App\Modules\Security\Domain\Dto\UserPostHeaderDto;
14
use App\Modules\Security\Domain\Repository\SecurityCommentsEventHandlingRepositoryInterface;
15
use App\Modules\Security\Domain\Repository\SecurityPostEventsHandlingRepositoryInterface;
16
use App\Modules\Security\Domain\Repository\UserCreationRepositoryInterface;
17
use App\Modules\Security\Domain\Repository\UserFindingRepositoryInterface;
18
use App\Modules\Security\Domain\Repository\UserPostHeadersFindingRepositoryInterface;
19
use App\Modules\Security\Domain\Repository\UserUpdatingRepositoryInterface;
20
use DusanKasan\Knapsack\Collection;
21
use Symfony\Component\Uid\Ulid;
22
23
class InMemorySecurityRepository implements
24
    SecurityPostEventsHandlingRepositoryInterface,
25
    UserPostHeadersFindingRepositoryInterface,
26
    UserCreationRepositoryInterface,
27
    SecurityCommentsEventHandlingRepositoryInterface,
28
    UserFindingRepositoryInterface,
29
    UserUpdatingRepositoryInterface
30
{
31
32
    private static Collection $postHeaders;
33
    private static Collection $users;
34
35
    public function __construct()
36
    {
37
        self::clear();
38
    }
39
40
    public static function clear(): void
41
    {
42
        self::$postHeaders = Collection::from([]);
43
        self::$users = Collection::from([
44
            new InMemoryUser(new Ulid(InMemoryUser::ID), "[email protected]", ["ROLE_USER"], 1)
45
        ]);
46
    }
47
48
    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...
49
    {
50
        self::$postHeaders = self::$postHeaders->append(new InMemoryUserPostHeader(
51
            $newPostHeader->getId(),
52
            $newPostHeader->getTitle(),
53
            $newPostHeader->getSummary(),
54
            $newPostHeader->getTags(),
55
            self::$users->get(0),
1 ignored issue
show
Bug introduced by
It seems like self::users->get(0) can also be of type DusanKasan\Knapsack\Collection; however, parameter $user of App\Tests\Modules\Securi...stHeader::__construct() does only seem to accept App\Tests\Modules\Securi...Repository\InMemoryUser, 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

55
            /** @scrutinizer ignore-type */ self::$users->get(0),
Loading history...
56
            $newPostHeader->getCreatedAt(),
57
            $newPostHeader->getVersion(),
58
            $newPostHeader->getCommentsCount()
59
        ));
60
    }
61
62
    function updatePostHeader(UpdateExistingUserPostHeaderDto $updatedPostHeader): 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...
63
    {
64
        foreach (self::$postHeaders
65
                     ->filter(function ($header) use ($updatedPostHeader) {
66
                         return $header->getId() == $updatedPostHeader->getId() && $header->getVersion() <= $updatedPostHeader->getVersion();
67
                     })
68
                     ->toArray() as $header) {
69
            $header->setTitle($updatedPostHeader->getTitle());
70
            $header->setSummary($updatedPostHeader->getSummary());
71
            $header->setTags($updatedPostHeader->getTags());
72
            $header->setVersion($updatedPostHeader->getVersion());
73
        }
74
    }
75
76
    function deletePostHeader(DeleteExistingUserPostHeaderDto $deletedPostHeader): 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...
77
    {
78
        self::$postHeaders = self::$postHeaders->filter(
79
            function ($header) use ($deletedPostHeader) {
80
                return $header->getId() != $deletedPostHeader->getId();
81
            }
82
        )->realize();
83
    }
84
85
    function findPostHeaders(): array
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...
86
    {
87
        return self::$postHeaders
88
            ->map(function ($header) {
89
                return new UserPostHeaderDto(
90
                    $header->getId(),
91
                    $header->getTitle(),
92
                    $header->getSummary(),
93
                    $header->getTags(),
94
                    $header->getUser()->getId(),
95
                    $header->getUser()->getEmail(),
96
                    $header->getCreatedAt(),
97
                    $header->getVersion(),
98
                    $header->getCommentsCount()
99
                );
100
            })
101
            ->toArray();
102
    }
103
104
    function createUser(CreateNewUserDto $newUser): Ulid
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...
105
    {
106
        $id = new Ulid();
107
        self::$users = self::$users->append(new InMemoryUser($id, $newUser->getLogin(), $newUser->getRoles(), 1));
108
        return $id;
109
    }
110
111
    public function findPostsByUserId(Ulid $userId, int $pageNo): Page
112
    {
113
        $from = ($pageNo - 1) * self::PAGE_SIZE;
114
        $to = $from + self::PAGE_SIZE;
115
        $size = self::$postHeaders
116
            ->filter(function ($header) use ($userId) {
117
                return $header->getUser()->getId() == $userId;
118
            })->size();
119
        $data = self::$postHeaders
120
            ->filter(function ($header) use ($userId) {
121
                return $header->getUser()->getId() == $userId;
122
            })
123
            ->slice($from, $to)
124
            ->map(function ($header) {
125
                return new UserPostHeaderDto(
126
                    $header->getId(),
127
                    $header->getTitle(),
128
                    $header->getSummary(),
129
                    $header->getTags(),
130
                    $header->getUser()->getId(),
131
                    $header->getUser()->getEmail(),
132
                    $header->getCreatedAt(),
133
                    $header->getVersion(),
134
                    $header->getCommentsCount()
135
                );
136
            })
137
            ->toArray();
138
        return new Page($data, $size, $pageNo, self::PAGE_SIZE);
139
    }
140
141
    public function updatePostCommentsCount(UpdatePostsCommentsCountDto $commentsCount): void
142
    {
143
        self::$postHeaders
144
            ->filter(function ($header) use ($commentsCount) {
145
                return $header->getId() == $commentsCount->getPostId();
146
            })
147
            ->each(function ($header) use ($commentsCount) {
148
                $header->setCommentsCount($commentsCount->getCommentsCount());
149
            })
150
            ->realize();
151
    }
152
153
    public function exists(string $login): bool
154
    {
155
        return self::$users
156
            ->filter(function ($user) use ($login) {
157
                return $user->getEmail() == $login;
158
            })
159
            ->sizeIsGreaterThan(0);
160
    }
161
162
    public function renameUser(RenameExistingUserDto $renamedUser): void
163
    {
164
        self::$users
165
            ->filter(function ($user) use ($renamedUser) {
166
                return $user->getEmail() == $renamedUser->getCurrentLogin();
167
            })
168
            ->each(function ($user) use ($renamedUser) {
169
                $user->setEmail($renamedUser->getNewLogin());
170
            });
171
    }
172
173
    public function changePassword(ChangeExistingUserPasswordDto $changedPassword): void
174
    {
175
        //noop;
176
    }
177
}