Add Subscriber demo

This commit is contained in:
2021-01-14 15:00:07 +03:00
parent cb1f3a6bb5
commit 45a7e8abce
8 changed files with 83 additions and 17 deletions

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Subscriber extends Model
{
protected $table = 'subscribers';
public static function add($event, $callback, $weight = 0)
{
list($destination_module, $destination_method) = explode(':', $callback);
list($source_module, $source_method) = explode(':', $event);
$code = strtolower($destination_module . '_' . $destination_method . '_after_' . $source_module . '_' . $source_method);
if (Subscriber::where('code', $code)->count() == 0) {
$subscriber = new Subscriber([
'code' => $code,
'destination_module' => strtolower($destination_module),
'destination_method' => strtolower($destination_method),
'source_module' => strtolower($source_module),
'source_method' => strtolower($source_method),
'weight' => $weight
]);
$subscriber->save();
}
}
}