Completed
Push — dev-deps-test ( 24d6da...48c4b6 )
by Christian
05:28
created

Controller::signAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
ccs 0
cts 21
cp 0
rs 8.8571
cc 3
eloc 17
nc 4
nop 2
crap 12
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaBundle package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\PandaBundle\Controller;
13
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Xabbuh\PandaBundle\Event\EventFactory;
19
use Xabbuh\PandaClient\Api\CloudManagerInterface;
20
use Xabbuh\PandaClient\Signer\PandaSigner;
21
22
/**
23
 * XabbuhPandaBundle controllers.
24
 *
25
 * @author Christian Flothmann <[email protected]>
26
 */
27
class Controller
28
{
29
    /**
30
     * @var CloudManagerInterface
31
     */
32
    private $cloudManager;
33
34
    /**
35
     * @var EventDispatcherInterface
36
     */
37
    private $eventDispatcher;
38
39
    /**
40
     * @param CloudManagerInterface    $cloudManager
41
     * @param EventDispatcherInterface $eventDispatcher
42
     */
43
    public function __construct(CloudManagerInterface $cloudManager, EventDispatcherInterface $eventDispatcher)
44
    {
45
        $this->cloudManager = $cloudManager;
46
        $this->eventDispatcher = $eventDispatcher;
47
    }
48
49
    /**
50
     * Sign a set of given url parameters and return them as a JSON object.
51
     *
52
     * Specials keys are <em>method</em> and <em>path</em>. <em>method</em> is
53
     * the http method and <em>path</em> the url part of the api request for
54
     * which the signature is generated. The default <em>method</em> if not
55
     * given is <em>GET</em>, the default <em>path</em> is <em>/videos.json</em>.
56
     * All remaining url parameters are treated as parameters for the api
57
     * request.
58
     *
59
     * @param string  $cloud   Cloud to use for performing API requests
60
     * @param Request $request The current request
61
     *
62
     * @return JsonResponse The response
63
     */
64
    public function signAction($cloud, Request $request)
65
    {
66
        $params = $request->query->all();
67
68
        if (isset($params['method'])) {
69
            $method = $params['method'];
70
            unset($params['method']);
71
        } else {
72
            $method = 'GET';
73
        }
74
75
        if (isset($params['path'])) {
76
            $path = $params['path'];
77
            unset($params['path']);
78
        } else {
79
            $path = '/videos.json';
80
        }
81
82
        $httpClient = $this->cloudManager->getCloud($cloud)->getHttpClient();
83
        $cloudId = $httpClient->getCloudId();
84
        $account = $httpClient->getAccount();
85
        $signer = PandaSigner::getInstance($cloudId, $account);
86
87
        return new JsonResponse($signer->signParams($method, $path, $params));
88
    }
89
90
    /**
91
     * Authorize a file upload.
92
     *
93
     * The name of the file to upload and its size are passed as POST parameters.
94
     * The response contains a JSON encoded object. It includes a property
95
     * upload_url to which the caller should send its video file.
96
     *
97
     * @param string  $cloud   Cloud to use for performing API requests
98
     * @param Request $request The current request
99
     *
100
     * @return JsonResponse The response
101
     */
102
    public function authoriseUploadAction($cloud, Request $request)
103
    {
104
        $payload = json_decode($request->request->get('payload'));
105
        $upload = $this->cloudManager
106
            ->getCloud($cloud)
107
            ->registerUpload(
108
                $payload->filename,
109
                $payload->filesize,
110
                null,
111
                true
112
            );
113
114
        return new JsonResponse(array('upload_url' => $upload->location));
115
    }
116
117
    /**
118
     * Endpoint for notification requests.
119
     *
120
     * @param Request $request The current request
121
     *
122
     * @return Response An empty response with status code 200
123
     */
124
    public function notifyAction(Request $request)
125
    {
126
        try {
127
            $event = EventFactory::createEventFromRequest($request);
128
            $this->eventDispatcher->dispatch(constant(get_class($event).'::NAME'), $event);
129
130
            return new Response('');
131
        } catch (\InvalidArgumentException $e) {
132
            return new Response($e->getMessage(), 400);
133
        }
134
    }
135
}
136