60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Slack\Event;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Slack\Client\Payload;
|
|
|
|
class Factory {
|
|
private const LOGKEY = 'SEf';
|
|
|
|
/**
|
|
* @var array event type to event class mapping
|
|
*/
|
|
public const map = [
|
|
'app_home_opened' => AppHomeOpened::class,
|
|
'member_joined_channel' => MemberJoinedChannel::class,
|
|
'channel_left' => ChannelLeft::class,
|
|
'group_left' => GroupLeft::class,
|
|
'message' => Message::class,
|
|
'reaction_added' => ReactionAdded::class,
|
|
'pin_added' => PinAdded::class,
|
|
'pin_removed' => PinRemoved::class,
|
|
];
|
|
|
|
/**
|
|
* Returns new event instance
|
|
*
|
|
* @param string $type
|
|
* @param array $request
|
|
* @return Base
|
|
*/
|
|
public static function create(string $type,array $request): Base
|
|
{
|
|
$class = Arr::get(self::map,$type,Unknown::class);
|
|
Log::debug(sprintf('%s:Working out Event Class for [%s] as [%s]',static::LOGKEY,$type,$class),['m'=>__METHOD__]);
|
|
|
|
if (App::environment() == 'local')
|
|
file_put_contents('/tmp/event.'.$type,print_r($request,TRUE));
|
|
|
|
return new $class($request);
|
|
}
|
|
|
|
public static function make(Payload $request): Base
|
|
{
|
|
// During the life of the event, this method is called twice - once during Middleware processing, and finally by the Controller.
|
|
static $o = NULL;
|
|
static $or = NULL;
|
|
|
|
if (! $o OR ($or != $request)) {
|
|
$or = $request;
|
|
$o = self::create(Arr::get($request->getData(),'payload.event.type'),Arr::get($request->getData(),'payload'));
|
|
}
|
|
|
|
return $o;
|
|
}
|
|
}
|