PersistPlayerRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Game\Infrastructure\Repository\Doctrine;
6
7
use App\Game\Features\Player\Level\PlayerLevelRepositoryInterface;
8
use App\Game\Features\Player\Player\Player;
9
use App\Game\Features\Player\Player\PlayerId;
10
use App\Game\Features\Player\Player\PlayerNotFoundException;
11
use App\Game\Features\Registration\PlayerRepositoryInterface;
12
use App\Game\Features\Registration\Player\PlayerDto;
13
use App\Game\Infrastructure\Encoder\PasswordEncoderInterface;
14
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
0 ignored issues
show
Bug introduced by
The type Doctrine\Bundle\Doctrine...ServiceEntityRepository 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...
15
use Doctrine\Persistence\ManagerRegistry;
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ManagerRegistry 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...
16
17
final class PersistPlayerRepository extends ServiceEntityRepository implements
18
    PlayerRepositoryInterface,
19
    PlayerLevelRepositoryInterface
20
{
21
    private PasswordEncoderInterface $encoder;
22
23
    public function __construct(ManagerRegistry $registry, PasswordEncoderInterface $encoder)
24
    {
25
        parent::__construct($registry, Player::class);
26
        $this->encoder = $encoder;
27
    }
28
29
    public function createPlayer(PlayerDto $playerDto): void
30
    {
31
        $player = new Player(new PlayerId($playerDto->playerId()->id()));
32
        $player->changeNickname($playerDto->nickname());
33
        $player->changeRole($playerDto->role()->getValue());
34
        $player->changePassword($this->encoder->encodePassword($playerDto->password(), null));
35
36
        $this->store($player);
37
    }
38
39
    public function changeLevel(PlayerId $playerId): void
40
    {
41
        $player = $this->createQueryBuilder('u')
42
            ->andWhere('u.playerId = :id')
43
            ->setParameter('id', (string) $playerId)
44
            ->getQuery()
45
            ->getOneOrNullResult();
46
47
        if (!$player instanceof Player) {
48
            throw new PlayerNotFoundException();
49
        }
50
51
        $player->changeLevel($player->level() + 1);
52
53
        $this->store($player);
54
    }
55
56
    private function store(Player $player): void
57
    {
58
        $entityManager = $this->getEntityManager();
59
        $entityManager->persist($player);
60
        $entityManager->flush($player);
61
    }
62
}
63