Test Setup Failed
Push — master ( d6bd2d...bc363c )
by Alexey
03:03
created

SubscriptionRepository::add()   A

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