Skip to content

Commit bb15667

Browse files
committed
Extract notifications from new kn posting action
Also add tests and remove more stan errors from the baseline
1 parent 2f89227 commit bb15667

File tree

9 files changed

+192
-95
lines changed

9 files changed

+192
-95
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5045,11 +5045,6 @@ parameters:
50455045
count: 1
50465046
path: src/Module/Communication/Action/AddKnPost/AddKnPost.php
50475047

5048-
-
5049-
message: "#^Variable \\$plot might not be defined\\.$#"
5050-
count: 1
5051-
path: src/Module/Communication/Action/AddKnPost/AddKnPost.php
5052-
50535048
-
50545049
message: "#^Cannot call method getId\\(\\) on Stu\\\\Orm\\\\Entity\\\\UserInterface\\|null\\.$#"
50555050
count: 1
@@ -5365,16 +5360,6 @@ parameters:
53655360
count: 1
53665361
path: src/Module/Control/GameController.php
53675362

5368-
-
5369-
message: "#^Method Stu\\\\Module\\\\Control\\\\GameController\\:\\:executeCallback\\(\\) has parameter \\$actions with no value type specified in iterable type array\\.$#"
5370-
count: 1
5371-
path: src/Module/Control/GameController.php
5372-
5373-
-
5374-
message: "#^Method Stu\\\\Module\\\\Control\\\\GameController\\:\\:executeView\\(\\) has parameter \\$views with no value type specified in iterable type array\\.$#"
5375-
count: 1
5376-
path: src/Module/Control/GameController.php
5377-
53785363
-
53795364
message: "#^Method Stu\\\\Module\\\\Control\\\\GameController\\:\\:getAchievements\\(\\) return type has no value type specified in iterable type array\\.$#"
53805365
count: 1
@@ -5415,16 +5400,6 @@ parameters:
54155400
count: 1
54165401
path: src/Module/Control/GameController.php
54175402

5418-
-
5419-
message: "#^Method Stu\\\\Module\\\\Control\\\\GameController\\:\\:main\\(\\) has parameter \\$actions with no value type specified in iterable type array\\.$#"
5420-
count: 1
5421-
path: src/Module/Control/GameController.php
5422-
5423-
-
5424-
message: "#^Method Stu\\\\Module\\\\Control\\\\GameController\\:\\:main\\(\\) has parameter \\$views with no value type specified in iterable type array\\.$#"
5425-
count: 1
5426-
path: src/Module/Control/GameController.php
5427-
54285403
-
54295404
message: "#^Method Stu\\\\Module\\\\Control\\\\GameController\\:\\:sendInformation\\(\\) has parameter \\$category_id with no type specified\\.$#"
54305405
count: 1
@@ -5555,16 +5530,6 @@ parameters:
55555530
count: 1
55565531
path: src/Module/Control/GameControllerInterface.php
55575532

5558-
-
5559-
message: "#^Method Stu\\\\Module\\\\Control\\\\GameControllerInterface\\:\\:main\\(\\) has parameter \\$actions with no value type specified in iterable type array\\.$#"
5560-
count: 1
5561-
path: src/Module/Control/GameControllerInterface.php
5562-
5563-
-
5564-
message: "#^Method Stu\\\\Module\\\\Control\\\\GameControllerInterface\\:\\:main\\(\\) has parameter \\$views with no value type specified in iterable type array\\.$#"
5565-
count: 1
5566-
path: src/Module/Control/GameControllerInterface.php
5567-
55685533
-
55695534
message: "#^Method Stu\\\\Module\\\\Control\\\\GameControllerInterface\\:\\:sendInformation\\(\\) has no return type specified\\.$#"
55705535
count: 1
@@ -11040,26 +11005,11 @@ parameters:
1104011005
count: 1
1104111006
path: src/Module/Ship/View/ShowShip/ShowShip.php
1104211007

