Completed
Push — master ( 678bac...955920 )
by Paweł
09:19 queued 10s
created

PackageSeoMediaUploadController::uploadAction()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 26

Duplication

Lines 12
Ratio 46.15 %

Importance

Changes 0
Metric Value
dl 12
loc 26
rs 9.1928
c 0
b 0
f 0
cc 5
nc 10
nop 3
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 Nelmio\ApiDocBundle\Annotation\Model;
20
use Nelmio\ApiDocBundle\Annotation\Operation;
21
use Swagger\Annotations as SWG;
22
use SWP\Bundle\CoreBundle\Service\SeoImageUploaderInterface;
23
use SWP\Bundle\SeoBundle\Form\Type\SeoImageType;
24
use SWP\Component\Common\Response\ResponseContext;
25
use SWP\Component\Common\Response\SingleResourceResponse;
26
use SWP\Component\Storage\Factory\FactoryInterface;
27
use SWP\Component\Storage\Repository\RepositoryInterface;
28
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\Routing\Annotation\Route;
31
32
class PackageSeoMediaUploadController extends AbstractController
33
{
34
    private $seoMetadataFactory;
35
36
    private $seoMetadataRepository;
37
38
    public function __construct(
39
        FactoryInterface $seoMetadataFactory,
40
        RepositoryInterface $seoMetadataRepository
41
    ) {
42
        $this->seoMetadataFactory = $seoMetadataFactory;
43
        $this->seoMetadataRepository = $seoMetadataRepository;
44
    }
45
46
    /**
47
     * @Operation(
48
     *     tags={"seo"},
49
     *     summary="Uploads SEO image for Package",
50
     *     @SWG\Parameter(
51
     *         name="metaMediaFile",
52
     *         in="formData",
53
     *         description="",
54
     *         required=false,
55
     *         type="file"
56
     *     ),
57
     *     @SWG\Parameter(
58
     *         name="ogMediaFile",
59
     *         in="formData",
60
     *         description="",
61
     *         required=false,
62
     *         type="file"
63
     *     ),
64
     *     @SWG\Parameter(
65
     *         name="twitterMediaFile",
66
     *         in="formData",
67
     *         description="",
68
     *         required=false,
69
     *         type="file"
70
     *     ),
71
     *     @SWG\Response(
72
     *         response="201",
73
     *         description="Returned on success.",
74
     *         @Model(type=\SWP\Bundle\ContentBundle\Model\ArticleSeoMetadata::class, groups={"api"})
75
     *     )
76
     * )
77
     *
78
     * @Route("/api/{version}/packages/seo/upload/{packageGuid}", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_upload_package_seo_image")
79
     *
80
     * @param Request $request
81
     *
82
     * @return SingleResourceResponse
83
     */
84
    public function uploadAction(Request $request, string $packageGuid, SeoImageUploaderInterface $seoImageUploader): SingleResourceResponse
85
    {
86
        $seoMetadata = $this->seoMetadataRepository->findOneByPackageGuid($packageGuid);
0 ignored issues
show
Bug introduced by
The method findOneByPackageGuid() does not exist on SWP\Component\Storage\Re...ory\RepositoryInterface. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
87
88
        if (null === $seoMetadata) {
89
            $seoMetadata = $this->seoMetadataFactory->create();
90
        }
91
92
        $form = $this->get('form.factory')->createNamed('', SeoImageType::class, $seoMetadata);
93
        $form->handleRequest($request);
94
95 View Code Duplication
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            try {
97
                $seoMetadata->setPackageGuid($packageGuid);
98
                $seoImageUploader->handleUpload($seoMetadata);
99
100
                $this->seoMetadataRepository->add($seoMetadata);
101
            } catch (\Exception $e) {
102
                return new SingleResourceResponse(['message' => 'Could not upload an image.'], new ResponseContext(400));
103
            }
104
105
            return new SingleResourceResponse($seoMetadata, new ResponseContext(201));
106
        }
107
108
        return new SingleResourceResponse($form, new ResponseContext(400));
109
    }
110
}
111