Skip to content

Commit 2faaa0e

Browse files
author
awu
committed
feat: refactor DataMapper to introduce tryRequest method with improved error handling
1 parent ece9cab commit 2faaa0e

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

src/DataMapper.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\HttpFoundation\Request;
99
use Wundii\DataMapper\DataMapper as BaseDataMapper;
1010
use Wundii\DataMapper\Enum\SourceTypeEnum;
11+
use Wundii\DataMapper\Exception\DataMapperException;
1112
use Wundii\DataMapper\SymfonyBundle\Enum\MapStatusEnum;
1213

1314
/**
@@ -41,37 +42,43 @@ public function request(
4142
string|object $object,
4243
array $rootElementTree = [],
4344
bool $forceInstance = false,
44-
): object|array|null {
45-
/**
46-
* reset error message and status
47-
*/
48-
$this->errorMessage = null;
49-
$this->mapStatusEnum = MapStatusEnum::AWAIT;
50-
45+
): object|array {
5146
$content = $request->getContent();
5247
$sourceTypeEnum = match ($request->headers->get('Content-Type')) {
5348
'application/json' => SourceTypeEnum::JSON,
5449
'application/neon', 'text/neon' => SourceTypeEnum::NEON,
5550
'application/xml', 'text/xml' => SourceTypeEnum::XML,
5651
'application/yaml', 'text/yaml' => SourceTypeEnum::YAML,
57-
default => null,
52+
default => throw DataMapperException::InvalidArgument('Unsupported content type'),
5853
};
5954

60-
if ($sourceTypeEnum === null) {
61-
$this->errorMessage = 'Unsupported content type';
62-
$this->mapStatusEnum = MapStatusEnum::ERROR;
63-
return null;
64-
}
65-
6655
if ($content === '' && ! $forceInstance) {
67-
$this->errorMessage = 'No content provided in request';
68-
$this->mapStatusEnum = MapStatusEnum::ERROR;
69-
return null;
56+
throw DataMapperException::InvalidArgument('No content provided in request');
7057
}
7158

59+
return $this->map($sourceTypeEnum, $content, $object, $rootElementTree, $forceInstance);
60+
}
61+
62+
/**
63+
* @param class-string<T>|T $object
64+
* @param string[] $rootElementTree
65+
* @param bool $forceInstance // create a new instance, if no data can be found for the object
66+
* @return ($object is class-string ? T : T[])
67+
*/
68+
public function tryRequest(
69+
Request $request,
70+
string|object $object,
71+
array $rootElementTree = [],
72+
bool $forceInstance = false,
73+
): object|array|null {
74+
/**
75+
* reset error message
76+
*/
77+
$this->errorMessage = null;
78+
7279
try {
7380
$this->mapStatusEnum = MapStatusEnum::SUCCESS;
74-
return $this->map($sourceTypeEnum, $content, $object, $rootElementTree, $forceInstance);
81+
return $this->request($request, $object, $rootElementTree, $forceInstance);
7582
} catch (Exception $exception) {
7683
$this->errorMessage = $exception->getMessage();
7784
$this->mapStatusEnum = MapStatusEnum::ERROR;

tests/DataMapperTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testRequestWithInvalidContentTypes(): void
6363
$object = TypeString::class;
6464

6565
$request = $this->getRequest('fail', $content);
66-
$result = $this->dataMapper->request($request, $object);
66+
$result = $this->dataMapper->tryRequest($request, $object);
6767

6868
$this->assertNull($result);
6969
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());
@@ -79,7 +79,7 @@ public function testRequestWithValidContentTypeJson(): void
7979
$object = TypeString::class;
8080

8181
$request = $this->getRequest('application/json', $content);
82-
$result = $this->dataMapper->request($request, $object);
82+
$result = $this->dataMapper->tryRequest($request, $object);
8383

8484
$this->assertNull($this->dataMapper->getErrorMessage());
8585
$this->assertEquals(MapStatusEnum::SUCCESS, $this->dataMapper->getMapStatusEnum());
@@ -95,7 +95,7 @@ public function testRequestWithEmptyContentTypeJson(): void
9595
$object = TypeString::class;
9696

9797
$request = $this->getRequest('application/json', $content);
98-
$result = $this->dataMapper->request($request, $object);
98+
$result = $this->dataMapper->tryRequest($request, $object);
9999

100100
$this->assertNull($result);
101101
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());
@@ -111,7 +111,7 @@ public function testRequestWithEmptyContentTypeJsonAndForceInstance(): void
111111
$object = TypeString::class;
112112

