Completed
Push — master ( 082352...f656e7 )
by Petr
03:04
created

HumanRepository::findByNameOrId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Rottenwood\KingdomBundle\Entity\Infrastructure;
4
5
use Rottenwood\KingdomBundle\Entity\Human;
6
use Rottenwood\KingdomBundle\Entity\Room;
7
8
//TODO[Rottenwood]: Rename to HumanRepository
9
class HumanRepository extends AbstractRepository {
10
11
    /**
12
     * @param int $userId
13
     * @return Human|null
14
     */
15
    public function findById($userId) {
16
        return $this->find($userId);
17
    }
18
19
    /**
20
     * @param Room  $room
21
     * @param array $onlinePlayerIds
22
     * @param array $excludePlayerIds
23
     * @return Human[]
24
     */
25
    public function findOnlineByRoom($room, array $onlinePlayerIds, array $excludePlayerIds = []) {
26
        $builder = $this->createQueryBuilder('u');
27
        $builder->where('u.room = :room');
28
        $builder->andWhere($builder->expr()->in('u.id', $onlinePlayerIds));
29
30
        if (!empty($excludePlayerIds)) {
31
            $builder->andWhere($builder->expr()->notIn('u.id', $excludePlayerIds));
32
        }
33
34
        $builder->setParameters(['room' => $room]);
35
36
        return $builder->getQuery()->getResult();
37
    }
38
39
    /**
40
     * @return Human[]
41
     */
42
    public function findAllHumans() {
43
        return $this->findAll();
44
    }
45
46
    /**
47
     * @param string $username
48
     * @return Human|null
49
     */
50
    public function findByUsername($username) {
51
        return $this->findOneBy(['username' => $username]);
52
    }
53
54
    /**
55
     * @param string $name
56
     * @return Human|null
57
     */
58
    public function findByName($name) {
59
        return $this->findOneBy(['name' => $name]);
60
    }
61
62
    /**
63
     * @param string|int $userNameOrId
64
     * @return Human|null
65
     */
66
    public function findByNameOrId($userNameOrId) {
67
        return $this->findByName($userNameOrId) ?: $this->findById($userNameOrId);
68
    }
69
70
    /**
71
     * @param string $email
72
     * @return Human|null
73
     */
74
    public function findByEmail($email) {
75
        return $this->findOneBy(['email' => $email]);
76
    }
77
}
78