Test Failed
Pull Request — dev (#1952)
by Janko
02:59
created

TradepostDeletionHandler::delete()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 27.7252

Importance

Changes 0
Metric Value
cc 5
eloc 30
nc 7
nop 1
dl 0
loc 47
ccs 1
cts 32
cp 0.0313
crap 27.7252
rs 9.1288
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Player\Deletion\Handler;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Stu\Module\History\Lib\EntryCreatorInterface;
9
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
10
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Stu\Orm\Entity\User;
12
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
13
use Stu\Orm\Repository\StorageRepositoryInterface;
14
use Stu\Orm\Repository\TradePostRepositoryInterface;
15
use Stu\Orm\Repository\UserRepositoryInterface;
16
17
final class TradepostDeletionHandler implements PlayerDeletionHandlerInterface
18
{
19
    public function __construct(
20
        private TradePostRepositoryInterface $tradePostRepository,
21 1
        private SpacecraftRepositoryInterface $spacecraftRepository,
22
        private UserRepositoryInterface $userRepository,
23
        private StorageRepositoryInterface $storageRepository,
24
        private EntryCreatorInterface $entryCreator,
25
        private PrivateMessageSenderInterface $privateMessageSender
26
    ) {}
27
28 1
    #[Override]
29
    public function delete(User $user): void
30
    {
31
        $fallbackUser = $this->userRepository->getFallbackUser();
32
33
        foreach ($this->tradePostRepository->getByUser($user->getId()) as $tradepost) {
34
            $station = $tradepost->getStation();
35
36
            // send PMs to storage owners except tradepost owner
37
            foreach ($this->tradePostRepository->getUsersWithStorageOnTradepost($tradepost->getId()) as $user) {
38
                if ($user->getId() !== $tradepost->getUserId()) {
39
                    $this->privateMessageSender->send(
40
                        UserConstants::USER_NOONE,
41
                        $user->getId(),
42
                        sprintf(
43
                            'Der Handelsposten "%s" bei den Koordinaten %s wurde verlassen. Du solltest deine Waren hier schleunigst abholen, sonst gehen sie verloren.',
44
                            $tradepost->getName(),
45
                            $station->getSectorString()
46
                        )
47
                    );
48
                }
49
            }
50
51
            //create history entry
52
            $this->entryCreator->addEntry(
53
                'Der Handelsposten in Sektor ' . $station->getSectorString() . ' wurde verlassen.',
54
                UserConstants::USER_NOONE,
55
                $station
56
            );
57
58
            //transfer tradepost to noone user
59
            $tradepost->setUser($fallbackUser);
60
            $tradepost->setName('Verlassener Handelsposten');
61
            $tradepost->setDescription('Verlassener Handelsposten');
62
            $tradepost->setTradeNetwork(UserConstants::USER_NOONE);
63
            $this->tradePostRepository->save($tradepost);
64
65
            $station->setUser($fallbackUser);
66
            $station->setName('Verlassener Handelsposten');
67
            $station->getCondition()->setDisabled(true);
68
            $this->spacecraftRepository->save($station);
69
70
            //change torpedo owner
71
            if ($station->getTorpedoStorage() !== null) {
72
                $storage = $station->getTorpedoStorage()->getStorage();
73
                $storage->setUser($fallbackUser);
74
                $this->storageRepository->save($storage);
75
            }
76
        }
77
    }
78
}
79