113113
$request = $this->getRequest('application/json', $content);
114-
$result = $this->dataMapper->request($request, $object, [], true);
114+
$result = $this->dataMapper->tryRequest($request, $object, [], true);
115115

116116
$this->assertNull($result);
117117
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());
@@ -127,7 +127,7 @@ public function testRequestWithValidContentTypeNeon(): void
127127
$object = TypeString::class;
128128

129129
$request = $this->getRequest('application/neon', $content);
130-
$result = $this->dataMapper->request($request, $object);
130+
$result = $this->dataMapper->tryRequest($request, $object);
131131

132132
$this->assertNull($this->dataMapper->getErrorMessage());
133133
$this->assertEquals(MapStatusEnum::SUCCESS, $this->dataMapper->getMapStatusEnum());
@@ -143,7 +143,7 @@ public function testRequestWithEmptyContentTypeNeon(): void
143143
$object = TypeString::class;
144144

145145
$request = $this->getRequest('application/neon', $content);
146-
$result = $this->dataMapper->request($request, $object);
146+
$result = $this->dataMapper->tryRequest($request, $object);
147147

148148
$this->assertNull($result);
149149
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());
@@ -159,7 +159,7 @@ public function testRequestWithEmptyContentTypeNeonAndForceInstance(): void
159159
$object = TypeString::class;
160160

161161
$request = $this->getRequest('application/neon', $content);
162-
$result = $this->dataMapper->request($request, $object, [], true);
162+
$result = $this->dataMapper->tryRequest($request, $object, [], true);
163163

164164
$this->assertNull($result);
165165
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());
@@ -176,7 +176,7 @@ public function testRequestWithValidContentTypeXml(): void
176176
$object = TypeString::class;
177177

178178
$request = $this->getRequest('application/xml', $content);
179-
$result = $this->dataMapper->request($request, $object);
179+
$result = $this->dataMapper->tryRequest($request, $object);
180180

181181
$this->assertNull($this->dataMapper->getErrorMessage());
182182
$this->assertEquals(MapStatusEnum::SUCCESS, $this->dataMapper->getMapStatusEnum());
@@ -192,7 +192,7 @@ public function testRequestWithEmptyContentTypeXml(): void
192192
$object = TypeString::class;
193193

194194
$request = $this->getRequest('application/xml', $content);
195-
$result = $this->dataMapper->request($request, $object);
195+
$result = $this->dataMapper->tryRequest($request, $object);
196196

197197
$this->assertNull($result);
198198
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());
@@ -208,7 +208,7 @@ public function testRequestWithEmptyContentTypeXmlAndForceInstance(): void
208208
$object = TypeString::class;
209209

210210
$request = $this->getRequest('application/xml', $content);
211-
$result = $this->dataMapper->request($request, $object, [], true);
211+
$result = $this->dataMapper->tryRequest($request, $object, [], true);
212212

213213
$this->assertNull($result);
214214
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());
@@ -224,7 +224,7 @@ public function testRequestWithValidContentTypeYaml(): void
224224
$object = TypeString::class;
225225

226226
$request = $this->getRequest('application/yaml', $content);
227-
$result = $this->dataMapper->request($request, $object);
227+
$result = $this->dataMapper->tryRequest($request, $object);
228228

229229
$this->assertNull($this->dataMapper->getErrorMessage());
230230
$this->assertEquals(MapStatusEnum::SUCCESS, $this->dataMapper->getMapStatusEnum());
@@ -240,7 +240,7 @@ public function testRequestWithEmptyContentTypeYaml(): void
240240
$object = TypeString::class;
241241

242242
$request = $this->getRequest('application/yaml', $content);
243-
$result = $this->dataMapper->request($request, $object);
243+
$result = $this->dataMapper->tryRequest($request, $object);
244244

245245
$this->assertNull($result);
246246
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());
@@ -256,7 +256,7 @@ public function testRequestWithEmptyContentTypeYamlAndForceInstance(): void
256256
$object = TypeString::class;
257257

258258
$request = $this->getRequest('application/yaml', $content);
259-
$result = $this->dataMapper->request($request, $object, [], true);
259+
$result = $this->dataMapper->tryRequest($request, $object, [], true);
260260

261261
$this->assertNull($result);
262262
$this->assertEquals(MapStatusEnum::ERROR, $this->dataMapper->getMapStatusEnum());

0 commit comments

Comments
 (0)