Test Failed
Push — master ( 8ed6d1...926417 )
by Alexey
03:15
created

UserRepository::getTopUsers()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 31
ccs 7
cts 7
cp 1
rs 8.8571
cc 3
eloc 18
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO;
7
use Skobkin\Bundle\PointToolsBundle\Entity\User;
8
9
class UserRepository extends EntityRepository
10
{
11
    /**
12
     * Case-insensitive user search
13
     *
14
     * @param string $login
15
     * @return User|null
16
     * @throws \Doctrine\ORM\NonUniqueResultException
17
     */
18 1
    public function findUserByLogin($login)
19
    {
20 1
        $qb = $this->createQueryBuilder('u');
21
22
        return $qb
23 1
            ->select('u')
24 1
            ->where('LOWER(u.login) = LOWER(:login)')
25 1
            ->setMaxResults(1)
26 1
            ->setParameter('login', $login)
27 1
            ->getQuery()->getOneOrNullResult()
28
        ;
29
    }
30
31
    /**
32
     * Case insensitive user LIKE %login% search
33
     *
34
     * @param string $login
35
     * @param int $limit
36
     *
37
     * @return User[]
38
     */
39
    public function findUsersLikeLogin($login, $limit = 10)
40
    {
41
        if (empty($login)) {
42
            return [];
43
        }
44
45
        $qb = $this->createQueryBuilder('u');
46
47
        return $qb
48
            ->where('LOWER(u.login) LIKE LOWER(:login)')
49
            ->orderBy('u.login', 'ASC')
50
            ->setMaxResults($limit)
51
            ->setParameter('login', '%'.$login.'%')
52
            ->getQuery()
53
            ->getResult()
54
        ;
55
    }
56
57
    /**
58
     * @return integer
59
     */
60 2
    public function getUsersCount()
61
    {
62 2
        $qb = $this->createQueryBuilder('u');
63
64 2
        return $qb->select('COUNT(u)')->getQuery()->getSingleScalarResult();
65
    }
66
67
    /**
68
     * @param integer $id
69
     * @return User[]
70
     */
71 1
    public function findUserSubscribersById($id)
72
    {
73 1
        if (!is_int($id)) {
74
            throw new \InvalidArgumentException('$id must be an integer');
75
        }
76
77 1
        $qb = $this->createQueryBuilder('u');
78
79
        return $qb
80 1
            ->select('u')
81 1
            ->innerJoin('u.subscriptions', 's')
82 1
            ->where('s.author = :author')
83 1
            ->orderBy('u.login', 'asc')
84 1
            ->setParameter('author', $id)
85 1
            ->getQuery()->getResult()
86
        ;
87
    }
88
89
    /**
90
     * Returns top users by subscribers count
91
     *
92 1
     * @param int $limit
93
     *
94 1
     * @return TopUserDTO[]
95
     */
96
    public function getTopUsers($limit = 30)
97
    {
98
        if (!is_int($limit)) {
99 1
            throw new \InvalidArgumentException('$limit must be an integer');
100
        }
101
102 1
        $qb = $this->getEntityManager()->getRepository('SkobkinPointToolsBundle:Subscription')->createQueryBuilder('s');
103 1
104 1
        $rows = $qb
105 1
            ->select([
106 1
                'NEW Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO(a.login, COUNT(s.subscriber))',
107 1
                'COUNT(s.subscriber) as subscribers_count'
108
            ])
109
            ->innerJoin('s.author', 'a')
110
            ->orderBy('subscribers_count', 'desc')
111
            ->groupBy('a.id')
112
            ->setMaxResults($limit)
113
            ->getQuery()->getResult()
114
        ;
115
116
        $result = [];
117
118
        // Removing subscribers_count element, saving TopUserDTO
119
        // @todo remove crutches, refactor query
120
        foreach ($rows as $row) {
121
            unset($row['subscribers_count']);
122
            $result[] = reset($row);
123
        }
124
125
        return $result;
126
    }
127
}