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

ApiController::gatewaysAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
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