diff --git a/src/app/Console/Command.php b/src/app/Console/Command.php index 63808a8e..ee3624b5 100644 --- a/src/app/Console/Command.php +++ b/src/app/Console/Command.php @@ -1,112 +1,112 @@ getObject(\App\Domain::class, $domain, 'namespace'); } /** * Find an object. * * @param string $objectClass The name of the class * @param string $objectIdOrTitle The name of a database field to match. * @param string|null $objectTitle An additional database field to match. * * @return mixed */ - public function getObject($objectClass, $objectIdOrTitle, $objectTitle) + public function getObject($objectClass, $objectIdOrTitle, $objectTitle = null) { if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { $object = $objectClass::withTrashed()->find($objectIdOrTitle); } else { $object = $objectClass::find($objectIdOrTitle); } if (!$object && !empty($objectTitle)) { if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { $object = $objectClass::withTrashed()->where($objectTitle, $objectIdOrTitle)->first(); } else { $object = $objectClass::where($objectTitle, $objectIdOrTitle)->first(); } } return $object; } /** * Find the user. * * @param string $user User ID or email * * @return \App\User|null */ public function getUser($user) { return $this->getObject(\App\User::class, $user, 'email'); } /** * Find the wallet. * * @param string $wallet Wallet ID * * @return \App\Wallet|null */ public function getWallet($wallet) { return $this->getObject(\App\Wallet::class, $wallet, null); } /** * Return a string for output, with any additional attributes specified as well. * * @param mixed $entry An object * * @return string */ protected function toString($entry) { /** * Haven't figured out yet, how to test if this command implements an option for additional * attributes. if (!in_array('attr', $this->options())) { return $entry->{$entry->getKeyName()}; } */ $str = [ $entry->{$entry->getKeyName()} ]; foreach ($this->option('attr') as $attr) { if ($attr == $entry->getKeyName()) { $this->warn("Specifying {$attr} is not useful."); continue; } if (!array_key_exists($attr, $entry->toArray())) { $this->error("Attribute {$attr} isn't available"); continue; } if (is_numeric($entry->{$attr})) { $str[] = $entry->{$attr}; } else { $str[] = !empty($entry->{$attr}) ? $entry->{$attr} : "null"; } } return implode(" ", $str); } } diff --git a/src/app/Console/Commands/Discount/ActivateCommand.php b/src/app/Console/Commands/Discount/ActivateCommand.php new file mode 100644 index 00000000..64115dd3 --- /dev/null +++ b/src/app/Console/Commands/Discount/ActivateCommand.php @@ -0,0 +1,53 @@ +getObject(\App\Discount::class, $this->argument('discount')); + + if (!$discount) { + $this->error("No such discount. {$this->argument('discount')}"); + return 1; + } + + $discount->active = true; + $discount->save(); + } +} diff --git a/src/app/Console/Commands/Discount/DeactivateCommand.php b/src/app/Console/Commands/Discount/DeactivateCommand.php new file mode 100644 index 00000000..a5e5f610 --- /dev/null +++ b/src/app/Console/Commands/Discount/DeactivateCommand.php @@ -0,0 +1,50 @@ +getObject(\App\Discount::class, $this->argument('discount')); + + if (!$discount) { + $this->error("No such discount. {$this->argument('discount')}"); + return 1; + } + + $discount->active = false; + $discount->save(); + } +} diff --git a/src/app/Console/Commands/Discount/GetDiscountCommand.php b/src/app/Console/Commands/Discount/GetDiscountCommand.php new file mode 100644 index 00000000..f859b01d --- /dev/null +++ b/src/app/Console/Commands/Discount/GetDiscountCommand.php @@ -0,0 +1,42 @@ +description = "Delete a {$this->objectName}"; $this->signature = sprintf( "%s%s:delete {%s}", $this->commandPrefix ? $this->commandPrefix . ":" : "", $this->objectName, $this->objectName ); $class = new $this->objectClass(); try { foreach (Schema::getColumnListing($class->getTable()) as $column) { if ($column == "id") { continue; } $this->signature .= " {--{$column}=}"; } } catch (\Exception $e) { \Log::error("Could not extract options: {$e->getMessage()}"); } $classes = class_uses_recursive($this->objectClass); parent::__construct(); } public function getProperties() { if (!empty($this->properties)) { return $this->properties; } $class = new $this->objectClass(); $this->properties = []; foreach (Schema::getColumnListing($class->getTable()) as $column) { if ($column == "id") { continue; } if (($value = $this->option($column)) !== null) { $this->properties[$column] = $value; } } return $this->properties; } /** * Execute the console command. * * @return mixed */ public function handle() { $result = parent::handle(); if (!$result) { return 1; } $argument = $this->argument($this->objectName); $object = $this->getObject($this->objectClass, $argument, $this->objectTitle); if (!$object) { $this->error("No such {$this->objectName} {$argument}"); return 1; } - $object->delete(); + if ($this->commandPrefix == 'scalpel') { + $this->objectClass::withoutEvents( + function () use ($object) { + $object->delete(); + } + ); + } } }