Passed
Push — trunk ( c56b80...32ae88 )
by Christian
11:56 queued 12s
created

SystemConfigController::saveKeyValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\System\SystemConfig\Api;
4
5
use Shopware\Core\Framework\Context;
6
use Shopware\Core\Framework\Feature;
7
use Shopware\Core\Framework\Log\Package;
8
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
9
use Shopware\Core\System\SystemConfig\Service\ConfigurationService;
10
use Shopware\Core\System\SystemConfig\SystemConfigService;
11
use Shopware\Core\System\SystemConfig\Validation\SystemConfigValidator;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
#[Route(defaults: ['_routeScope' => ['api']])]
19
#[Package('system-settings')]
20
class SystemConfigController extends AbstractController
21
{
22
    /**
23
     * @internal
24
     */
25
    public function __construct(
26
        private readonly ConfigurationService $configurationService,
27
        private readonly SystemConfigService $systemConfig,
28
        private readonly SystemConfigValidator $systemConfigValidator
29
    ) {
30
    }
31
32
    #[Route(path: '/api/_action/system-config/check', name: 'api.action.core.system-config.check', defaults: ['_acl' => ['system_config:read']], methods: ['GET'])]
33
    public function checkConfiguration(Request $request, Context $context): JsonResponse
34
    {
35
        $domain = (string) $request->query->get('domain');
36
37
        if ($domain === '') {
38
            return new JsonResponse(false);
39
        }
40
41
        return new JsonResponse($this->configurationService->checkConfiguration($domain, $context));
42
    }
43
44
    #[Route(path: '/api/_action/system-config/schema', name: 'api.action.core.system-config', methods: ['GET'])]
45
    public function getConfiguration(Request $request, Context $context): JsonResponse
46
    {
47
        $domain = (string) $request->query->get('domain');
48
49
        if ($domain === '') {
50
            throw new MissingRequestParameterException('domain');
51
        }
52
53
        return new JsonResponse($this->configurationService->getConfiguration($domain, $context));
54
    }
55
56
    #[Route(path: '/api/_action/system-config', name: 'api.action.core.system-config.value', defaults: ['_acl' => ['system_config:read']], methods: ['GET'])]
57
    public function getConfigurationValues(Request $request): JsonResponse
58
    {
59
        $domain = (string) $request->query->get('domain');
60
        if ($domain === '') {
61
            throw new MissingRequestParameterException('domain');
62
        }
63
64
        $salesChannelId = $request->query->get('salesChannelId');
65
        if (!\is_string($salesChannelId)) {
66
            $salesChannelId = null;
67
        }
68
69
        $inherit = $request->query->getBoolean('inherit');
70
71
        $values = $this->systemConfig->getDomain($domain, $salesChannelId, $inherit);
72
        if (empty($values)) {
73
            $json = '{}';
74
        } else {
75
            $json = json_encode($values, \JSON_PRESERVE_ZERO_FRACTION);
76
        }
77
78
        return new JsonResponse($json, 200, [], true);
79
    }
80
81
    #[Route(path: '/api/_action/system-config', name: 'api.action.core.save.system-config', defaults: ['_acl' => ['system_config:update', 'system_config:create', 'system_config:delete']], methods: ['POST'])]
82
    public function saveConfiguration(Request $request): JsonResponse
83
    {
84
        $salesChannelId = $request->query->get('salesChannelId');
85
        if (!\is_string($salesChannelId)) {
86
            $salesChannelId = null;
87
        }
88
89
        $kvs = $request->request->all();
90
        $this->systemConfig->setMultiple($kvs, $salesChannelId);
91
92
        return new JsonResponse(null, Response::HTTP_NO_CONTENT);
93
    }
94
95
    /**
96
     * @deprecated tag:v6.6.0 $context param will be required
97
     */
98
    #[Route(path: '/api/_action/system-config/batch', name: 'api.action.core.save.system-config.batch', defaults: ['_acl' => ['system_config:update', 'system_config:create', 'system_config:delete']], methods: ['POST'])]
99
    public function batchSaveConfiguration(Request $request, ?Context $context = null): JsonResponse
100
    {
101
        if (!$context) {
102
            Feature::triggerDeprecationOrThrow(
103
                'v6.6.0.0',
104
                'Second parameter `$context` will be required in method `batchSaveConfiguration()` in `SystemConfigController` in v6.6.0.0'
105
            );
106
107
            $context = Context::createDefaultContext();
108
        }
109
110
        $this->systemConfigValidator->validate($request->request->all(), $context);
111
112
        /**
113
         * @var string $salesChannelId
114
         * @var array<string, mixed> $kvs
115
         */
116
        foreach ($request->request->all() as $salesChannelId => $kvs) {
117
            if ($salesChannelId === 'null') {
118
                $salesChannelId = null;
119
            }
120
121
            $this->systemConfig->setMultiple($kvs, $salesChannelId);
122
        }
123
124
        return new JsonResponse(null, Response::HTTP_NO_CONTENT);
125
    }
126
}
127