Completed
Push — master ( e48710...5fe26c )
by Rafał
40:26 queued 26:25
created

SeoMediaController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 87
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getAction() 0 4 1
B uploadSeoImageAction() 0 30 6
A findOr404() 0 8 2
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 2019 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 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Controller;
18
19
use Doctrine\Common\Cache\Cache;
20
use Doctrine\Common\Persistence\ObjectManager;
21
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
22
use SWP\Bundle\ContentBundle\Controller\AbstractMediaController;
23
use SWP\Bundle\ContentBundle\File\FileExtensionCheckerInterface;
24
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface;
25
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface;
26
use SWP\Bundle\ContentBundle\Provider\FileProviderInterface;
27
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
28
use SWP\Bundle\CoreBundle\Service\SeoImageUploaderInterface;
29
use SWP\Bundle\SeoBundle\Form\Type\SeoMetadataType;
30
use SWP\Component\Common\Exception\NotFoundHttpException;
31
use SWP\Component\Common\Response\ResponseContext;
32
use SWP\Component\Common\Response\SingleResourceResponse;
33
use SWP\Component\Storage\Factory\FactoryInterface;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\Routing\Annotation\Route;
36
37
class SeoMediaController extends AbstractMediaController
38
{
39
    private $seoMetadataFactory;
40
41
    private $articleProvider;
42
43
    private $seoObjectManager;
44
45
    public function __construct(
46
        FactoryInterface $seoMetadataFactory,
47
        ArticleProviderInterface $articleProvider,
48
        ObjectManager $seoObjectManager,
49
        MediaManagerInterface $seoMediaManager,
50
        Cache $cacheProvider,
51
        FileProviderInterface $fileProvider,
52
        FileExtensionCheckerInterface $fileExtensionChecker
53
    ) {
54
        $this->seoMetadataFactory = $seoMetadataFactory;
55
        $this->articleProvider = $articleProvider;
56
        $this->seoObjectManager = $seoObjectManager;
57
58
        parent::__construct($seoMediaManager, $cacheProvider, $fileProvider, $fileExtensionChecker);
59
    }
60
61
    /**
62
     * @Route("/media/seo/{mediaId}.{extension}", methods={"GET"}, options={"expose"=true}, requirements={"mediaId"=".+"}, name="swp_seo_media_get")
63
     */
64
    public function getAction(string $mediaId, string $extension): Response
65
    {
66
        return $this->getMedia($mediaId, $extension);
67
    }
68
69
    /**
70
     * @ApiDoc(
71
     *     resource=true,
72
     *     description="Uploads SEO image",
73
     *     statusCodes={
74
     *         201="Returned on success."
75
     *     },
76
     *     input="SWP\Bundle\ContentBundle\Form\Type\ImageUploadType"
77
     * )
78
     * @Route("/api/{version}/upload/seo_image/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_upload_seo_image")
79
     *
80
     * @param Request $request
81
     *
82
     * @return SingleResourceResponse
83
     */
84
    public function uploadSeoImageAction(Request $request, string $id, SeoImageUploaderInterface $seoImageUploader): SingleResourceResponse
85
    {
86
        $article = $this->findOr404($id);
87
        $seoMetadata = $article->getSeoMetadata();
88
89
        if (null === $seoMetadata) {
90
            $seoMetadata = $this->seoMetadataFactory->create();
91
        }
92
93
        $form = $this->get('form.factory')->createNamed('', SeoMetadataType::class, $seoMetadata);
94
        $form->handleRequest($request);
95
96
        if ($form->isSubmitted() && $form->isValid()) {
97
            try {
98
                $seoImageUploader->handleUpload($seoMetadata);
99
100
                if (null === $article->getSeoMetadata()) {
101
                    $article->setSeoMetadata($seoMetadata);
102
                }
103
104
                $this->seoObjectManager->flush();
105
            } catch (\Exception $e) {
106
                return new SingleResourceResponse(['message' => 'Could not upload an image.'], new ResponseContext(400));
107
            }
108
109
            return new SingleResourceResponse($seoMetadata, new ResponseContext(201));
110
        }
111
112
        return new SingleResourceResponse($form, new ResponseContext(400));
113
    }
114
115
    private function findOr404(string $id): ArticleInterface
116
    {
117
        if (null === $article = $this->articleProvider->getOneById($id)) {
118
            throw new NotFoundHttpException('Article was not found.');
119
        }
120
121
        return $article;
122
    }
123
}
124