-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Customer events to default webhook
1 parent
7b77c45
commit 708022b
Showing
12 changed files
with
193 additions
and
76 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Netflex\Toolbox\Events\Customers; | ||
|
||
class CustomerCreated extends CustomerChangeEvent | ||
{ | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Netflex\Toolbox\Events\Customers; | ||
|
||
class CustomerUpdated extends CustomerChangeEvent | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters