Controller::notifyAction()   A
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

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

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
            } else {
133
                $this->eventDispatcher->dispatch(constant(get_class($event).'::NAME'), $event);
134
            }
135
136 4
            return new Response('');
137 2
        } catch (\InvalidArgumentException $e) {
138 2
            return new Response($e->getMessage(), 400);
139
        }
140
    }
141
}
142