Skip to content

Commit 0edb00c

Browse files
committed
abstract the only parameter
1 parent 1e85d97 commit 0edb00c

38 files changed

+413
-775
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace Ushahidi\Modules\V5\Actions\Post;
4+
5+
use Ushahidi\Modules\V5\Models\Post\Post;
6+
use Ushahidi\Contracts\Sources;
7+
use Ushahidi\Modules\V5\Http\Resources\LockCollection;
8+
use Ushahidi\Modules\V5\Models\Contact;
9+
use Illuminate\Support\Collection;
10+
use Ushahidi\Modules\V5\Http\Resources\PostValueCollection;
11+
12+
trait HandlePostOnlyParameters
13+
{
14+
public function addHydrateRelationships(Post $post, array $hydrates)
15+
{
16+
foreach ($hydrates as $hydrate) {
17+
switch ($hydrate) {
18+
case 'color':
19+
$post->color = $post->survey ? $post->survey->color : null;
20+
break;
21+
case 'categories':
22+
// $result['categories'] = $post->categories;
23+
break;
24+
case 'completed_stages':
25+
$post->completed_stages = $post->postStages;
26+
27+
break;
28+
case 'post_content':
29+
$post->post_content = $this->getResourcePostContent($post);
30+
break;
31+
case 'translations':
32+
break;
33+
case 'contact':
34+
$post->contact = null;
35+
if ($post->source === Sources::WHATSAPP) {
36+
if ($this->userHasManagePostPermissions()) {
37+
if (isset($post->metadata['contact'])) {
38+
$post->contact = (new Contact)->fill($post->metadata['contact']);
39+
}
40+
} else {
41+
if (isset($post->metadata['contact']['id'])) {
42+
$post->contact = (new Contact)->fill(['id'=>$post->metadata['contact']['id']]);
43+
}
44+
}
45+
}
46+
if ($post->message) {
47+
if ($this->userHasManagePostPermissions()) {
48+
$post->contact = $post->message->contact;
49+
} else {
50+
$post->contact = $post->message->contact->setVisible(["id"]);
51+
}
52+
}
53+
break;
54+
case 'locks':
55+
$post->locks = new LockCollection($post->locks);
56+
break;
57+
58+
case 'source':
59+
$message = $post->message;
60+
$post->source = $post->source ?? ($message && isset($message->type)
61+
? $message->type
62+
: Post::DEFAULT_SOURCE_TYPE);
63+
64+
break;
65+
66+
case 'data_source_message_id':
67+
$post->data_source_message_id = null;
68+
$message = $post->message;
69+
if ($message) {
70+
$post->data_source_message_id = $message->data_source_message_id ?? null;
71+
}
72+
break;
73+
case 'message':
74+
if ($post->message && !$this->userHasManagePostPermissions()) {
75+
$post->message->makeHidden("contact");
76+
}
77+
break;
78+
case 'enabled_languages':
79+
$post->enabled_languages = [
80+
'default' => $post->base_language,
81+
'available' => $post->translations->groupBy('language')->keys()
82+
];
83+
$relations['enabled_languages'] = true;
84+
break;
85+
}
86+
}
87+
return $post;
88+
}
89+
public function hideFieldsUsedByRelationships(Post $post, array $fields = [])
90+
{
91+
foreach ($fields as $field) {
92+
$post->offsetUnset($field);
93+
}
94+
$post->offsetUnset('values_int');
95+
96+
return $post;
97+
}
98+
public function hideUnwantedRelationships(Post $post, array $hydrates)
99+
{
100+
// hide post_content relationships
101+
$post->makeHidden('survey');
102+
$post->makeHidden('valuesVarchar');
103+
$post->makeHidden('valuesInt');
104+
$post->makeHidden('valuesText');
105+
$post->makeHidden('valuesDatetime');
106+
$post->makeHidden('valuesDecimal');
107+
$post->makeHidden('valuesDecimal');
108+
$post->makeHidden('valuesGeometry');
109+
$post->makeHidden('valuesMarkdown');
110+
$post->makeHidden('valuesMedia');
111+
$post->makeHidden('valuesPoint');
112+
$post->makeHidden('valuesRelation');
113+
$post->makeHidden('valuesPostsMedia');
114+
$post->makeHidden('valuesPostsSet');
115+
$post->makeHidden('valuesPostTag');
116+
117+
// hide source relationships
118+
if (!in_array('message', $hydrates)) {
119+
$post->makeHidden('message');
120+
}
121+
122+
// hide completed_stages relationships
123+
$post->makeHidden('postStages');
124+
125+
126+
return $post;
127+
}
128+
129+
private function getResourcePostContent($post)
130+
{
131+
$values = $post->getPostValues(); // Calling method on Post Model
132+
$no_values = $values->count() === 0 ? true : false;
133+
$col = new Collection([
134+
'values' => $values,
135+
'tasks' => $post->survey ? $post->survey->tasks : []
136+
]);
137+
if ($post->survey) {
138+
$post_content = new PostValueCollection($col);
139+
} else {
140+
$post_content = Collection::make([]);
141+
}
142+
return $post_content;
143+
}
144+
145+
public function handleSourceField($post)
146+
{
147+
if (!$post->source) {
148+
$message = $post->message;
149+
$post->source = ($message && isset($message->type)
150+
? $message->type
151+
: Post::DEFAULT_SOURCE_TYPE);
152+
}
153+
return $post;
154+
}
155+
}

