Skip to content

Commit 4a49976

Browse files
committed
webhook attributes transformation fix
1 parent 065c612 commit 4a49976

File tree

9 files changed

+29
-24
lines changed

9 files changed

+29
-24
lines changed

src/Controller/Webhook/AddListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
2929

3030
$webhook = $this->repository->save($type, $params);
3131
if ($webhook === null) {
32-
throw new ApiException('Webhook: \''.$params->listeningUrl().'\' with type: \''.$type.'\' and attribute array already exist', HttpCodes::HTTP_CONFLICT);
32+
throw new ApiException('Webhook: \''.$params->listeningUrl().'\' with type: \''.$type.'\' already exist', HttpCodes::HTTP_CONFLICT);
3333
}
3434

3535
return ResponseFactory::makeJson($webhook, new WebhookTransformer());

src/Controller/Webhook/Symfony.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function addListener(Request $request): Response
2727

2828
$webhook = $this->repository->save($type, $params);
2929
if ($webhook === null) {
30-
throw new ConflictHttpException('Webhook: \''.$params->listeningUrl().'\' with type: \''.$type.'\' and attribute array already exist');
30+
throw new ConflictHttpException('Webhook: \''.$params->listeningUrl().'\' with type: \''.$type.'\' already exist');
3131
}
3232

3333
return ResponseFactory::makeJson($webhook, new WebhookTransformer());

src/Model/Webhook/Params.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class Params
99
/**
1010
* @param non-empty-string $listeningUrl
1111
* @param Priority::* $priority
12-
* @param array<string, string> $attributes
12+
* @param array<non-empty-string, non-empty-string> $attributes
1313
*/
1414
public function __construct(
1515
private string $listeningUrl,
@@ -35,7 +35,7 @@ public function priority(): int
3535
}
3636

