Passed
Push — dev ( f10869...6b4369 )
by Janko
12:04
created

ShipBuildplanDeletionHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 25
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A delete() 0 18 4
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 RuntimeException;
9
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
10
use Stu\Orm\Entity\ShipInterface;
11
use Stu\Orm\Entity\UserInterface;
12
use Stu\Orm\Repository\ShipBuildplanRepositoryInterface;
13
use Stu\Orm\Repository\UserRepositoryInterface;
14
15
/**
16
 * Deletes ship buildplans on user deletion
17
 */
18
final class ShipBuildplanDeletionHandler implements PlayerDeletionHandlerInterface
19
{
20 2
    public function __construct(
21
        private ShipBuildplanRepositoryInterface $shipBuildplanRepository,
22
        private UserRepositoryInterface $userRepository
23 2
    ) {}
24
25 1
    #[Override]
26
    public function delete(UserInterface $user): void
27
    {
28 1
        foreach ($this->shipBuildplanRepository->getByUser($user->getId()) as $shipBuildplan) {
29
30 1
            $existsForeignShip = !$shipBuildplan->getShiplist()
31 1
                ->filter(fn(ShipInterface $ship): bool => $ship->getUser() !== $shipBuildplan->getUser())
32 1
                ->isEmpty();
33
34 1
            if ($existsForeignShip) {
35 1
                $user = $this->userRepository->find(UserEnum::USER_FOREIGN_BUILDPLANS);
36 1
                if ($user === null) {
37
                    throw new RuntimeException(sprintf('user with id %d does not exist', UserEnum::USER_FOREIGN_BUILDPLANS));
38
                }
39 1
                $shipBuildplan->setUser($user);
40 1
                $this->shipBuildplanRepository->save($shipBuildplan);
41
            } else {
42 1
                $this->shipBuildplanRepository->delete($shipBuildplan);
43
            }
44
        }
45
    }
46
}
47