11043-
-
11044-
message: "#^Strict comparison using \\=\\=\\= between Stu\\\\Orm\\\\Entity\\\\ShipInterface and null will always evaluate to false\\.$#"
11045-
count: 1
11046-
path: src/Module/Ship/View/ShowShip/ShowShip.php
11047-
11048-
-
11049-
message: "#^Ternary operator condition is always true\\.$#"
11050-
count: 1
11051-
path: src/Module/Ship/View/ShowShip/ShowShip.php
11052-
1105311008
-
1105411009
message: "#^Variable \\$astroEntry might not be defined\\.$#"
1105511010
count: 1
1105611011
path: src/Module/Ship/View/ShowShip/ShowShip.php
1105711012

11058-
-
11059-
message: "#^Variable \\$firstOrbitShip might not be defined\\.$#"
11060-
count: 2
11061-
path: src/Module/Ship/View/ShowShip/ShowShip.php
11062-
1106311013
-
1106411014
message: "#^Variable \\$startTime might not be defined\\.$#"
1106511015
count: 5

src/Module/Communication/Action/AddKnPost/AddKnPost.php

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44

55
namespace Stu\Module\Communication\Action\AddKnPost;
66

7-
use Doctrine\ORM\EntityManagerInterface;
8-
use Stu\Component\Game\GameEnum;
7+
use Stu\Module\Communication\Lib\NewKnPostNotificatorInterface;
98
use Stu\Module\Control\ActionControllerInterface;
109
use Stu\Module\Control\GameController;
1110
use Stu\Module\Control\GameControllerInterface;
12-
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
13-
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
14-
use Stu\Orm\Entity\KnPostInterface;
1511
use Stu\Orm\Entity\RpgPlotInterface;
1612
use Stu\Orm\Repository\KnPostRepositoryInterface;
1713
use Stu\Orm\Repository\RpgPlotMemberRepositoryInterface;
@@ -32,32 +28,29 @@ final class AddKnPost implements ActionControllerInterface
3228

3329
private UserRepositoryInterface $userRepository;
3430

35-
private PrivateMessageSenderInterface $privateMessageSender;
36-
37-
private EntityManagerInterface $entityManager;
31+
private NewKnPostNotificatorInterface $newKnPostNotificator;
3832

3933
public function __construct(
4034
AddKnPostRequestInterface $addKnPostRequest,
4135
KnPostRepositoryInterface $knPostRepository,
4236
RpgPlotMemberRepositoryInterface $rpgPlotMemberRepository,
4337
RpgPlotRepositoryInterface $rpgPlotRepository,
4438
UserRepositoryInterface $userRepository,
45-
PrivateMessageSenderInterface $privateMessageSender,
46-
EntityManagerInterface $entityManager
39+
NewKnPostNotificatorInterface $newKnPostNotificator
4740
) {
4841
$this->addKnPostRequest = $addKnPostRequest;
4942
$this->knPostRepository = $knPostRepository;
5043
$this->rpgPlotMemberRepository = $rpgPlotMemberRepository;
5144
$this->rpgPlotRepository = $rpgPlotRepository;
5245
$this->userRepository = $userRepository;
53-
$this->privateMessageSender = $privateMessageSender;
54-
$this->entityManager = $entityManager;
46+
$this->newKnPostNotificator = $newKnPostNotificator;
5547
}
5648

5749
public function handle(GameControllerInterface $game): void
5850
{
5951
$user = $game->getUser();
6052
$userId = $user->getId();
53+
$plot = null;
6154

6255
$title = $this->addKnPostRequest->getTitle();
6356
$text = $this->addKnPostRequest->getText();
@@ -95,10 +88,9 @@ public function handle(GameControllerInterface $game): void
9588
$post->setDate(time());
9689

9790
$this->knPostRepository->save($post);
98-
$this->entityManager->flush();
9991

10092
if ($plot !== null) {
101-
$this->notifyPlotMembers($post, $plot);
93+
$this->newKnPostNotificator->notify($post, $plot);
10294
}
10395

10496
$game->addInformation(_('Der Beitrag wurde hinzugefügt'));
@@ -112,31 +104,6 @@ public function handle(GameControllerInterface $game): void
112104
$game->setView(GameController::DEFAULT_VIEW);
113105
}
114106

