Skip to content

Commit

Permalink
Add Customer events to default webhook
Browse files Browse the repository at this point in the history
tommysolsen committed Jan 4, 2024
1 parent 7b77c45 commit 708022b
Showing 12 changed files with 193 additions and 76 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions src/Events/Customers/CustomerChangeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Netflex\Toolbox\Events\Customers;

use Netflex\Structure\Model;
use Netflex\Toolbox\Events\WebhookEvent;

abstract class CustomerChangeEvent extends WebhookEvent
{
/**
* @var mixed
*/
public string $type;
public ?int $customer_id;

public function __construct($type, $customer_id)
{
$this->type = $type;
$this->customer_id = !is_null($customer_id) ? (int)$customer_id : null;
}

public function getCustomer(): ?Model
{
$id = $this->customer_id;
return once(function () use ($id) {
return (config('toolbox-events.user-model', 'App\\Models\\User'))::find($id);
});
}

public function toArray()
{
return [
'type' => $this->type,
'directory_id' => $this->directory_id,
'customer_id' => $this->customer_id,
];
}

}
9 changes: 9 additions & 0 deletions src/Events/Customers/CustomerCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Netflex\Toolbox\Events\Customers;

class CustomerCreated extends CustomerChangeEvent
{


}
23 changes: 23 additions & 0 deletions src/Events/Customers/CustomerDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Netflex\Toolbox\Events\Customers;

use Illuminate\Support\Facades\App;
use Netflex\Structure\Entry;
use Netflex\Structure\Model;
use Netflex\Toolbox\Events\Structures\StructureChangeEvent;

class CustomerDeleted extends CustomerChangeEvent
{
public array $customer_data;

public function __construct($type, array $customer_data)
{
$this->customer_data = $customer_data;
parent::__construct($type, $customer_data['id'] ?? -1);
}

public function getCustomer(): ?Model {
return (new (config('toolbox-events.user-model', 'App\\Models\\User')))->newFromBuilder($this->customer_data);
}
}
8 changes: 8 additions & 0 deletions src/Events/Customers/CustomerUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Netflex\Toolbox\Events\Customers;

class CustomerUpdated extends CustomerChangeEvent
{

}
2 changes: 1 addition & 1 deletion src/Events/Structures/EntryCreated.php
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@

use Netflex\Structure\Model;

class EntryCreated extends StructureEvent
class EntryCreated extends StructureChangeEvent
{
}
2 changes: 1 addition & 1 deletion src/Events/Structures/EntryDeleted.php
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
use Netflex\Structure\Entry;
use Netflex\Structure\Model;

class EntryDeleted extends StructureEvent
class EntryDeleted extends StructureChangeEvent
{

public array $entry_data;
4 changes: 2 additions & 2 deletions src/Events/Structures/EntryUpdated.php
Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@

namespace Netflex\Toolbox\Events\Structures;

use Netflex\Toolbox\Events\Structures\StructureEvent;
use Netflex\Toolbox\Events\Structures\StructureChangeEvent;

class EntryUpdated extends StructureEvent
class EntryUpdated extends StructureChangeEvent
{

}
45 changes: 45 additions & 0 deletions src/Events/Structures/StructureChangeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Netflex\Toolbox\Events\Structures;

use Netflex\Structure\Entry;
use Netflex\Structure\Model;
use Netflex\Toolbox\Events\WebhookEvent;

abstract class StructureChangeEvent extends WebhookEvent implements \Stringable
{
public string $type;
public int $directory_id;
public ?int $entry_id;

public function getEntry(): ?Model
{
$id = $this->entry_id;
/** @var Entry $entry */
$entry = once(function () use ($id) {
return Entry::where('id', $id)->first();
});
return $entry;
}

public function __construct($type, $directory_id, $entry_id)
{
$this->type = $type;
$this->directory_id = $directory_id;
$this->entry_id = !is_null($entry_id) ? (int)$entry_id : null;
}

public function toArray()
{
return [
'type' => $this->type,
'directory_id' => $this->directory_id,
'entry_id' => $this->entry_id,
];
}

public function __toString()
{
return json_encode($this->toArray());
}
}
69 changes: 0 additions & 69 deletions src/Events/Structures/StructureEvent.php

This file was deleted.

63 changes: 63 additions & 0 deletions src/Events/WebhookEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Netflex\Toolbox\Events;

use Netflex\Toolbox\Events\Customers\CustomerCreated;
use Netflex\Toolbox\Events\Customers\CustomerDeleted;
use Netflex\Toolbox\Events\Customers\CustomerUpdated;
use Netflex\Toolbox\Events\Structures\EntryCreated;
use Netflex\Toolbox\Events\Structures\EntryDeleted;
use Netflex\Toolbox\Events\Structures\EntryUpdated;

abstract class WebhookEvent
{

public static function fromRequest(): self
{
$split = explode(".", request()->get('event'));


if ($split[0] === 'entry') {
abort_unless(sizeof($split) == 3, 500);
if ($split[2] === 'created') {
$entry_id = request()->get('entry_id');
abort_unless(!!$entry_id, 500, 'expected created webhook to have entry_id');
return new EntryCreated($split[2], $split[1], $entry_id);
}

if ($split[2] === 'updated') {
$entry_id = request()->get('entry_id');
abort_unless(!!$entry_id, 500, 'expected updated webhook to have entry_id');
return new EntryUpdated($split[2], $split[1], $entry_id);
}

if ($split[2] === 'deleted') {
$entry_id = request()->get('entry_data');
abort_unless(!!$entry_id, 500, 'expected created webhook to have entry_id');
return new EntryDeleted($split[2], $split[1], $entry_id);
}
} else if ($split[0] === 'customer') {
abort_unless(sizeof($split) == 2, 500);
if ($split[1] === 'created') {
$customer_id = request()->get('customer_id');
abort_unless(!!$customer_id, 500, 'expected created webhook to have customer_id');
return new CustomerCreated($split[1], $customer_id);
}

if ($split[1] === 'updated') {
$customer_id = request()->get('customer_id');
abort_unless(!!$customer_id, 500, 'expected updated webhook to have customer_id');
return new CustomerUpdated($split[1], $customer_id);
}

if ($split[1] === 'deleted') {
$customer_id = request()->get('customer_data');
abort_unless(!!$customer_id, 500, 'expected created webhook to have customer_id');
return new CustomerDeleted($split[1], $customer_id);
}

}

throw new \Exception("Invalid part");
}
}
4 changes: 2 additions & 2 deletions src/Http/Controllers/StructureWebhookController.php
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

namespace Netflex\Toolbox\Http\Controllers;

use Netflex\Toolbox\Events\Structures\StructureEvent;
use Netflex\Toolbox\Events\Structures\StructureChangeEvent;

class StructureWebhookController
{
@@ -23,7 +23,7 @@ public function process()

abort_unless($validDigest, 403);

$event = StructureEvent::fromRequest();
$event = StructureChangeEvent::fromRequest();
event($event);
return "OK";
}

0 comments on commit 708022b

Please sign in to comment.