src/Ushahidi/Modules/V5/Actions/Post/Handlers/FindPostByIdQueryHandler.php

Lines changed: 3 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@
77
use Ushahidi\Modules\V5\Actions\Post\Handlers\AbstractPostQueryHandler;
88
use Ushahidi\Modules\V5\Actions\Post\Queries\FindPostByIdQuery;
99
use Ushahidi\Modules\V5\Repository\Post\PostRepository;
10-
use Ushahidi\Modules\V5\Models\Post\Post;
11-
use Ushahidi\Modules\V5\Models\Contact;
12-
use Illuminate\Support\Collection;
13-
use Ushahidi\Modules\V5\Http\Resources\PostValueCollection;
14-
use Ushahidi\Modules\V5\Http\Resources\ContactPointerResource;
15-
use Ushahidi\Modules\V5\Http\Resources\MessagePointerResource;
16-
use Ushahidi\Modules\V5\Http\Resources\LockCollection;
17-
use Ushahidi\Modules\V5\Http\Resources\Survey\TaskCollection;
18-
use Ushahidi\Contracts\Sources;
10+
use Ushahidi\Modules\V5\Actions\Post\HandlePostOnlyParameters;
1911

2012
class FindPostByIdQueryHandler extends AbstractPostQueryHandler
2113
{
14+
use HandlePostOnlyParameters;
2215
private $postRepository;
2316

2417
public function __construct(PostRepository $postRepository)
@@ -52,140 +45,7 @@ public function __invoke(Action $action)
5245
$post,
5346
array_diff($action->getFieldsForRelationship(), $action->getFields())
5447
);
48+
$post = $this->handleSourceField($post);
5549
return $this->hideUnwantedRelationships($post, $action->getHydrates());
5650
}
57-
58-
private function addHydrateRelationships(Post $post, array $hydrates)
59-
{
60-
foreach ($hydrates as $hydrate) {
61-
switch ($hydrate) {
62-
case 'color':
63-
$post->color = $post->survey ? $post->survey->color : null;
64-
break;
65-
case 'categories':
66-
// $result['categories'] = $post->categories;
67-
break;
68-
case 'sets':
69-
// $post->sets = $post->sets->pluck('id');
70-
break;
71-
case 'completed_stages':
72-
$post->completed_stages = $post->postStages;
73-
74-
break;
75-
case 'post_content':
76-
$post->post_content = $this->getResourcePostContent($post);
77-
break;
78-
case 'translations':
79-
break;
80-
case 'contact':
81-
$post->contact = null;
82-
if ($post->source === Sources::WHATSAPP) {
83-
if ($this->userHasManagePostPermissions()) {
84-
if (isset($post->metadata['contact'])) {
85-
// $post->contact = Contact::find($post->contact_id); // not good in case there are many whatsapp posts this will be problem for the DB
86-
$post->contact = (new Contact)->fill($post->metadata['contact']);
87-
}
88-
} else {
89-
if (isset($post->metadata['contact']['id'])) {
90-
$post->contact = (new Contact)->fill(['id'=>$post->metadata['contact']['id']]);
91-
}
92-
}
93-
} elseif ($post->message) {
94-
if ($this->userHasManagePostPermissions()) {
95-
$post->contact = $post->message->contact;
96-
} else {
97-
$post->contact = $post->message->contact->setVisible(["id"]);
98-
}
99-
}
100-
break;
101-
case 'locks':
102-
$post->locks = new LockCollection($post->locks);
103-
break;
104-
105-
case 'source':
106-
$message = $post->message;
107-
$post->source = $post->source ?? ($message && isset($message->type)
108-
? $message->type
109-
: Post::DEFAULT_SOURCE_TYPE);
110-
111-
break;
112-
113-
case 'data_source_message_id':
114-
$post->data_source_message_id = null;
115-
$message = $post->message;
116-
if ($message) {
117-
$post->data_source_message_id = $message->data_source_message_id ?? null;
118-
}
119-
break;
120-
case 'message':
121-
if ($post->message && !$this->userHasManagePostPermissions()) {
122-
$post->message->makeHidden("contact");
123-
}
124-
break;
125-
case 'enabled_languages':
126-
$post->enabled_languages = [
127-
'default' => $post->base_language,
128-
'available' => $post->translations->groupBy('language')->keys()
129-
];
130-
$relations['enabled_languages'] = true;
131-
break;
132-
}
133-
}
134-
return $post;
135-
}
136-
private function hideFieldsUsedByRelationships(Post $post, array $fields = [])
137-
{
138-
foreach ($fields as $field) {
139-
$post->offsetUnset($field);
140-
}
141-
$post->offsetUnset('values_int');
142-
143-
return $post;
144-
}
145-
private function hideUnwantedRelationships(Post $post, array $hydrates)
146-
{
147-
// hide post_content relationships
148-
$post->makeHidden('survey');
149-
$post->makeHidden('valuesVarchar');
150-
$post->makeHidden('valuesInt');
151-
$post->makeHidden('valuesText');
152-
$post->makeHidden('valuesDatetime');
153-
$post->makeHidden('valuesDecimal');
154-
$post->makeHidden('valuesDecimal');
155-
$post->makeHidden('valuesGeometry');
156-
$post->makeHidden('valuesMarkdown');
157-
$post->makeHidden('valuesMedia');
158-
$post->makeHidden('valuesPoint');
159-
$post->makeHidden('valuesRelation');
160-
$post->makeHidden('valuesPostsMedia');
161-
$post->makeHidden('valuesPostsSet');
162-
$post->makeHidden('valuesPostTag');
163-
164-
// hide source relationships
165-
if (!in_array('message', $hydrates)) {
166-
$post->makeHidden('message');
167-
}
168-
169-
// hide completed_stages relationships
170-
$post->makeHidden('postStages');
171-
172-
173-
return $post;
174-
}
175-
176-
private function getResourcePostContent($post)
177-
{
178-
$values = $post->getPostValues(); // Calling method on Post Model
179-
$no_values = $values->count() === 0 ? true : false;
180-
$col = new Collection([
181-
'values' => $values,
182-
'tasks' => $post->survey ? $post->survey->tasks : []
183-
]);
184-
if ($post->survey) {
185-
$post_content = new PostValueCollection($col);
186-
} else {
187-
$post_content = Collection::make([]);
188-
}
189-
return $post_content;
190-
}
19151
}

0 commit comments

Comments
 (0)