OrderController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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