Completed
Push — master ( 5f7378...7a35f9 )
by Paweł
08:11
created

CurrentThemeController::listSettingsAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2018 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 2018 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\Component\Common\Response\SingleResourceResponseInterface;
23
use Symfony\Component\Routing\Annotation\Route;
24
use SWP\Bundle\CoreBundle\Context\ScopeContextInterface;
25
use SWP\Bundle\CoreBundle\Form\Type\ThemeLogoUploadType;
26
use SWP\Bundle\CoreBundle\Theme\Provider\ThemeLogoProviderInterface;
27
use SWP\Component\Common\Response\ResponseContext;
28
use SWP\Component\Common\Response\SingleResourceResponse;
29
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
30
use Symfony\Component\HttpFoundation\Request;
31
32
class CurrentThemeController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
33
{
34
    /**
35
     * Uploads current theme logo.
36
     *
37
     * @Operation(
38
     *     tags={"theme"},
39
     *     summary="Uploads current theme logo",
40
     *     @SWG\Parameter(
41
     *         name="logo",
42
     *         in="formData",
43
     *         description="Logo file",
44
     *         required=false,
45
     *         type="file"
46
     *     ),
47
     *     @SWG\Response(
48
     *         response="201",
49
     *         description="Returned on success.",
50
     *         @Model(type=\SWP\Bundle\CoreBundle\Model\Settings::class, groups={"api"})
51
     *     )
52
     * )
53
     *
54
     * @Route("/api/{version}/theme/logo_upload/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_upload_theme_logo_2")
55
     * @Route("/api/{version}/theme/logo_upload/{type}", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_upload_theme_logo")
56
     */
57
    public function uploadThemeLogoAction(Request $request, string $type = ThemeLogoProviderInterface::SETTING_NAME_DEFAULT): SingleResourceResponseInterface
58
    {
59
        $themeContext = $this->get('swp_core.theme.context.tenant_aware');
60
61
        if (null === ($theme = $themeContext->getTheme())) {
62
            throw new \LogicException('Theme is not set!');
63
        }
64
65
        $form = $this->get('form.factory')->createNamed('', ThemeLogoUploadType::class, $theme);
66
        $form->handleRequest($request);
67
68
        if ($form->isSubmitted() && $form->isValid()) {
69
            $tenantContext = $this->get('swp_multi_tenancy.tenant_context');
70
            $currentTenant = $tenantContext->getTenant();
71
72
            try {
73
                $settingsManager = $this->get('swp_settings.manager.settings');
74
                $setting = $settingsManager->get($type, ScopeContextInterface::SCOPE_THEME, $currentTenant);
75
                $theme->setLogoPath($setting);
76
                $themeLogoUploader = $this->get('swp_core.uploader.theme_logo');
77
                $themeLogoUploader->upload($theme);
78
            } catch (\Exception $e) {
79
                return new SingleResourceResponse(['message' => 'Could not upload logo.'], new ResponseContext(400));
80
            }
81
82
            $settingsManager = $this->get('swp_settings.manager.settings');
83
            $setting = $settingsManager->set($type, $theme->getLogoPath(), ScopeContextInterface::SCOPE_THEME, $currentTenant);
84
85
            return new SingleResourceResponse($setting, new ResponseContext(201));
86
        }
87
88
        return new SingleResourceResponse($form, new ResponseContext(400));
89
    }
90
91
    /**
92
     * Lists current theme settings.
93
     *
94
     * @Operation(
95
     *     tags={"theme"},
96
     *     summary="Lists current theme settings",
97
     *     @SWG\Response(
98
     *         response="200",
99
     *         description="Returned on success.",
100
     *          @Model(type=\SWP\Bundle\CoreBundle\Model\Settings::class, groups={"api"})
101
     *     )
102
     * )
103
     *
104
     * @Route("/api/{version}/theme/settings/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_theme_settings_list")
105
     */
106
    public function listSettingsAction(): SingleResourceResponseInterface
107
    {
108
        $themeContext = $this->get('swp_core.theme.context.tenant_aware');
109
110
        if (null === $themeContext->getTheme()) {
111
            throw new \LogicException('Theme is not set!');
112
        }
113
114
        $tenantContext = $this->get('swp_multi_tenancy.tenant_context');
115
        $settingsManager = $this->get('swp_settings.manager.settings');
116
        $settings = $settingsManager->getByScopeAndOwner(ScopeContextInterface::SCOPE_THEME, $tenantContext->getTenant());
117
118
        return new SingleResourceResponse($settings);
119
    }
120
}
121