SubscriptionRepository::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
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): void
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(int $id): int
22
    {
23 4
        $qb = $this->createQueryBuilder('s');
24 4
        return $qb
25 4
            ->select('COUNT(s.subscriber)')
26 4
            ->innerJoin('s.author', 'sa')
27 4
            ->where('sa.id = :id')
28
            ->setParameter('id', $id)
29
            ->getQuery()->getSingleScalarResult()
30
        ;
31
    }
32
33
    /**
34
     * @param User $user
35
     * @param User[] $subscribers
36
     */
37
    public function removeSubscribers(User $user, array $subscribers): void
38
    {
39
        $qb = $this->createQueryBuilder('s');
40
        $qb
41
            ->delete()
42
            ->where('s.author = :author')
43
            ->andWhere('s.subscriber IN (:subscribers)')
44
            ->setParameter('author', $user->getId())
45
            ->setParameter('subscribers', $subscribers)
46
            ->getQuery()->execute();
47
        ;
48
    }
49
}