Completed
Push — master ( 7fe99b...67b589 )
by Rafał
15s queued 10s
created

ContentPushController::findExistingPackage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Controller;
18
19
use Hoa\Mime\Mime;
20
use SWP\Bundle\ContentBundle\Form\Type\MediaFileType;
21
use SWP\Bundle\ContentBundle\Model\ArticleMedia;
22
use SWP\Bundle\ContentBundle\Provider\FileProvider;
23
use SWP\Bundle\CoreBundle\MessageHandler\Message\ContentPushMessage;
24
use SWP\Component\Bridge\Events;
25
use SWP\Component\Common\Response\ResponseContext;
26
use SWP\Component\Common\Response\SingleResourceResponse;
27
use SWP\Component\Common\Response\SingleResourceResponseInterface;
28
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
29
use Symfony\Component\EventDispatcher\GenericEvent;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
use Symfony\Component\Routing\Annotation\Route;
33
34
class ContentPushController extends AbstractController
35
{
36
    /**
37
     * @Route("/api/{version}/content/push", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_content_push")
38
     */
39
    public function pushContentAction(Request $request): SingleResourceResponseInterface
40
    {
41
        $package = $this->container->get('swp_bridge.transformer.json_to_package')->transform($request->getContent());
42
        $this->container->get('event_dispatcher')->dispatch(Events::SWP_VALIDATION, new GenericEvent($package));
43
44
        $currentTenant = $this->container->get('swp_multi_tenancy.tenant_context')->getTenant();
45
46
        $this->dispatchMessage(new ContentPushMessage($currentTenant->getId(), $request->getContent()));
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, SWP\Bundle\CoreBundle\Me...hMessage::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
47
48
        return new SingleResourceResponse(['status' => 'OK'], new ResponseContext(201));
49
    }
50 12
51
    /**
52 12
     * @Route("/api/{version}/assets/push", methods={"POST"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_assets_push")
53 12
     */
54
    public function pushAssetsAction(Request $request): SingleResourceResponseInterface
55 12
    {
56
        $form = $this->get('form.factory')->createNamed('', MediaFileType::class);
57 12
        $form->handleRequest($request);
58
59
        if ($form->isSubmitted() && $form->isValid()) {
60
            $mediaManager = $this->get('swp_content_bundle.manager.media');
61
            $uploadedFile = $form->getData()['media'];
62
            $mediaId = $request->request->get('mediaId');
63
64
            if ($uploadedFile->isValid()) {
65
                $fileProvider = $this->container->get(FileProvider::class);
66 12
                $file = $fileProvider->getFile(ArticleMedia::handleMediaId($mediaId), $uploadedFile->guessExtension());
67 12
68 11
                if (null === $file) {
69 11
                    $file = $mediaManager->handleUploadedFile($uploadedFile, $mediaId);
70
                    $this->get('swp.object_manager.media')->flush();
71 11
                }
72
73
                return new SingleResourceResponse(
74
                    [
75
                        'media_id' => $mediaId,
76
                        'URL' => $mediaManager->getMediaPublicUrl($file),
77
                        'media' => base64_encode($mediaManager->getFile($file)),
78
                        'mime_type' => Mime::getMimeFromExtension($file->getFileExtension()),
79
                        'filemeta' => [],
80
                    ],
81
                    new ResponseContext(201)
82
                );
83
            }
84
85
            throw new \Exception('Uploaded file is not valid:'.$uploadedFile->getErrorMessage());
86
        }
87
88
        return new SingleResourceResponse($form);
89
    }
90
91
    /**
92
     * @Route("/api/{version}/assets/{action}/{mediaId}.{extension}", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, requirements={"mediaId"=".+", "action"="get|push"}, name="swp_api_assets_get")
93
     */
94
    public function getAssetsAction(string $mediaId, string $extension): SingleResourceResponseInterface
95
    {
96
        $fileProvider = $this->container->get(FileProvider::class);
97
        $file = $fileProvider->getFile(ArticleMedia::handleMediaId($mediaId), $extension);
98
99
        if (null === $file) {
100
            throw new NotFoundHttpException('Media don\'t exist in storage');
101
        }
102
103
        $mediaManager = $this->get('swp_content_bundle.manager.media');
104
105
        return new SingleResourceResponse([
106
            'media_id' => $mediaId,
107
            'URL' => $mediaManager->getMediaPublicUrl($file),
108
            'media' => base64_encode($mediaManager->getFile($file)),
109
            'mime_type' => Mime::getMimeFromExtension($file->getFileExtension()),
110
            'filemeta' => [],
111
        ]);
112
    }
113
114
    protected function getPackageRepository()
115
    {
116
        return $this->get('swp.repository.package');
117
    }
118
}
119