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

ShipBuildplanDeletionHandler::delete()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.8333
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 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