Completed
Push — master ( bb050e...36e41a )
by Paweł
11:10
created

BridgeController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 10
dl 0
loc 107
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 40 8
A getSDK() 0 9 1
A getClient() 0 13 1
A isValidEndpoint() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Bridge for the Content API.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\BridgeBundle\Controller;
16
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
19
use Superdesk\ContentApiSdk\Api\Authentication\OAuthPasswordAuthentication;
20
use Superdesk\ContentApiSdk\Api\Request\RequestParameters;
21
use Superdesk\ContentApiSdk\Client\ApiClientInterface;
22
use Superdesk\ContentApiSdk\ContentApiSdk;
23
use Superdesk\ContentApiSdk\Exception\ContentApiException;
24
use SWP\Bundle\BridgeBundle\Client\GuzzleApiClient;
25
use SWP\Bundle\BridgeBundle\Client\GuzzleClient;
26
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
27
use Symfony\Component\HttpFoundation\Request;
28
29
/**
30
 * @Route("/bridge")
31
 */
32
class BridgeController extends Controller
33
{
34
    /**
35
     * @Route("/{endpoint}/")
36
     * @Route("/{endpoint}/{objectId}/")
37
     * @Method("GET")
38
     *
39
     * Index action for bridge controller
40
     *
41
     * @param Request     $request
42
     * @param string      $endpoint Endpoint of the api
43
     * @param string|null $objectId Identifier of object to retrieve
44
     *
45
     * @throws ContentApiException
46
     *
47
     * @return \Symfony\Component\HttpFoundation\Response
48
     */
49
    public function indexAction(Request $request, $endpoint, $objectId = null)
50
    {
51
        $data = [];
52
        $apiClient = $this->getClient();
53
        $sdk = $this->getSDK($apiClient);
54
55
        $parameters = $request->query->all();
56
        $requestParams = new RequestParameters();
57
        $requestParams->setQueryParameterArray($parameters);
58
        $endpointPath = sprintf('/%s', $endpoint);
59
60
        if ($this->isValidEndpoint($endpointPath)) {
61
            throw new ContentApiException(sprintf('Endpoint %s not supported.', $endpoint));
62
        }
63
64
        switch ($endpointPath) {
65
            case $sdk::SUPERDESK_ENDPOINT_ITEMS:
66
                if (!is_null($objectId)) {
67
                    $data = $sdk->getItem($objectId);
68
                } else {
69
                    $data = $sdk->getItems($requestParams);
70
                }
71
72
                break;
73
74
            case $sdk::SUPERDESK_ENDPOINT_PACKAGES:
75
                // TODO: Change this in the future to match the superdesk public api parameter name
76
                $resolve = (isset($parameters['resolveItems']) && $parameters['resolveItems']) ? true : false;
77
78
                if (!is_null($objectId)) {
79
                    $data = $sdk->getPackage($objectId, $resolve);
80
                } else {
81
                    $data = $sdk->getPackages($requestParams, $resolve);
82
                }
83
84
                break;
85
        }
86
87
        return $this->render('SWPBridgeBundle:Default:data_dump.html.twig', ['data' => $data]);
88
    }
89
90
    /**
91
     * Get an instance of the sdk.
92
     *
93
     * @param ApiClientInterface $apiClient Api HTTP Client
94
     *
95
     * @return ContentApiSdk
96
     */
97
    private function getSDK(ApiClientInterface $apiClient)
98
    {
99
        return new ContentApiSdk(
100
            $apiClient,
101
            $this->container->getParameter('swp_bridge.api.host'),
102
            $this->container->getParameter('swp_bridge.api.port'),
103
            $this->container->getParameter('swp_bridge.api.protocol')
104
        );
105
    }
106
107
    /**
108
     * Get an instance of the HTTP client. The returned class should implement
109
     * the \Superdesk\ContentApiSdk\Client\ApiClientInterface interface.
110
     *
111
     * @return GuzzleApiClient
112
     */
113
    private function getClient()
114
    {
115
        $authentication = new OAuthPasswordAuthentication(new GuzzleClient());
116
        $authentication
117
            ->setClientId($this->container->getParameter('swp_bridge.auth.client_id'))
118
            ->setUsername($this->container->getParameter('swp_bridge.auth.username'))
119
            ->setPassword($this->container->getParameter('swp_bridge.auth.password'));
120
121
        $apiClient = new GuzzleApiClient(new GuzzleClient(), $authentication);
122
        $apiClient->setOptions($this->container->getParameter('swp_bridge.options'));
123
124
        return $apiClient;
125
    }
126
127
    /**
128
     * Check if the supplied endpoint is supported by the SDK.
129
     *
130
     * @param string $endpoint Endpoint url (/ will be automatically prepended)
131
     *
132
     * @return bool
133
     */
134
    private function isValidEndpoint($endpoint)
135
    {
136
        return !in_array(sprintf('/%s', ltrim($endpoint, '/')), ContentApiSdk::getAvailableEndpoints());
137
    }
138
}
139