1
|
|
|
<?php |
2
|
|
|
namespace PortlandLabs\Slackbot\Slack\Rtm\Event; |
3
|
|
|
|
4
|
|
|
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message; |
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use Psr\Log\LoggerAwareTrait; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
class Factory |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
use LoggerAwareTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A map of types this factory can build |
16
|
|
|
*/ |
17
|
|
|
private const TYPES = [ |
18
|
|
|
'hello' => Hello::class, |
19
|
|
|
'pong' => Pong::class, |
20
|
|
|
'message' => Message\UserMessage::class, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
private const SHORT_MAP = [ |
24
|
|
|
'ts' => 'timestamp' |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** @var ContainerInterface */ |
28
|
|
|
protected $container; |
29
|
|
|
|
30
|
|
|
public function __construct(ContainerInterface $container, LoggerInterface $logger) |
31
|
|
|
{ |
32
|
|
|
$this->container = $container; |
33
|
|
|
$this->setLogger($logger); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Generate a new Event instance based on message data |
38
|
|
|
* |
39
|
|
|
* @param array $data |
40
|
|
|
* |
41
|
|
|
* @return Event|null |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function buildEvent(array $data) |
44
|
|
|
{ |
45
|
|
|
// If the type isn't set, this likely isn't an event at all |
46
|
|
|
if (!isset($data['type'])) { |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Find the type class |
51
|
|
|
$typeKey = $this->getMapKey($data); |
52
|
|
|
$typeClass = self::TYPES[$typeKey] ?? null; |
53
|
|
|
|
54
|
|
|
// If we can't build it, log and return null |
55
|
|
|
if (!$typeClass) { |
56
|
|
|
$this->logger->debug(sprintf('[<bold>RTM.FTY</bold>] Missing event class for type "%s" "%s"', $typeKey, $typeClass)); |
|
|
|
|
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Build the event |
61
|
|
|
$event = $this->container->get($typeClass); |
62
|
|
|
unset($data['type'], $data['subtype']); |
63
|
|
|
|
64
|
|
|
if ($event instanceof Pong) { |
65
|
|
|
$event->setData($data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($data as $key => $value) { |
69
|
|
|
$key = self::SHORT_MAP[$key] ?? $key; |
70
|
|
|
$setter = camel_case('set_' . $key); |
|
|
|
|
71
|
|
|
|
72
|
|
|
if (!method_exists($event, $setter)) { |
73
|
|
|
$this->logger->debug(sprintf('[<bold>RTM.FTY</bold>] Missing event setter for "%s" data "%s"', $typeKey, $key)); |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$event->{$setter}($value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $event; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get a key for the type as specified by the data array |
85
|
|
|
* |
86
|
|
|
* @example input ['type' => 'foo', 'subtype' => 'bar'], output 'foo.bar'. If the subtype were omitted output would just be 'foo' |
87
|
|
|
* |
88
|
|
|
* @param array $data |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function getMapKey(array $data) |
93
|
|
|
{ |
94
|
|
|
$type = $data['type'] ?? ''; |
95
|
|
|
$subtype = $data['subtype'] ?? null; |
96
|
|
|
|
97
|
|
|
// Return type.subtype or type |
98
|
|
|
return $type . ($subtype ? '.' . $subtype : ''); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |