Passed
Branch master (c87ba8)
by Christian
16:02
created

WidgetAjaxController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getContent() 0 21 4
A savePositions() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Dashboard\Controller;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use TYPO3\CMS\Core\Http\JsonResponse;
23
use TYPO3\CMS\Dashboard\Dashboard;
24
use TYPO3\CMS\Dashboard\DashboardRepository;
25
use TYPO3\CMS\Dashboard\WidgetRegistry;
26
use TYPO3\CMS\Dashboard\Widgets\EventDataInterface;
27
use TYPO3\CMS\Dashboard\Widgets\WidgetInterface;
28
29
/**
30
 * @internal
31
 */
32
class WidgetAjaxController extends AbstractController
33
{
34
    /**
35
     * @var Dashboard
36
     */
37
    protected $currentDashboard;
38
39
    /**
40
     * @var DashboardRepository
41
     */
42
    protected $dashboardRepository;
43
44
    /**
45
     * @var WidgetRegistry
46
     */
47
    protected $widgetRegistry;
48
49
    public function __construct(DashboardRepository $dashboardRepository, WidgetRegistry $widgetRegistry)
50
    {
51
        $this->dashboardRepository = $dashboardRepository;
52
        $this->widgetRegistry = $widgetRegistry;
53
54
        $this->currentDashboard = $this->dashboardRepository->getDashboardByIdentifier($this->loadCurrentDashboard());
55
    }
56
57
    /**
58
     * @param ServerRequestInterface $request
59
     * @return ResponseInterface
60
     * @throws \Exception
61
     */
62
    public function getContent(ServerRequestInterface $request): ResponseInterface
63
    {
64
        $queryParams = $request->getQueryParams();
65
66
        try {
67
            $widgetObject = $this->widgetRegistry->getAvailableWidget((string)$queryParams['widget']);
68
        } catch (\InvalidArgumentException $e) {
69
            return new JsonResponse(['error' => 'Widget is not available!']);
70
        }
71
72
        if (!$widgetObject instanceof WidgetInterface) {
0 ignored issues
show
introduced by
$widgetObject is always a sub-type of TYPO3\CMS\Dashboard\Widgets\WidgetInterface.
Loading history...
73
            return new JsonResponse(['error' => 'Widget doesn\'t have a valid widget class']);
74
        }
75
76
        $data = [
77
            'widget' => $queryParams['widget'],
78
            'content' => $widgetObject->renderWidgetContent(),
79
            'eventdata' => $widgetObject instanceof EventDataInterface ? $widgetObject->getEventData() : [],
80
        ];
81
82
        return new JsonResponse($data);
83
    }
84
85
    /**
86
     * Get the order of the widgets from the request and save the order in the database by using the repository
87
     *
88
     * @param ServerRequestInterface $request
89
     * @return ResponseInterface
90
     */
91
    public function savePositions(ServerRequestInterface $request): ResponseInterface
92
    {
93
        $body = $request->getParsedBody();
94
        $widgets = [];
95
        foreach ($body['widgets'] as $widget) {
96
            $widgets[$widget[1]] = ['identifier' => $widget[0]];
97
        }
98
99
        if ($this->currentDashboard !== null) {
100
            $this->dashboardRepository->updateWidgetConfig($this->currentDashboard, $widgets);
101
        }
102
        return new JsonResponse(['status' => 'saved']);
103
    }
104
}
105