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

OrderController::batchOrderAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 25
rs 8.8571
cc 3
eloc 16
nc 3
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 Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
16
class OrderController extends BaseController
17
{
18
    /**
19
     * @Route("/{language}/paywall/subscriptions", name="paywall_subscriptions", options={"expose"=true})
20
     *
21
     * @Method("GET")
22
     */
23
    public function indexAction()
24
    {
25
        $response = new Response();
26
        $templatesService = $this->get('newscoop.templates.service');
27
        $response->setContent($templatesService->fetchTemplate('_paywall/index.tpl'));
28
29
        return $response;
30
    }
31
32
    /**
33
     * @Route("/paywall/subscriptions/calculate/{currency}", name="paywall_subscribe_order_calculate", options={"expose"=true})
34
     *
35
     * @Method("POST")
36
     */
37
    public function calculateAction(Request $request, $currency)
38
    {
39
        $items = $request->request->get('batchorder', array());
40
        $userService = $this->get('user');
41
        $response = new JsonResponse();
42
        try {
43
            $userService->getCurrentUser();
44
        } catch (\Exception $e) {
45
            $response->setStatusCode(401);
46
47
            return $response;
48
        }
49
50
        try {
51
            $orderService = $this->get('newscoop_paywall.services.order');
52
            $order = $orderService->processAndCalculateOrderItems($items, $currency);
53
        } catch (\Exception $e) {
54
            $response->setStatusCode(422);
55
56
            return $response;
57
        }
58
59
        return new JsonResponse(array(
60
            'itemsCount' => $order->countItems(),
61
            'total' => $order->getTotal(),
62
            'currency' => $order->getCurrency(),
63
        ));
64
    }
65
}
66