• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / handlebars / 34103667826

07 Sep 2026 09:00AM UTC coverage: 89.692% (-0.4%) from 90.104%
34103667826

push

github

web-flow
Merge pull request #635 from CPS-IT/feature/ds-streamline

[!!!][FEATURE] Streamline data source handling across all data processors

53 of 65 new or added lines in 4 files covered. (81.54%)

5 existing lines in 3 files now uncovered.

1662 of 1853 relevant lines covered (89.69%)

6.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/Classes/DataProcessing/DataSource/DataSourceCollection.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars".
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 CPSIT\Typo3Handlebars\DataProcessing\DataSource;
19

20
use TYPO3\CMS\Frontend;
21

22
/**
23
 * DataSourceCollection
24
 *
25
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
26
 * @license GPL-2.0-or-later
27
 */
28
final class DataSourceCollection
29
{
30
    /**
31
     * @var array<value-of<DataSource>, array<string|int, mixed>>
32
     */
33
    private array $dataSources = [];
34

35
    /**
36
     * @param array<string, mixed>|null $contentObjectConfiguration
37
     * @param array<string, mixed>|null $processorConfiguration
38
     * @param array<string|int, mixed>|null $processedData
39
     */
40
    public static function for(
2✔
41
        ?Frontend\ContentObject\ContentObjectRenderer $cObj = null,
42
        ?array $contentObjectConfiguration = null,
43
        ?array $processorConfiguration = null,
44
        ?array $processedData = null,
45
    ): self {
46
        $collection = new self();
2✔
47

48
        if ($cObj !== null) {
2✔
49
            $collection->set(DataSource::ContentObjectRenderer, $cObj->data);
1✔
50
        }
51
        if ($contentObjectConfiguration !== null) {
2✔
52
            $collection->set(DataSource::ContentObjectConfiguration, $contentObjectConfiguration);
1✔
53
        }
54
        if ($processedData !== null) {
2✔
55
            $collection->set(DataSource::ProcessedData, $processedData);
2✔
56
        }
57
        if ($processorConfiguration !== null) {
2✔
58
            $collection->set(DataSource::ProcessorConfiguration, $processorConfiguration);
1✔
59
        }
60

61
        return $collection;
2✔
62
    }
63

64
    /**
65
     * @return array<string|int, mixed>
66
     */
67
    public function get(DataSource $dataSource): array
11✔
68
    {
69
        return $this->dataSources[$dataSource->value] ?? [];
11✔
70
    }
71

72
    /**
73
     * @param array<string|int, mixed> $configuration
74
     */
75
    public function set(DataSource $dataSource, array $configuration): self
11✔
76
    {
77
        $this->dataSources[$dataSource->value] = $configuration;
11✔
78

79
        return $this;
11✔
80
    }
81

82
    public function has(DataSource $dataSource): bool
3✔
83
    {
84
        return array_key_exists($dataSource->value, $this->dataSources);
3✔
85
    }
86

87
    public function remove(DataSource $dataSource): self
1✔
88
    {
89
        unset($this->dataSources[$dataSource->value]);
1✔
90

91
        return $this;
1✔
92
    }
93

94
    /**
95
     * @template T
96
     * @param non-empty-string $key
97
     * @param DataSource|list<DataSource> $dataSources
98
     * @param T $default
99
     * @return mixed|T
100
     */
101
    public function resolve(string $key, DataSource|array $dataSources = [], mixed $default = null): mixed
7✔
102
    {
103
        // Get from all configured data sources (in the given order) if no data sources are configured explicitly
104
        // The order can be seen as priority for each single data source
105
        if ($dataSources === []) {
7✔
106
            $dataSources = $this->getConfiguredDataSourcesSortedByPriority();
1✔
107
        } elseif ($dataSources instanceof DataSource) {
6✔
108
            $dataSources = [$dataSources];
5✔
109
        }
110

111
        foreach ($dataSources as $dataSource) {
7✔
112
            $found = false;
7✔
113
            $result = $this->resolveForDataSource($key, $dataSource, $found);
7✔
114

115
            if ($found) {
7✔
116
                return $result;
6✔
117
            }
118
        }
119

120
        return $default;
1✔
121
    }
122

UNCOV
123
    public function resolveCurrentUid(): int|string
×
124
    {
125
        $uid = $this->resolve('uid', DataSource::ContentObjectRenderer);
×
126

127
        if (!is_numeric($uid)) {
×
128
            return '*unknown*';
×
129
        }
130

131
        return (int)$uid;
×
132
    }
133

134
    /**
135
     * @param DataSource|list<DataSource> $dataSources
136
     */
137
    public function with(string $key, mixed $value, DataSource|array $dataSources = []): self
3✔
138
    {
139
        // Apply to all configured data sources if no data sources are configured explicitly
140
        if ($dataSources === []) {
3✔
141
            foreach ($this->dataSources as $dataSource => $configuration) {
1✔
142
                $this->dataSources[$dataSource][$key] = $value;
1✔
143
            }
144

145
            return $this;
1✔
146
        }
147

148
        if ($dataSources instanceof DataSource) {
2✔
149
            $dataSources = [$dataSources];
1✔
150
        }
151

152
        foreach ($dataSources as $dataSource) {
2✔
153
            $this->dataSources[$dataSource->value] ??= [];
2✔
154
            $this->dataSources[$dataSource->value][$key] = $value;
2✔
155
        }
156

157
        return $this;
2✔
158
    }
159

160
    private function resolveForDataSource(string $key, DataSource $dataSource, bool &$found = false): mixed
7✔
161
    {
162
        $configuration = $this->get($dataSource);
7✔
163
        $found = array_key_exists($key, $configuration);
7✔
164

165
        return $found ? $configuration[$key] : null;
7✔
166
    }
167

168
    /**
169
     * @return list<DataSource>
170
     */
171
    private function getConfiguredDataSourcesSortedByPriority(): array
1✔
172
    {
173
        $dataSources = array_map(DataSource::from(...), array_keys($this->dataSources));
1✔
174

175
        return DataSource::sortByPriority($dataSources);
1✔
176
    }
177
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc