forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookRepository.php
More file actions
111 lines (89 loc) · 2.65 KB
/
Copy pathWebhookRepository.php
File metadata and controls
111 lines (89 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Ushahidi Webhook Repository
*
* @author Ushahidi Team <[email protected]>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\App\Repository;
use Ushahidi\Core\Entity;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Entity\Webhook;
use Ushahidi\Core\Entity\WebhookRepository as WebhookRepositoryContract;
use Ushahidi\Core\Traits\UserContext;
use Ushahidi\Core\Traits\AdminAccess;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Log;
class WebhookRepository extends OhanzeeRepository implements WebhookRepositoryContract
{
use UserContext;
use AdminAccess;
protected function getTable()
{
return 'webhooks';
}
// OhanzeeRepository
public function setSearchConditions(SearchData $search)
{
$query = $this->search_query;
$user = $this->getUser();
// Limit search to user's records unless they are admin
// or if we get user=me as a search param
if (! $this->isUserAdmin($user) || $search->user === 'me') {
$search->user = $this->getUserId();
}
foreach ([
'user'
] as $fk) {
if ($search->$fk) {
$query->where("webhooks.{$fk}_id", '=', $search->$fk);
}
}
}
public function getEntity(array $data = null)
{
return new Webhook($data);
}
public function getByEventType($event_type = null)
{
return $this->getEntity($this->selectOne(compact('event_type')));
}
public function getAllByEventType($event_type = null)
{
$query = $this->selectQuery(compact('event_type'));
$results = $query->execute($this->db());
return $results->as_array();
}
public function getByUUID($webhook_uuid = null)
{
return $this->getEntity($this->selectOne(compact('webhook_uuid')));
}
// CreateRepository
public function create(Entity $entity)
{
$uuid = Uuid::uuid4();
$uuid = $uuid->toString();
$state = [
'user_id' => $entity->user_id,
'webhook_uuid' => $uuid,
'created' => time(),
];
return parent::create($entity->setState($state));
}
// UpdateRepository
public function update(Entity $entity)
{
$record = $entity->asArray();
$record['updated'] = time();
return $this->executeUpdate(['id' => $entity->id], $record);
}
public function getSearchFields()
{
return [
'user'
];
}
}