Source of file NotifyMiddleware.php
Size: 2,171 Bytes - Last Modified: 2021-12-24T07:06:29+00:00
/var/www/docs.ssmods.com/process/src/src/NotifyMiddleware.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 | <?php namespace MaximeRainville\SilverstripeCliNotify; use Joli\JoliNotif\Notification; use Joli\JoliNotif\NotifierFactory; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\Middleware\HTTPMiddleware; class NotifyMiddleware implements HTTPMiddleware { public function process(HTTPRequest $request, callable $delegate) { if (Director::is_cli() && preg_match('/^dev\//i', $request->getURL()) && !$this->isUnitTest()) { try { $response = $delegate($request); $this->notify($request, $response); return $response; } catch (\Exception $ex) { $this->notifyError($request->getURL()); throw $ex; } } return $delegate($request); } protected function notify(HTTPRequest $request, HTTPResponse $response): void { $notifier = NotifierFactory::create(); $notification = new Notification(); $url = $request->getURL(); $notification->setTitle('Silverstripe CMS'); if ($response->isError()) { $notification ->setBody("ERROR $url") ->setIcon(__DIR__.'/../icons/error.png'); } else { $notification ->setBody("DONE $url") ->setIcon(__DIR__.'/../icons/success.png'); } $notifier->send($notification); } protected function notifyError(string $url) { $notifier = NotifierFactory::create(); $notification = new Notification(); $notification ->setTitle('Silverstripe CMS') ->setBody("ERROR $url") ->setIcon(__DIR__.'/../icons/error.png'); $notifier->send($notification); } /** * Attempts to guess if we are currently running in a Unit test context */ protected function isUnitTest(): bool { if (!isset($_SERVER['SCRIPT_NAME'])) { return false; } $script = $_SERVER['SCRIPT_NAME']; return $script === './tests/cli-script.php'; } } |