115-
private function notifyPlotMembers(KnPostInterface $post, RpgPlotInterface $plot): void
116-
{
117-
foreach ($plot->getMembers() as $member) {
118-
if ($member->getUser() !== $post->getUser()) {
119-
$user = $member->getUser();
120-
121-
$text = sprintf(
122-
_('Der Spieler %s hat einen neuen Beitrag zum Plot "%s" hinzugefügt.'),
123-
$post->getUser()->getName(),
124-
$plot->getTitle()
125-
);
126-
127-
$href = sprintf(_('comm.php?SHOW_SINGLE_KN=1&id=%d'), $post->getId());
128-
129-
$this->privateMessageSender->send(
130-
GameEnum::USER_NOONE,
131-
$user->getId(),
132-
$text,
133-
PrivateMessageFolderSpecialEnum::PM_SPECIAL_SYSTEM,
134-
$href
135-
);
136-
}
137-
}
138-
}
139-
140107
public function performSessionCheck(): bool
141108
{
142109
return true;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stu\Module\Communication\Lib;
6+
7+
use Stu\Component\Game\GameEnum;
8+
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
9+
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
10+
use Stu\Orm\Entity\KnPostInterface;
11+
use Stu\Orm\Entity\RpgPlotInterface;
12+
use Stu\Orm\Entity\RpgPlotMemberInterface;
13+
14+
/**
15+
* Notifies users about new kn postings
16+
*/
17+
final class NewKnPostNotificator implements NewKnPostNotificatorInterface
18+
{
19+
private PrivateMessageSenderInterface $privateMessageSender;
20+
21+
public function __construct(
22+
PrivateMessageSenderInterface $privateMessageSender
23+
){
24+
$this->privateMessageSender = $privateMessageSender;
25+
}
26+
27+
public function notify(KnPostInterface $post, RpgPlotInterface $plot): void
28+
{
29+
$postUser = $post->getUser();
30+
$url = $post->getUrl();
31+
$postUserId = $postUser->getId();
32+
33+
$text = sprintf(
34+
'Der Spieler %s hat einen neuen Beitrag zum Plot "%s" hinzugefügt.',
35+
$postUser->getUserName(),
36+
$plot->getTitle()
37+
);
38+
39+
// filter the postUser from the member list
40+
$plot->getMembers()
41+
->filter(
42+
fn (RpgPlotMemberInterface $member): bool => $member->getUserId() !== $postUserId
43+
)
44+
->map(function (RpgPlotMemberInterface $member) use ($text, $url): void {
45+
$this->privateMessageSender->send(
46+
GameEnum::USER_NOONE,
47+
$member->getUserId(),
48+
$text,
49+
PrivateMessageFolderSpecialEnum::PM_SPECIAL_SYSTEM,
50+
$url
51+
);
52+
});
53+
}
54+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stu\Module\Communication\Lib;
6+
7+
8+
use Stu\Orm\Entity\KnPostInterface;
9+
use Stu\Orm\Entity\RpgPlotInterface;
10+
11+
interface NewKnPostNotificatorInterface
12+
{
13+
public function notify(KnPostInterface $post, RpgPlotInterface $plot): void;
14+
}

src/Module/Communication/services.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
namespace Stu\Module\Communication;
66

7-
use Stu\Module\Communication\Action\RateKnPost\RateKnPost;
8-
use Stu\Module\Communication\Action\RateKnPost\RateKnPostRequest;
9-
use Stu\Module\Communication\Action\RateKnPost\RateKnPostRequestInterface;
10-
use Stu\Module\Communication\View\ShowKnRating\ShowKnRating;
11-
use Stu\Module\Control\GameController;
127
use Stu\Module\Communication\Action\AddKnPlotMember\AddKnPlotMember;
138
use Stu\Module\Communication\Action\AddKnPlotMember\AddKnPlotMemberRequest;
149
use Stu\Module\Communication\Action\AddKnPlotMember\AddKnPlotMemberRequestInterface;
@@ -42,9 +37,13 @@
4237
use Stu\Module\Communication\Action\PostKnComment\PostKnComment;
4338
use Stu\Module\Communication\Action\PostKnComment\PostKnCommentRequest;
4439
use Stu\Module\Communication\Action\PostKnComment\PostKnCommentRequestInterface;
40+
use Stu\Module\Communication\Action\RateKnPost\RateKnPost;
41+
use Stu\Module\Communication\Action\RateKnPost\RateKnPostRequest;
42+
use Stu\Module\Communication\Action\RateKnPost\RateKnPostRequestInterface;
4543
use Stu\Module\Communication\Action\SetKnMark\SetKnMark;
4644
use Stu\Module\Communication\Action\SetKnMark\SetKnMarkRequest;
4745
use Stu\Module\Communication\Action\SetKnMark\SetKnMarkRequestInterface;
46+
use Stu\Module\Communication\Lib\NewKnPostNotificator;
4847
use Stu\Module\Communication\View\Overview\Overview;
4948
use Stu\Module\Communication\View\Overview\OverviewRequest;
5049
use Stu\Module\Communication\View\Overview\OverviewRequestInterface;
@@ -61,6 +60,7 @@
6160
use Stu\Module\Communication\View\ShowKnPlot\ShowKnPlot;
6261
use Stu\Module\Communication\View\ShowKnPlot\ShowKnPlotRequest;
6362
use Stu\Module\Communication\View\ShowKnPlot\ShowKnPlotRequestInterface;
63+
use Stu\Module\Communication\View\ShowKnRating\ShowKnRating;
6464
use Stu\Module\Communication\View\ShowPlotList\ShowPlotList;
6565
use Stu\Module\Communication\View\ShowSearchResult\ShowPostIdSearchResult;
6666
use Stu\Module\Communication\View\ShowSearchResult\ShowPostSearchResult;
@@ -72,6 +72,7 @@
7272
use Stu\Module\Communication\View\ShowSingleKn\ShowSingleKnRequestInterface;
7373
use Stu\Module\Communication\View\ShowUserPlotList\ShowUserPlotList;
7474
use Stu\Module\Communication\View\ShowWriteKn\ShowWriteKn;
75+
use Stu\Module\Control\GameController;
7576
use function DI\autowire;
7677

7778
return [
@@ -96,7 +97,11 @@
9697
ShowSearchResultRequestInterface::class => autowire(ShowSearchResultRequest::class),
9798
'COMMUNICATION_ACTIONS' => [
9899
SetKnMark::ACTION_IDENTIFIER => autowire(SetKnMark::class),
99-
AddKnPost::ACTION_IDENTIFIER => autowire(AddKnPost::class),
100+
AddKnPost::ACTION_IDENTIFIER => autowire(AddKnPost::class)
101+
->constructorParameter(
102+
'newKnPostNotificator',
103+
autowire(NewKnPostNotificator::class)
104+
),
100105
PostKnComment::ACTION_IDENTIFIER => autowire(PostKnComment::class),
101106
DeleteKnComment::ACTION_IDENTIFIER => autowire(DeleteKnComment::class),
102107
EditKnPost::ACTION_IDENTIFIER => autowire(EditKnPost::class),

src/Orm/Entity/KnPost.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,9 @@ public function setRatings(array $ratings): KnPostInterface
236236
$this->ratings = $ratings;
237237
return $this;
238238
}
239+
240+
public function getUrl(): string
241+
{
242+
return sprintf('/comm.php?SHOW_SINGLE_KN=1&id=%d', $this->getId());
243+
}
239244
}

src/Orm/Entity/KnPostInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public function getRatings(): array;
5454
* @param array<mixed> $ratings
5555
*/
5656
public function setRatings(array $ratings): KnPostInterface;
57+
58+
/**
59+
* Returns the relativ url to this posting
60+
*/
61+
public function getUrl(): string;
5762
}

src/Orm/Repository/KnPostRepository.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function save(KnPostInterface $post): void
2424
$em = $this->getEntityManager();
2525

2626
$em->persist($post);
27+
$em->flush();
2728
}
2829

2930
public function delete(KnPostInterface $post): void

0 commit comments

Comments
 (0)