Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Command.php
<?php | <?php | ||||
namespace App\Console; | namespace App\Console; | ||||
use Illuminate\Support\Facades\DB; | |||||
abstract class Command extends \Illuminate\Console\Command | abstract class Command extends \Illuminate\Console\Command | ||||
{ | { | ||||
/** | /** | ||||
* Annotate this command as being dangerous for any potential unintended consequences. | * Annotate this command as being dangerous for any potential unintended consequences. | ||||
* | * | ||||
* Commands are considered dangerous if; | * Commands are considered dangerous if; | ||||
* | * | ||||
* * observers are deliberately not triggered, meaning that the deletion of an object model that requires the | * * observers are deliberately not triggered, meaning that the deletion of an object model that requires the | ||||
Show All 25 Lines | abstract class Command extends \Illuminate\Console\Command | ||||
* @param string $objectClass The name of the class | * @param string $objectClass The name of the class | ||||
* @param string $objectIdOrTitle The name of a database field to match. | * @param string $objectIdOrTitle The name of a database field to match. | ||||
* @param string|null $objectTitle An additional database field to match. | * @param string|null $objectTitle An additional database field to match. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function getObject($objectClass, $objectIdOrTitle, $objectTitle = null) | public function getObject($objectClass, $objectIdOrTitle, $objectTitle = null) | ||||
{ | { | ||||
if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { | $withDeleted = $this->hasOption('with-deleted') && $this->option('with-deleted'); | ||||
$object = $objectClass::withTrashed()->find($objectIdOrTitle); | |||||
} else { | $object = $this->getObjectModel($objectClass, $withDeleted)->find($objectIdOrTitle); | ||||
$object = $objectClass::find($objectIdOrTitle); | |||||
} | |||||
if (!$object && !empty($objectTitle)) { | if (!$object && !empty($objectTitle)) { | ||||
if ($this->hasOption('with-deleted') && $this->option('with-deleted')) { | $object = $this->getObjectModel($objectClass, $withDeleted) | ||||
$object = $objectClass::withTrashed()->where($objectTitle, $objectIdOrTitle)->first(); | ->where($objectTitle, $objectIdOrTitle)->first(); | ||||
} | |||||
return $object; | |||||
} | |||||
/** | |||||
* Returns a preconfigured Model object for a specified class. | |||||
* | |||||
* @param string $objectClass The name of the class | |||||
* @param bool $withDeleted Include withTrashed() query | |||||
* | |||||
* @return mixed | |||||
*/ | |||||
protected function getObjectModel($objectClass, $withDeleted = false) | |||||
{ | |||||
if ($withDeleted) { | |||||
$model = $objectClass::withTrashed(); | |||||
} else { | } else { | ||||
$object = $objectClass::where($objectTitle, $objectIdOrTitle)->first(); | $model = new $objectClass(); | ||||
} | } | ||||
$modelsWithTenant = [ | |||||
\App\Discount::class, | |||||
\App\Domain::class, | |||||
\App\Group::class, | |||||
\App\Package::class, | |||||
\App\Plan::class, | |||||
\App\Sku::class, | |||||
\App\User::class, | |||||
]; | |||||
$modelsWithOwner = [ | |||||
\App\Wallet::class, | |||||
]; | |||||
$tenant_id = \config('app.tenant_id'); | |||||
// Add tenant filter | |||||
if (in_array($objectClass, $modelsWithTenant)) { | |||||
$model = $model->withEnvTenant(); | |||||
} elseif (in_array($objectClass, $modelsWithOwner)) { | |||||
$model = $model->whereExists(function ($query) { | |||||
$query->select(DB::raw(1)) | |||||
->from('users') | |||||
->whereRaw('wallets.user_id = users.id') | |||||
->whereRaw('users.tenant_id ' . ($tenant_id ? "= $tenant_id" : 'is null')); | |||||
}); | |||||
} | } | ||||
return $object; | // TODO: tenant check for Entitlement, Transaction, etc. | ||||
return $model; | |||||
} | } | ||||
/** | /** | ||||
* Find the user. | * Find the user. | ||||
* | * | ||||
* @param string $user User ID or email | * @param string $user User ID or email | ||||
* | * | ||||
* @return \App\User|null | * @return \App\User|null | ||||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |