Skip to content

Commit eb6012f

Browse files
committed
refactor
1 parent 586a572 commit eb6012f

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

src/LiveComponent/src/Test/TestLiveComponent.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626
*/
2727
final class TestLiveComponent
2828
{
29-
private array $props;
30-
private string $csrfToken;
31-
private object $component;
32-
3329
/**
3430
* @internal
3531
*/
@@ -55,11 +51,9 @@ public function __construct(
5551
$this->metadata->get('route'),
5652
[
5753
'_live_component' => $this->metadata->getName(),
58-
'props' => json_encode($props->getProps()),
54+
'props' => json_encode($props->getProps(), flags: \JSON_THROW_ON_ERROR),
5955
]
6056
));
61-
62-
$this->updateState();
6357
}
6458

6559
public function render(): RenderedComponent
@@ -69,19 +63,15 @@ public function render(): RenderedComponent
6963

7064
public function component(): object
7165
{
72-
if (isset($this->component)) {
73-
return $this->component;
74-
}
75-
7666
$component = $this->factory->get($this->metadata->getName());
7767
$componentAttributes = $this->hydrator->hydrate(
7868
$component,
79-
$this->props,
69+
$this->props(),
8070
[],
8171
$this->metadataFactory->getMetadata($this->metadata->getName()),
8272
);
8373

84-
return $this->component = (new MountedComponent($this->metadata->getName(), $component, $componentAttributes))->getComponent();
74+
return (new MountedComponent($this->metadata->getName(), $component, $componentAttributes))->getComponent();
8575
}
8676

8777
/**
@@ -117,9 +107,7 @@ public function response(): Response
117107

118108
private function request(array $content = [], string $action = null): self
119109
{
120-
if (!isset($this->props)) {
121-
throw new \LogicException('A live component action has redirected and you can no longer access the component.');
122-
}
110+
$csrfToken = $this->csrfToken();
123111

124112
$this->client->request(
125113
'POST',
@@ -130,31 +118,32 @@ private function request(array $content = [], string $action = null): self
130118
'_live_action' => $action,
131119
])
132120
),
133-
parameters: ['data' => json_encode(array_merge($content, ['props' => $this->props]))],
134-
server: ['HTTP_X_CSRF_TOKEN' => $this->csrfToken],
121+
parameters: ['data' => json_encode(array_merge($content, ['props' => $this->props()]))],
122+
server: $csrfToken ? ['HTTP_X_CSRF_TOKEN' => $csrfToken] : [],
135123
);
136124

137-
return $this->updateState();
125+
return $this;
138126
}
139127

140-
private function updateState(): self
128+
private function props(): array
141129
{
142130
$crawler = $this->client->getCrawler();
143131

144-
unset($this->component);
132+
if (!count($node = $crawler->filter('[data-live-props-value]'))) {
133+
throw new \LogicException('A live component action has redirected and you can no longer access the component.');
134+
}
145135

146-
$props = $crawler->filter('[data-live-props-value]');
147-
$csrf = $crawler->filter('[data-live-csrf-value]');
136+
return json_decode($node->attr('data-live-props-value'), true, flags: \JSON_THROW_ON_ERROR);
137+
}
148138

149-
if (!\count($props) || !\count($csrf)) {
150-
unset($this->props, $this->csrfToken);
139+
private function csrfToken(): ?string
140+
{
141+
$crawler = $this->client->getCrawler();
151142

152-
return $this;
143+
if (!count($node = $crawler->filter('[data-live-csrf-value]'))) {
144+
return null;
153145
}
154146

155-
$this->props = json_decode($props->attr('data-live-props-value'), true, flags: \JSON_THROW_ON_ERROR);
156-
$this->csrfToken = $csrf->attr('data-live-csrf-value');
157-
158-
return $this;
147+
return $node->attr('data-live-csrf-value');
159148
}
160149
}

0 commit comments

Comments
 (0)