Passed
Push — master ( 3db25e...88be9e )
by Alexey
03:31
created

SubscriptionRepository::removeSubscribers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Skobkin\Bundle\PointToolsBundle\Entity\User;
7
8
class SubscriptionRepository extends EntityRepository
9
{
10
    /**
11
     * @param int $id
12
     *
13
     * @return int
14
     */
15 4
    public function getUserSubscribersCountById($id): int
16
    {
17 4
        if (!is_int($id)) {
18
            throw new \InvalidArgumentException('$id must be an integer');
19
        }
20
21 4
        $qb = $this->createQueryBuilder('s');
22
        return $qb
23 4
            ->select('COUNT(s.subscriber)')
24 4
            ->innerJoin('s.author', 'sa')
25 4
            ->where('sa.id = :id')
26 4
            ->setParameter('id', $id)
27 4
            ->getQuery()->getSingleScalarResult()
28
        ;
29
    }
30
31
    /**
32
     * @param User $user
33
     * @param User[] $subscribers
34
     */
35
    public function removeSubscribers(User $user, array $subscribers)
36
    {
37
        $qb = $this->createQueryBuilder('s');
38
        $qb
39
            ->delete()
40
            ->where('s.author = :author')
41
            ->andWhere('s.subscriber IN (:subscribers)')
42
            ->setParameter('author', $user->getId())
43
            ->setParameter('subscribers', $subscribers)
44
            ->getQuery()->execute();
45
        ;
46
    }
47
}