Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChartWidgetRenderer
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 supports
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 render
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Grid\Widget;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Core\Engine\Domain\Model\PermissionContext;
12use App\Core\Engine\Domain\Model\RelationGridMetadata;
13use App\Core\Engine\Domain\Model\WidgetMetadata;
14use App\Core\Grid\Widget\Application\WidgetAggregationDataServiceInterface;
15use Psr\Http\Message\ServerRequestInterface;
16use Twig\Environment as TwigEnvironment;
17
18/**
19 * Chart Widget Renderer.
20 *
21 * Renders interactive Chart.js analytical charts inside Gridstack tiles.
22 *
23 * @package App\Core\Grid\Widget
24 */
25final readonly class ChartWidgetRenderer implements WidgetRendererInterface
26{
27    private const array ICONS = [
28        'bar'            => 'bi bi-bar-chart-line-fill',
29        'horizontal_bar' => 'bi bi-bar-chart-steps',
30        'line'           => 'bi bi-graph-up',
31        'area'           => 'bi bi-activity',
32        'pie'            => 'bi bi-pie-chart-fill',
33        'doughnut'       => 'bi bi-circle-square',
34        'polar_area'     => 'bi bi-radar',
35    ];
36
37    /**
38     * ChartWidgetRenderer constructor.
39     *
40     * @param WidgetAggregationDataServiceInterface $aggregationService Aggregation engine.
41     * @param TwigEnvironment                       $twig               Twig template environment.
42     */
43    public function __construct(
44        private WidgetAggregationDataServiceInterface $aggregationService,
45        private TwigEnvironment                       $twig
46    ) {
47    }
48
49    /** {@inheritdoc} */
50    public function supports(WidgetMetadata $widget): bool
51    {
52        return $widget->name === 'custom_chart'
53            || $widget->category === 'chart'
54            || $widget->handlerClass === self::class
55            || $widget->handlerClass === 'App\\Core\\Grid\\Widget\\ChartWidgetRenderer';
56    }
57
58    /** {@inheritdoc} */
59    public function render(
60        RelationGridMetadata   $relation,
61        ServerRequestInterface $request,
62        PermissionContext      $context
63    ): string {
64        $params = $relation->widgetParams;
65        $chartType = (string) ($params['chart_type'] ?? 'bar');
66        $chartData = $this->aggregationService->computeChartData($params, $context);
67        $iconClass = self::ICONS[$chartType] ?? 'bi bi-bar-chart-line';
68
69        return $this->twig->render('engine/partials/widget_chart_tile.twig', [
70            'relation'   => $relation,
71            'chart_data' => $chartData,
72            'chart_type' => $chartType,
73            'icon_class' => $iconClass,
74        ]);
75    }
76}