Page MenuHomeDevCentral

D617.id1518.diff
No OneTemporary

D617.id1518.diff

diff --git a/app/Console/Commands/NotificationsPayload.php b/app/Console/Commands/NotificationsPayload.php
new file mode 100644
--- /dev/null
+++ b/app/Console/Commands/NotificationsPayload.php
@@ -0,0 +1,143 @@
+<?php
+
+namespace Nasqueron\Notifications\Console\Commands;
+
+use Illuminate\Console\Command;
+
+use InvalidArgumentException;
+use ReflectionClass;
+
+class NotificationsPayload extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'notifications:payload {service} {payload} {args*}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Gets a notification payload from a service payload';
+
+ /**
+ * The service to handle a payload for.
+ *
+ * @var string
+ */
+ private $service;
+
+ /**
+ * The payload.
+ *
+ * @var string
+ */
+ private $payload;
+
+ /**
+ * The parameters to pass to the notifications class constructor.
+ *
+ * An array with arguments' names as keys, arguments' values as values.
+ *
+ * @var array
+ */
+ private $constructor;
+
+ /**
+ * Creates a new command instance.
+ *
+ * @return void
+ */
+ public function __construct() {
+ parent::__construct();
+ }
+
+ /**
+ * Executes the console command.
+ */
+ public function handle() {
+ if ($this->parseArguments()) {
+ $this->printNotification();
+ }
+ }
+
+ /**
+ * @return bool true if arguments looks good; otherwise, false.
+ */
+ public function parseArguments () {
+ try {
+ $this->parseService();
+ $this->parsePayload();
+ $this->parseConstructorParameters();
+ } catch (InvalidArgumentException $ex) {
+ $this->error($ex->getMessage());
+ return false;
+ }
+
+ return true;
+ }
+
+ private function parseService () {
+ $this->service = $this->argument('service');
+
+ if (!class_exists($this->getNotificationClass())) {
+ throw new InvalidArgumentException("Unknown service: $this->service");
+ }
+ }
+
+ private function parsePayload () {
+ $payloadFile = $this->argument('payload');
+
+ if (!file_exists($payloadFile)) {
+ throw new InvalidArgumentException("File not found: $payloadFile");
+ }
+
+ $this->payload = file_get_contents($payloadFile);
+ }
+
+ /**
+ * Parse all the extra arguments and sets the constructor property
+ * as an array of constructor arguments.
+ */
+ public function parseConstructorParameters () {
+ $keys = $this->getNotificationConstructorParameters();
+
+ $values = $this->argument('args');
+ $values['payload'] = json_decode($this->payload);
+
+ $countKeys = count($keys);
+ $countValues = count($values);
+
+ if ($countKeys != $countValues) {
+ throw new InvalidArgumentException("Number of arguments mismatch: got $countValues but expected $countKeys.");
+ }
+
+ $this->constructor = array_combine($keys, $values);
+ }
+
+ public function printNotification () {
+ $class = $this->getNotificationClass();
+ $args = array_values($this->constructor);
+ $instance = new $class(...$args);
+ echo json_encode($instance);
+ }
+
+ private function getNotificationClass () {
+ $namespace = "Nasqueron\Notifications\Notifications\\";
+ return $namespace . $this->service . "Notification";
+ }
+
+ public function getNotificationConstructorParameters () {
+ $parameters = [];
+
+ $class = new ReflectionClass($this->getNotificationClass());
+ foreach ($class->getConstructor()->getParameters() as $parameter) {
+ $parameters[] = $parameter->getName();
+ }
+
+ return $parameters;
+ }
+}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -15,6 +15,7 @@
protected $commands = [
\Nasqueron\Notifications\Console\Commands\ConfigShow::class,
\Nasqueron\Notifications\Console\Commands\Inspire::class,
+ \Nasqueron\Notifications\Console\Commands\NotificationsPayload::class,
\Nasqueron\Notifications\Console\Commands\PhabricatorProjectsMap::class,
];

File Metadata

Mime Type
text/plain
Expires
Sat, Jun 7, 04:36 (6 h, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2710221
Default Alt Text
D617.id1518.diff (4 KB)

Event Timeline