ApiController::listAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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