slack/src/Options/Factory.php

54 lines
1.4 KiB
PHP

<?php
namespace Slack\Options;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Slack\Client\Payload;
class Factory {
protected const LOGKEY = 'SOf';
/**
* @var array event type to event class mapping
*/
public const map = [
'block_suggestion' => BlockSuggestion::class,
'interactive_message' => InteractiveMessage::class,
];
/**
* Returns new event instance
*
* @param string $type
* @param array $request
* @return Base
*/
public static function create(string $type,array $request)
{
$class = Arr::get(config('slack.options',self::map),$type,Unknown::class);
Log::debug(sprintf('%s:Working out Interactive Options Event Class for [%s] as [%s]',static::LOGKEY,$type,$class),['m'=>__METHOD__]);
if (App::environment() == 'dev')
file_put_contents('/tmp/option.'.$type,print_r(json_decode($request->input('payload')),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.type'),Arr::get($request->getData(),'payload'));
}
return $o;
}
}