HumanRepository::findOnlineByRoom()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 3
1
<?php
2
3
namespace Rottenwood\KingdomBundle\Entity\Infrastructure;
4
5
use Rottenwood\KingdomBundle\Entity\Human;
6
use Rottenwood\KingdomBundle\Entity\Room;
7
8
/** {@inheritDoc} */
9
class HumanRepository extends AbstractRepository
10
{
11
12
    /**
13
     * @param Room  $room
14
     * @param array $onlinePlayerIds
15
     * @param array $excludePlayerIds
16
     * @return Human[]
17
     */
18
    public function findOnlineByRoom($room, array $onlinePlayerIds, array $excludePlayerIds = [])
19
    {
20
        $builder = $this->createQueryBuilder('u');
21
        $builder->where('u.room = :room');
22
23
        if ($onlinePlayerIds) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $onlinePlayerIds of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
24
            $builder->andWhere($builder->expr()->in('u.id', $onlinePlayerIds));
25
        }
26
27
        if (!empty($excludePlayerIds)) {
28
            $builder->andWhere($builder->expr()->notIn('u.id', $excludePlayerIds));
29
        }
30
31
        $builder->setParameters(['room' => $room]);
32
33
        return $builder->getQuery()->getResult();
34
    }
35
36
    /**
37
     * @return Human[]
38
     */
39
    public function findAllHumans()
40
    {
41
        return $this->findAll();
42
    }
43
44
    /**
45
     * @param string $username
46
     * @return Human|null
47
     */
48
    public function findByUsername($username)
49
    {
50
        return $this->findOneBy(['username' => $username]);
51
    }
52
53
    /**
54
     * @param string|int $userNameOrId
55
     * @return Human|null
56
     */
57
    public function findByNameOrId($userNameOrId)
58
    {
59
        return $this->findByName($userNameOrId) ?: $this->findById($userNameOrId);
60
    }
61
62
    /**
63
     * @param string $name
64
     * @return Human|null
65
     */
66
    public function findByName($name)
67
    {
68
        return $this->findOneBy(['name' => $name]);
69
    }
70
71
    /**
72
     * @param int $userId
73
     * @return Human|null
74
     */
75
    public function findById($userId)
76
    {
77
        return $this->find($userId);
78
    }
79
80
    /**
81
     * @param string $email
82
     * @return Human|null
83
     */
84
    public function findByEmail($email)
85
    {
86
        return $this->findOneBy(['email' => $email]);
87
    }
88
}
89