SubscriptionEventRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 124
Duplicated Lines 18.55 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 23
loc 124
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A getLastDayEventsCount() 0 13 1
A createUserLastSubscribersEventsQuery() 12 12 1
A getUserLastSubscribersEvents() 0 7 1
A createLastSubscriptionEventsQuery() 11 11 1
A getLastSubscriptionEvents() 0 7 1
A getLastEventsByDay() 0 28 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\QueryBuilder;
7
use Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent;
8
use Skobkin\Bundle\PointToolsBundle\Entity\User;
9
10
class SubscriptionEventRepository extends EntityRepository
11
{
12
    public function add(SubscriptionEvent $entity): void
13
    {
14
        $this->getEntityManager()->persist($entity);
15 4
    }
16
17 4
    /**
18
     * @return int
19 4
     */
20
    public function getLastDayEventsCount(): int
21
    {
22 4
        $qb = $this->createQueryBuilder('se');
23 4
24 4
        $from = (new \DateTime())->sub(new \DateInterval('PT24H'));
25 4
26
        return $qb
27
            ->select('COUNT(se)')
28
            ->where('se.date > :from_time')
29
            ->setParameter('from_time', $from)
30
            ->getQuery()->getSingleScalarResult()
31
        ;
32
    }
33
34
    /**
35
     * Creates QueryBuilder object for pagination of user subscribers events
36 3
     *
37
     * @param User $user
38 3
     *
39
     * @return QueryBuilder
40
     */
41 3 View Code Duplication
    public function createUserLastSubscribersEventsQuery(User $user): QueryBuilder
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42 3
    {
43 3
        $qb = $this->createQueryBuilder('se');
44 3
45 3
        return $qb
46
            ->select(['se', 's'])
47
            ->join('se.subscriber', 's')
48
            ->where('se.author = :author')
49
            ->orderBy('se.date', 'desc')
50
            ->setParameter('author', $user->getId())
51
        ;
52
    }
53
54
    /**
55
     * Get last user subscriber events
56
     *
57
     * @param User $user
58
     * @param int $limit
59
     *
60
     * @return SubscriptionEvent[]
61
     */
62
    public function getUserLastSubscribersEvents(User $user, int $limit = 20): array
63
    {
64
        $qb = $this->createUserLastSubscribersEventsQuery($user);
65
        $qb->setMaxResults($limit);
66
67
        return $qb->getQuery()->getResult();
68
    }
69
70 2
    /**
71
     * Get last global subscriptions QueryBuilder for pagination
72 2
     *
73
     * @return QueryBuilder
74
     */
75 2 View Code Duplication
    public function createLastSubscriptionEventsQuery(): QueryBuilder
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76 2
    {
77 2
        $qb = $this->createQueryBuilder('se');
78 2
79
        return $qb
80
            ->select(['se', 'a', 's'])
81
            ->innerJoin('se.author', 'a')
82
            ->innerJoin('se.subscriber', 's')
83
            ->orderBy('se.date', 'desc')
84
        ;
85
    }
86
87
    /**
88
     * Get last global subscription events
89
     *
90
     * @param int $limit
91
     *
92
     * @return SubscriptionEvent[]
93
     */
94
    public function getLastSubscriptionEvents(int $limit = 20): array
95
    {
96
        $qb = $this->createLastSubscriptionEventsQuery();
97
        $qb->setMaxResults($limit);
98
99
        return $qb->getQuery()->getResult();
100
    }
101
102
    /**
103
     * @return SubscriptionEvent[]
104
     */
105
    public function getLastEventsByDay(int $days = 30): array
106
    {
107
        $qb = $this->createQueryBuilder('se');
108
109
        $rows =  $qb
110
            ->select([
111
                'NEW Skobkin\Bundle\PointToolsBundle\DTO\DailyEvents(DAY(se.date), COUNT(se))',
112
                'DAY(se.date) as day',
113
            ])
114
            ->groupBy('day')
115
            ->orderBy('day', 'DESC')
116
            ->setMaxResults($days)
117
            ->getQuery()->getResult()
118
        ;
119
120
        $result = [];
121
122
        // Removing unnecessary element, saving DTO
123
        // @todo remove crutches, refactor query
124
        foreach ($rows as $row) {
125
            unset($row['day']);
126
            $result[] = reset($row);
127
        }
128
129
        $result = array_reverse($result);
130
131
        return $result;
132
    }
133
}