PurchaseController::purchaseAction()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 5
nop 1
dl 0
loc 27
rs 8.439
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 Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
18
/**
19
 * It handles purchase actions.
20
 */
21
class PurchaseController extends BaseController
22
{
23
    /**
24
     * @Route("/paywall/purchase/", name="paywall_plugin_purchase_purchase", options={"expose"=true})
25
     *
26
     * @Method("POST")
27
     */
28
    public function purchaseAction(Request $request)
29
    {
30
        $translator = $this->get('translator');
31
        $currencyProvider = $this->get('newscoop_paywall.currency_provider');
32
        $currencyContext = $this->get('newscoop_paywall.currency_context');
33
        $currencyContext->setCurrency($currencyProvider->getDefaultCurrency()->getCode());
34
        $items = $request->getSession()->get('paywall_purchase', array());
35
36
        if (empty($items)) {
37
            return $this->loadErrorTemplate($translator->trans('paywall.error.noitems'));
38
        }
39
40
        $method = $request->request->get('paymentMethod');
41
        $paymentMethodContext = $this->get('newscoop_paywall.payment_method_context');
42
        $paymentMethodContext->setMethod($method);
43
        $purchaseService = $this->get('newscoop_paywall.services.purchase');
44
        $response = $purchaseService->startPurchase($items);
45
        if ($response && $response->isRedirect()) {
46
            $response->redirect();
47
        }
48
49
        if ($response && !$response->isSuccessful()) {
50
            return $this->loadErrorTemplate($response->getMessage());
51
        }
52
53
        return $this->redirectToThankYou();
54
    }
55
56
    /**
57
     * @Route("/paywall/subscriptions/order-batch/{currency}", name="paywall_subscribe_order_batch", options={"expose"=true})
58
     *
59
     * @Method("POST")
60
     */
61
    public function batchOrderAction(Request $request, $currency)
62
    {
63
        $items = $request->request->get('batchorder', array());
64
        $response = new JsonResponse();
65
        if (empty($items)) {
66
            $response->setStatusCode(404);
67
68
            return $response;
69
        }
70
71
        $method = $request->request->get('paymentMethod');
72
        if (null !== $method) {
73
            $paymentMethodContext = $this->get('newscoop_paywall.payment_method_context');
74
            $paymentMethodContext->setMethod($method);
75
        }
76
77
        $request->getSession()->set('paywall_purchase', $items);
78
79
        $purchaseService = $this->get('newscoop_paywall.services.purchase');
80
        $result = $purchaseService->startPurchase($items, $currency);
81
82
        if (null === $result) {
83
            $response->setStatusCode(204);
84
85
            return $response;
86
        }
87
88
        $data = $result->getData();
89
        if (isset($data['ACK']) && 'Success' === $data['ACK']) {
90
            $response->headers->set('X-Location', $result->getRedirectUrl());
91
            $response->setStatusCode(302);
92
        } else {
93
            $response->setStatusCode(502);
94
        }
95
96
        return $response;
97
    }
98
99
    /**
100
     * @Route("/paywall/purchase/methods/", name="paywall_plugin_purchase_methods", options={"expose"=true})
101
     */
102
    public function methodsAction(Request $request)
103
    {
104
        $templatesService = $this->get('newscoop.templates.service');
105
        $translator = $this->get('translator');
106
107
        $items = $request->query->get('batchorder', array());
108
        if (empty($items)) {
109
            return $this->loadErrorTemplate($translator->trans('paywall.error.noitems'));
110
        }
111
112
        $request->getSession()->set('paywall_purchase', $items);
113
114
        $order = $this->get('newscoop_paywall.services.order')->processAndCalculateOrderItems($items);
115
116
        return new Response($templatesService->fetchTemplate(
117
            '_paywall/payment_methods.tpl',
118
            array(
119
                'amount' => $order->getTotal(),
120
                'currency' => $order->getCurrency(),
121
            )
122
        ), 200, array('Content-Type' => 'text/html'));
123
    }
124
125
    /**
126
     * @Route("/paywall/success/", name="paywall_plugin_purchase_return", options={"expose"=true})
127
     *
128
     * @Method("GET")
129
     */
130
    public function returnAction(Request $request)
131
    {
132
        $items = $request->getSession()->get('paywall_purchase', array());
133
        $purchaseService = $this->get('newscoop_paywall.services.purchase');
134
        $response = $purchaseService->finishPurchase($items);
135
        $request->getSession()->remove('paywall_purchase');
136
137
        if (!$response->isSuccessful() && !$response->isRedirect()) {
138
            return $this->loadErrorTemplate($response->getMessage());
139
        }
140
141
        return $this->redirectToThankYou();
142
    }
143
144
    /**
145
     * @Route("/paywall/cancel/", name="paywall_plugin_purchase_cancel", options={"expose"=true})
146
     *
147
     * @Method("GET")
148
     */
149 View Code Duplication
    public function cancelAction()
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...
150
    {
151
        $templatesService = $this->get('newscoop.templates.service');
152
153
        return new Response($templatesService->fetchTemplate(
154
            '_paywall/cancel.tpl'
155
        ), 200, array('Content-Type' => 'text/html'));
156
    }
157
158
    /**
159
     * @Route("/paywall/thank-you/", name="paywall_plugin_purchase_thank_you", options={"expose"=true})
160
     *
161
     * @Method("GET")
162
     */
163 View Code Duplication
    public function thankYouAction()
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...
164
    {
165
        $templatesService = $this->get('newscoop.templates.service');
166
167
        return new Response($templatesService->fetchTemplate(
168
            '_paywall/thankyou.tpl'
169
        ), 200, array('Content-Type' => 'text/html'));
170
    }
171
172 View Code Duplication
    private function loadErrorTemplate($message = '')
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...
173
    {
174
        $templatesService = $this->get('newscoop.templates.service');
175
176
        return new Response($templatesService->fetchTemplate(
177
            '_paywall/error.tpl',
178
            array('msg' => $message)
179
        ), 200, array('Content-Type' => 'text/html'));
180
    }
181
182
    private function redirectToThankYou()
183
    {
184
        return new RedirectResponse($this->get('router')->generate('paywall_plugin_purchase_thank_you'));
185
    }
186
}
187