Completed
Pull Request — master (#24)
by Rafał
03:01
created

SubscriptionRepository   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 2.68 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 1 Features 2
Metric Value
wmc 17
c 10
b 1
f 2
lcom 1
cbo 1
dl 4
loc 149
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
C getListByCriteria() 4 51 8
B findActiveBy() 0 27 4
A findActiveOneBy() 0 14 1
A findActive() 0 10 1
A getReference() 0 4 1
A findTemplates() 0 18 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
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @copyright 2015 Sourcefabric z.ú.
6
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
7
 */
8
namespace Newscoop\PaywallBundle\Entity\Repository;
9
10
use Newscoop\ListResult;
11
12
/**
13
 * Subscription repository.
14
 */
15
class SubscriptionRepository extends TranslationRepository
16
{
17
    /**
18
     * Gets all available subscriptions by criteria.
19
     *
20
     * @param SubscriptionCriteria $criteria
21
     * @param bool                 $returnQuery
22
     *
23
     * @return ListResult|Doctrine\ORM\Query
24
     */
25
    public function getListByCriteria($criteria, $returnQuery = false)
26
    {
27
        $qb = $this->createQueryBuilder('s');
28
29
        $qb->select('s', 'r', 'd')
30
            ->andWhere('s.is_active = :is_active')
31
            ->leftJoin('s.ranges', 'r')
32
            ->leftJoin('r.discount', 'd')
33
            ->setParameter('is_active', true);
34
35
        if ($criteria->name) {
36
            $qb->andWhere('s.name = :name')
37
                ->setParameter('name', $criteria->name);
38
        }
39
40
        if ($criteria->currency) {
41
            $qb->andWhere('s.currency = :currency')
42
                ->setParameter('currency', $criteria->currency);
43
        }
44
45
        if ($criteria->type) {
46
            $qb->andWhere('s.type = :type')
47
                ->setParameter('type', $criteria->type);
48
        }
49
50 View Code Duplication
        foreach ($criteria->perametersOperators as $key => $operator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
            $qb->andWhere('s.'.$key.' = :'.$key)
52
                ->setParameter($key, $criteria->$key);
53
        }
54
55
        $metadata = $this->getClassMetadata();
56
        foreach ($criteria->orderBy as $key => $order) {
57
            if (array_key_exists($key, $metadata->columnNames)) {
58
                $key = 's.'.$key;
59
            }
60
61
            $qb->orderBy($key, $order);
62
        }
63
64
        $list = new ListResult();
65
        $countBuilder = clone $qb;
66
        $query = $this->setTranslatableHints($qb->getQuery(), $criteria->locale);
67
        if ($returnQuery) {
68
            return $query;
69
        }
70
71
        $list->count = (int) $countBuilder->select('COUNT(s)')->getQuery()->getSingleScalarResult();
72
        $list->items = $query;
73
74
        return $list;
75
    }
76
77
    /**
78
     * Gets active Subscriptions by the specification and type.
79
     *
80
     * @param string $locale Current language code (e.g. "en")
81
     * @param array  $specs  Subscription specification
82
     *                       (e.g. publication id, section number etc.)
83
     *
84
     * @return array
85
     */
86
    public function findActiveBy($locale = null, $specs = array())
87
    {
88
        $queryBuilder = $this
89
            ->createQueryBuilder('s')
90
            ->select('s', 'r')
91
            ->leftJoin('s.ranges', 'r')
92
            ->join('s.specification', 'sp')
93
            ->where('s.is_active = true');
94
95
        if (!empty($specs)) {
96
            foreach ($specs as $key => $value) {
97
                if (!$value) {
98
                    $queryBuilder
99
                        ->andWhere('sp.'.$key.' IS NULL');
100
                } else {
101
                    $queryBuilder
102
                        ->andWhere('sp.'.$key.' = :'.$key)
103
                        ->setParameter($key, $value);
104
                }
105
            }
106
        }
107
108
        $query = $queryBuilder->getQuery();
109
110
        return $this->setTranslatableHints($query, $locale)
111
            ->getArrayResult();
112
    }
113
114
    public function findActiveOneBy($id, $locale = null)
115
    {
116
        $query = $this
117
            ->createQueryBuilder('s')
118
            ->where('s.id = :id')
119
            ->andWhere('s.is_active = true')
120
            ->setParameter('id', $id)
121
            ->getQuery()
122
        ;
123
124
        return $this->setTranslatableHints($query, $locale)
125
            ->getOneOrNullResult()
126
        ;
127
    }
128
129
    public function findActive($locale = null)
130
    {
131
        $query = $this
132
            ->createQueryBuilder('s')
133
            ->andWhere('s.is_active = true')
134
            ->getQuery()
135
        ;
136
137
        return $this->setTranslatableHints($query, $locale);
138
    }
139
140
    public function getReference($id)
141
    {
142
        return $this->getEntityManager()->getReference($this->getEntityName(), $id);
143
    }
144
145
    public function findTemplates($type = null, $locale = null)
146
    {
147
        $queryBuilder = $this
148
            ->createQueryBuilder('s')
149
            ->where('s.isTemplate = true')
150
            ->andWhere('s.is_active = true')
151
        ;
152
153
        if ($type) {
154
            $queryBuilder
155
                ->andWhere('s.type = :type')
156
                ->setParameter('type', $type);
157
        }
158
159
        $query = $queryBuilder->getQuery();
160
161
        return $this->setTranslatableHints($query, $locale);
162
    }
163
}
164