mirror of
https://github.com/AzuraCast/AzuraCast.git
synced 2024-06-15 05:36:37 +00:00
Make tests work again
This commit is contained in:
parent
33210b9450
commit
06b7f6adf7
|
@ -1,137 +0,0 @@
|
|||
<?php
|
||||
namespace App\EventHandler;
|
||||
|
||||
use App\Entity;
|
||||
use App\Event\SendWebhooks;
|
||||
use App\Exception;
|
||||
use App\Webhook\Connector;
|
||||
use Monolog\Handler\TestHandler;
|
||||
use Monolog\Logger;
|
||||
use Pimple\Psr11\ServiceLocator;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
class DefaultWebhooks implements EventSubscriberInterface
|
||||
{
|
||||
/** @var Logger */
|
||||
protected $logger;
|
||||
|
||||
/** @var ServiceLocator */
|
||||
protected $connectors;
|
||||
|
||||
public function __construct(Logger $logger, ServiceLocator $connectors)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->connectors = $connectors;
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
if (APP_TESTING_MODE) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
SendWebhooks::NAME => [
|
||||
['dispatchWebhooks', 0],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function dispatchWebhooks(SendWebhooks $event)
|
||||
{
|
||||
// Compile list of connectors for the station. Always dispatch to the local websocket receiver.
|
||||
$connectors = [];
|
||||
|
||||
if ($event->isStandalone()) {
|
||||
$connectors[] = [
|
||||
'type' => 'local',
|
||||
'triggers' => [],
|
||||
'config' => [],
|
||||
];
|
||||
}
|
||||
|
||||
// Assemble list of webhooks for the station
|
||||
$station_webhooks = $event->getStation()->getWebhooks();
|
||||
|
||||
if ($station_webhooks->count() > 0) {
|
||||
foreach($station_webhooks as $webhook) {
|
||||
/** @var Entity\StationWebhook $webhook */
|
||||
if ($webhook->isEnabled()) {
|
||||
$connectors[] = [
|
||||
'type' => $webhook->getType(),
|
||||
'triggers' => $webhook->getTriggers() ?: [],
|
||||
'config' => $webhook->getConfig() ?: [],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->logger->debug('Triggering events: '.implode(', ', $event->getTriggers()));
|
||||
|
||||
// Trigger all appropriate webhooks.
|
||||
foreach($connectors as $connector) {
|
||||
if (!$this->connectors->has($connector['type'])) {
|
||||
$this->logger->error(sprintf('Webhook connector "%s" does not exist; skipping.', $connector['type']));
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var Connector\ConnectorInterface $connector_obj */
|
||||
$connector_obj = $this->connectors->get($connector['type']);
|
||||
|
||||
if ($connector_obj->shouldDispatch($event, (array)$connector['triggers'])) {
|
||||
$this->logger->debug(sprintf('Dispatching connector "%s".', $connector['type']));
|
||||
|
||||
$connector_obj->dispatch($event, (array)$connector['config']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a "test" dispatch of the web hook, regardless of whether it is currently enabled, and
|
||||
* return any logging information this yields.
|
||||
*
|
||||
* @param Entity\Station $station
|
||||
* @param Entity\StationWebhook $webhook
|
||||
* @return TestHandler
|
||||
* @throws Exception
|
||||
*/
|
||||
public function testDispatch(Entity\Station $station, Entity\StationWebhook $webhook)
|
||||
{
|
||||
$webhook_type = $webhook->getType();
|
||||
$webhook_config = $webhook->getConfig();
|
||||
|
||||
if (!$this->connectors->has($webhook_type)) {
|
||||
throw new Exception(sprintf('Webhook connector "%s" does not exist; skipping.', $webhook_type));
|
||||
}
|
||||
|
||||
$handler = new TestHandler(Logger::DEBUG, false);
|
||||
$this->logger->pushHandler($handler);
|
||||
|
||||
/** @var Connector\ConnectorInterface $connector_obj */
|
||||
$connector_obj = $this->connectors->get($webhook_type);
|
||||
|
||||
$np = $station->getNowplaying();
|
||||
|
||||
$event = new SendWebhooks($station, $np);
|
||||
$connector_obj->dispatch($event, $webhook_config);
|
||||
|
||||
$this->logger->popHandler();
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly access a webhook connector of the specified type.
|
||||
*
|
||||
* @param $type
|
||||
* @return Connector\ConnectorInterface
|
||||
*/
|
||||
public function getConnector($type): Connector\ConnectorInterface
|
||||
{
|
||||
if ($this->connectors->has($type)) {
|
||||
return $this->connectors->get($type);
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException('Invalid web hook connector type specified.');
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@ class EnableView
|
|||
{
|
||||
$this->view->addData([
|
||||
'request' => $request,
|
||||
'user' => $request->getAttribute(Request::ATTRIBUTE_USER),
|
||||
]);
|
||||
|
||||
$request = $request->withAttribute(Request::ATTRIBUTE_VIEW, $this->view);
|
||||
|
|
|
@ -45,12 +45,6 @@ class GetCurrentUser
|
|||
$this->customization->setUser($user);
|
||||
$request = $this->customization->init($request);
|
||||
|
||||
$this->dispatcher->addListener(BuildView::NAME, function(BuildView $event) use ($user) {
|
||||
$event->getView()->addData([
|
||||
'user' => $user,
|
||||
]);
|
||||
});
|
||||
|
||||
$request = $request
|
||||
->withAttribute(Request::ATTRIBUTE_USER, $user)
|
||||
->withAttribute('is_logged_in', ($user instanceof Entity\User));
|
||||
|
|
Loading…
Reference in New Issue
Block a user