Skip to content

Commit 27920b0

Browse files
committed
refactor: Update CreateUsecase to allow default values on entities
Previously we created a blank entity and set state on it, this meant that any values that had a default value *and* were marked as immutable could not be set ie. `Post->type`. Instead CreateUsecase now creates the entity with the full payload data, so the default are only applied where the values doesn't appear in the payload, and the immutable check works correctly because the value isn't yet set.
1 parent 48b6893 commit 27920b0

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

src/App/Repository/PostRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function getEntity(array $data = null)
129129
)
130130
);
131131

132-
if ($data['form_id']) {
132+
if (!empty($data['form_id'])) {
133133
// Get Hidden Stage Ids to be excluded from results
134134
$excludeStages = $this->form_stage_repo->getHiddenStageIds(
135135
$data['form_id'],

src/Core/Entity/Post.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ class Post extends StaticEntity
4747
protected $contact_id;
4848
protected $data_source_message_id;
4949

50+
// StatefulData
51+
protected function getDefaultData()
52+
{
53+
return [
54+
'type' => 'report',
55+
'locale' => 'en_US',
56+
'published_to' => [],
57+
];
58+
}
59+
5060
// StatefulData
5161
protected function getDerived()
5262
{

src/Core/Usecase/CreateUsecase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function verifyValid(Entity $entity)
111111
*/
112112
protected function getEntity()
113113
{
114-
return $this->repo->getEntity()->setState($this->payload);
114+
return $this->repo->getEntity($this->payload);
115115
}
116116

117117
/**

tests/spec/Core/Usecase/CreateUsecaseSpec.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ private function tryGetEntity($repo, $entity)
3535
$this->setPayload($payload);
3636

3737
// Called by CreateUsecase::getEntity
38-
$repo->getEntity()->willReturn($entity);
39-
$entity->setState($payload)->willReturn($entity);
38+
$repo->getEntity($payload)->willReturn($entity);
4039
}
4140

4241
function it_fails_when_authorization_is_denied($auth, $repo, Entity $entity)

0 commit comments

Comments
 (0)