3737
/**
38-
* @return array<string, string>
38+
* @return array<non-empty-string, non-empty-string>
3939
*/
4040
public function attributes(): array
4141
{

src/Service/Client/ApiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function waitArray(ResponsePromise $promise, bool $allowInvalidJson = fal
245245
* @param non-empty-string $type
246246
* @param non-empty-string $listeningUrl example: https://example.com/listening/...
247247
* @param Priority::* $priority
248-
* @param array<non-empty-string, string> $requiredAttributes without them can not be webhook dispatched
248+
* @param array<non-empty-string, non-empty-string> $requiredAttributes without them can not be webhook dispatched
249249
* @param array<string, string|array<string>> $requestHeaders
250250
* @throws ApiException
251251
*/

src/Service/Webhook/Dispatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(
1616

1717
/**
1818
* @param non-empty-string $type
19-
* @param array<non-empty-string, string> $attributes
19+
* @param array<non-empty-string, non-empty-string> $attributes
2020
* @param array<non-empty-string, string|array<string>> $headers to apply for http request
2121
* @param array<RequestOptions::*, mixed> $options to apply for http request
2222
*/
@@ -34,7 +34,7 @@ public function dispatch(string $type, array $attributes, bool $synchronouslyFir
3434

3535
/**
3636
* @param non-empty-string $type
37-
* @param array<non-empty-string, string> $attributes
37+
* @param array<non-empty-string, non-empty-string> $attributes
3838
* @param positive-int $delayInSeconds
3939
* @param array<non-empty-string, string|array<string>> $headers to apply on http requests
4040
* @param array<RequestOptions::*, mixed> $options to apply on http requests

src/Service/Webhook/LaravelMysqlRepository.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222

2323
/**
2424
* @param non-empty-string $type
25-
* @param array<string, string> $attributes
25+
* @param array<non-empty-string, non-empty-string> $attributes
2626
* @return iterable<Webhook>
2727
*/
2828
public function loadForDispatching(string $type, array $attributes): iterable
@@ -150,7 +150,7 @@ public function delete(string $webhookId): void
150150
}
151151

152152
/**
153-
* @param array<string, string> $attributes
153+
* @param array<non-empty-string, non-empty-string> $attributes
154154
* @return array<int>
155155
*/
156156
private function getWebhooksForAttributes(ConnectionInterface $connection, array $attributes): array
@@ -183,7 +183,7 @@ private function getWebhooksForAttributes(ConnectionInterface $connection, array
183183
}
184184

185185
/**
186-
* @return array<string, string>
186+
* @return array<non-empty-string, non-empty-string>
187187
*/
188188
private function getWebhookRequiredAttributes(ConnectionInterface $connection, int $webhookId): array
189189
{
@@ -197,13 +197,13 @@ private function getWebhookRequiredAttributes(ConnectionInterface $connection, i
197197
}
198198
}
199199

200-
/** @var array<string, string> */
200+
/** @var array<non-empty-string, non-empty-string> */
201201
return $attributes;
202202
}
203203

204204
/**
205-
* @param array<string, string> $requiredAttributes
206-
* @param array<string, string> $attributes
205+
* @param array<non-empty-string, non-empty-string> $requiredAttributes
206+
* @param array<non-empty-string, non-empty-string> $attributes
207207
*/
208208
private function haveAttributesRequiredAttributes(array $requiredAttributes, array $attributes): bool
209209
{

src/Service/Webhook/ParamsTransformer.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use SimpleAsFuck\ApiToolkit\Model\Webhook\Params;
88
use SimpleAsFuck\ApiToolkit\Model\Webhook\Priority;
99
use SimpleAsFuck\ApiToolkit\Service\Transformation\Transformer;
10-
use SimpleAsFuck\Validator\Rule\ArrayRule\TypedKey;
1110
use SimpleAsFuck\Validator\Rule\Custom\UserClassRule;
1211
use SimpleAsFuck\Validator\Rule\Object\ObjectRule;
1312

@@ -19,10 +18,9 @@ final class ParamsTransformer implements Transformer, UserClassRule
1918
{
2019
public function validate(ObjectRule $rule): Params
2120
{
22-
$attributeType = static fn (TypedKey $key): string => $key->string()->notNull();
2321
$attributes = [];
24-
foreach ($rule->property('attributes')->array()->of($attributeType)->notNull() as $key => $attribute) {
25-
$attributes[(string) $key] = $attribute;
22+
foreach ($rule->property('attributes')->array()->ofObject()->notNull() as $object) {
23+
$attributes[$object->property('key')->string()->notEmpty()->notNull()] = $object->property('value')->string()->notEmpty()->notNull();
2624
}
2725
return new Params(
2826
$rule->property('listeningUrl')->string()->httpUrl([PHP_URL_PATH])->notNull(),
@@ -36,11 +34,18 @@ public function validate(ObjectRule $rule): Params
3634
*/
3735
public function toApi($transformed): \stdClass
3836
{
39-
$apiData = new \stdClass();
40-
$apiData->listeningUrl = $transformed->listeningUrl();
41-
$apiData->priority = $transformed->priority();
42-
$apiData->attributes = $transformed->attributes();
37+
$attributes = [];
38+
foreach ($transformed->attributes() as $key => $value) {
39+
$attributes[] = (object) [
40+
'key' => $key,
41+
'value' => $value,
42+
];
43+
}
4344

44-
return $apiData;
45+
return (object) [
46+
'listeningUrl' => $transformed->listeningUrl(),
47+
'priority' => $transformed->priority(),
48+
'attributes' => $attributes,
49+
];
4550
}
4651
}

src/Service/Webhook/Repository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class Repository
2525
* than listeners who accept anything
2626
*
2727
* @param non-empty-string $type
28-
* @param array<string, string> $attributes with webhook will be dispatched
28+
* @param array<non-empty-string, non-empty-string> $attributes with webhook will be dispatched
2929
* @return iterable<Webhook>
3030
*/
3131
abstract public function loadForDispatching(string $type, array $attributes): iterable;

test/Service/Webhook/LaravelMysqlRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function tearDownAfterClass(): void
6363
*
6464
* @param array<non-empty-string> $expectedUrls
6565
* @param non-empty-string $type
66-
* @param array<string, string> $attributes
66+
* @param array<non-empty-string, non-empty-string> $attributes
6767
*/
6868
public function testLoadForDispatching(array $expectedUrls, string $type, array $attributes): void
6969
{

0 commit comments

Comments
 (0)