Changeset View
Changeset View
Standalone View
Standalone View
src/app/Package.php
Show All 28 Lines | class Package extends Model | ||||
public $timestamps = false; | public $timestamps = false; | ||||
protected $fillable = [ | protected $fillable = [ | ||||
'title', | 'title', | ||||
'description', | 'description', | ||||
'discount_rate' | 'discount_rate' | ||||
]; | ]; | ||||
public function assign($object, $user = null) | |||||
machniak: My old method used entitleableClass() to make the code more universal. I.e. the calling code… | |||||
Done Inline ActionsIt should be the inverse, i.e. User->assignPackage($package), to preserve the narrative of a customer taking something off the shelf. vanmeeuwen: It should be the inverse, i.e. `User->assignPackage($package)`, to preserve the narrative of a… | |||||
{ | |||||
// if user == null, $object is a user ;-) | |||||
if ($user === null) { | |||||
$user = $object; | |||||
} | |||||
$entitleable_type = null; | |||||
if ($object instanceof \App\Domain) { | |||||
$entitleable_type = \App\Domain::class; | |||||
} | |||||
if ($object instanceof \App\User) { | |||||
$entitleable_type = \App\User::class; | |||||
} | |||||
$wallet_id = $user->wallets()->get()[0]->id; | |||||
foreach ($this->skus()->get() as $sku) { | |||||
//\Log::debug(var_export($sku, true)); | |||||
for ($i = $sku->pivot->qty; $i > 0; $i--) { | |||||
\App\Entitlement::create( | |||||
[ | |||||
Not Done Inline ActionsI'm pretty sure this will make a new query on every iteration. We should move that out of the foreach loop, so the query is executed only once. machniak: I'm pretty sure this will make a new query on every iteration. We should move that out of the… | |||||
Done Inline ActionsI'll move it out. vanmeeuwen: I'll move it out. | |||||
'owner_id' => $user->id, | |||||
'wallet_id' => $wallet_id, | |||||
'sku_id' => $sku->id, | |||||
'entitleable_id' => $entitleable_type ? $object->id : null, | |||||
'entitleable_type' => $entitleable_type | |||||
] | |||||
); | |||||
} | |||||
} | |||||
} | |||||
public function cost() | public function cost() | ||||
{ | { | ||||
$costs = 0; | $costs = 0; | ||||
foreach ($this->skus as $sku) { | foreach ($this->skus as $sku) { | ||||
$costs += ($sku->pivot->qty - $sku->units_free) * $sku->cost; | $costs += ($sku->pivot->qty - $sku->units_free) * $sku->cost; | ||||
} | } | ||||
Show All 13 Lines |
My old method used entitleableClass() to make the code more universal. I.e. the calling code didn't have to know with with arguments to call the method. I mean, look at SignupController, it calls $package->assign($user), which means it will assing the domain-hosting SKU to a user not the domain. This is a bug.