Completed
Push — dev-deps-test ( e45ca1...447eac )
by Christian
09:06
created

Controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 9
    public function __construct(CloudManagerInterface $cloudManager, EventDispatcherInterface $eventDispatcher)
44
    {
45 9
        $this->cloudManager = $cloudManager;
46 9
        $this->eventDispatcher = $eventDispatcher;
47 9
    }
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 2
    public function signAction($cloud, Request $request)
65
    {
66 2
        $params = $request->query->all();
67
68 2
        if (isset($params['method'])) {
69 1
            $method = $params['method'];
70 1
            unset($params['method']);
71
        } else {
72 2
            $method = 'GET';
73
        }
74
75 2
        if (isset($params['path'])) {
76 1
            $path = $params['path'];
77 1
            unset($params['path']);
78
        } else {
79 2
            $path = '/videos.json';
80
        }
81
82 2
        $httpClient = $this->cloudManager->getCloud($cloud)->getHttpClient();
83 2
        $cloudId = $httpClient->getCloudId();
84 2
        $account = $httpClient->getAccount();
85 2
        $signer = PandaSigner::getInstance($cloudId, $account);
86
87 2
        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 1
    public function authoriseUploadAction($cloud, Request $request)
103
    {
104 1
        $payload = json_decode($request->request->get('payload'));
105 1
        $upload = $this->cloudManager
106 1
            ->getCloud($cloud)
107 1
            ->registerUpload(
108 1
                $payload->filename,
109 1
                $payload->filesize,
110 1
                null,
111 1
                true
112
            );
113
114 1
        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 6
    public function notifyAction(Request $request)
125
    {
126
        try {
127 6
            $event = EventFactory::createEventFromRequest($request);
128 4
            $this->eventDispatcher->dispatch(constant(get_class($event).'::NAME'), $event);
129
130 4
            return new Response('');
131 2
        } catch (\InvalidArgumentException $e) {
132 2
            return new Response($e->getMessage(), 400);
133
        }
134
    }
135
}
136