Completed
Pull Request — master (#24)
by Rafał
07:17
created

ApiController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 126
Duplicated Lines 37.3 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 5
c 7
b 1
f 3
lcom 1
cbo 3
dl 47
loc 126
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A myAction() 0 21 1
A discountAction() 16 16 1
A currencyAction() 15 15 1
A listAction() 0 21 1
A gatewaysAction() 16 16 1

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\Controller;
9
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use FOS\RestBundle\Controller\FOSRestController;
13
use FOS\RestBundle\Controller\Annotations\View;
14
use Symfony\Component\HttpFoundation\Request;
15
use Newscoop\PaywallBundle\Criteria\SubscriptionCriteria;
16
17
/**
18
 * API controller.
19
 */
20
class ApiController extends FOSRestController
21
{
22
    /**
23
     * @Route("/api/paywall/pricelist/{currency}/{locale}.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_pricelist")
24
     *
25
     * @Method("GET")
26
     * @View()
27
     */
28
    public function listAction(Request $request, $currency, $locale = null)
29
    {
30
        $criteria = new SubscriptionCriteria();
31
        $criteria->locale = $locale;
32
        $paywallService = $this->get('paywall.subscription.service');
33
        $currencyContext = $this->get('newscoop_paywall.currency_context');
34
        $paginator = $this->get('newscoop.paginator.paginator_service');
35
        $criteria->type = $request->query->get('type');
36
        $list = $paywallService->getSubscriptionsByCriteria($criteria);
37
        $paginator->setUsedRouteParams(array('currency' => $currency));
38
        $currencyContext->setCurrency($currency);
39
40
        $priceList = $paginator->paginate(
41
            $list->items,
42
            array(
43
                'distinct' => false,
44
            )
45
        );
46
47
        return $priceList;
48
    }
49
50
    /**
51
     * @Route("/api/paywall/my-subscriptions/{locale}.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_my")
52
     *
53
     * @Method("GET")
54
     * @View()
55
     */
56
    public function myAction(Request $request, $locale = null)
57
    {
58
        $userService = $this->get('user');
59
        $user = $userService->getCurrentUser();
60
        $criteria = new SubscriptionCriteria();
61
        $criteria->user = $user;
62
        $criteria->locale = $locale;
63
        $paywallService = $this->get('paywall.subscription.service');
64
        $query = $paywallService->getMySubscriptionsByCriteria($criteria, true);
65
        $paginator = $this->get('newscoop.paginator.paginator_service');
66
        $list = $paginator->paginate(
67
            $query,
68
            array(
69
                'distinct' => false,
70
            )
71
        );
72
73
        $list['items'] = $paywallService->filterMySubscriptions($list['items']);
74
75
        return $list;
76
    }
77
78
    /**
79
     * @Route("/api/paywall/discounts.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_discount")
80
     *
81
     * @Method("GET")
82
     * @View()
83
     */
84 View Code Duplication
    public function discountAction(Request $request)
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...
85
    {
86
        $em = $this->get('em');
87
        $query = $em->getRepository('Newscoop\PaywallBundle\Entity\Discount')
88
            ->findActive();
89
90
        $paginator = $this->get('newscoop.paginator.paginator_service');
91
        $discounts = $paginator->paginate(
92
            $query,
93
            array(
94
                'distinct' => false,
95
            )
96
        );
97
98
        return $discounts;
99
    }
100
101
    /**
102
     * @Route("/api/paywall/currencies.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_currency")
103
     *
104
     * @Method("GET")
105
     * @View()
106
     */
107 View Code Duplication
    public function currencyAction(Request $request)
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...
108
    {
109
        $currencyRepository = $this->get('newscoop_paywall.currency.repository');
110
        $query = $currencyRepository->findAllAvailable();
111
112
        $paginator = $this->get('newscoop.paginator.paginator_service');
113
        $currencies = $paginator->paginate(
114
            $query,
115
            array(
116
                'distinct' => false,
117
            )
118
        );
119
120
        return $currencies;
121
    }
122
123
    /**
124
     * @Route("/api/paywall/gateways.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_gateways")
125
     *
126
     * @Method("GET")
127
     * @View()
128
     */
129 View Code Duplication
    public function gatewaysAction(Request $request)
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...
130
    {
131
        $provider = $this->get('newscoop_paywall.method_provider');
132
        $query = $provider->getEnabledMethods();
133
        $paginator = $this->get('newscoop.paginator.paginator_service');
134
        $gateways = $paginator->paginate(
135
            $query,
136
            array(
137
                'distinct' => false,
138
            )
139
        );
140
141
        $gateways['items'][] = $provider->getDefaultMethod();
142
143
        return $gateways;
144
    }
145
}
146