michael-rubel /
laravel-couponables
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace MichaelRubel\Couponables\Traits\Concerns; |
||||
| 6 | |||||
| 7 | use Illuminate\Database\Eloquent\Model; |
||||
| 8 | use Illuminate\Support\Collection; |
||||
| 9 | use Illuminate\Support\Str; |
||||
| 10 | use MichaelRubel\Couponables\Models\Contracts\CouponContract; |
||||
| 11 | |||||
| 12 | trait GeneratesCoupons |
||||
| 13 | { |
||||
| 14 | /** |
||||
| 15 | * Generate the coupon codes. |
||||
| 16 | * |
||||
| 17 | * @param int $times |
||||
| 18 | * @param int $length |
||||
| 19 | * @param array $attributes |
||||
| 20 | * |
||||
| 21 | * @return Collection |
||||
| 22 | */ |
||||
| 23 | 1 | public function generateCoupons(int $times = 5, int $length = 7, array $attributes = []): Collection |
|||
| 24 | { |
||||
| 25 | 1 | return Collection::times($times, function () use ($length, $attributes) { |
|||
| 26 | 1 | $fields = collect([ |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 27 | 1 | $this->model->getCodeColumn() => Str::random($length), |
|||
| 28 | 1 | $this->model->getTypeColumn() => CouponContract::TYPE_PERCENTAGE, |
|||
| 29 | 1 | ]); |
|||
| 30 | |||||
| 31 | 1 | $this->model->create($fields->merge($attributes)->toArray()); |
|||
|
0 ignored issues
–
show
$attributes of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::merge().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 32 | 1 | }); |
|||
| 33 | } |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * Generate the coupon code to redeem only by the specified model. |
||||
| 37 | * |
||||
| 38 | * @param Model $redeemer |
||||
| 39 | * @param string $code |
||||
| 40 | * @param array $attributes |
||||
| 41 | * |
||||
| 42 | * @return CouponContract |
||||
| 43 | */ |
||||
| 44 | 1 | public function generateCouponFor(Model $redeemer, string $code, array $attributes = []): CouponContract |
|||
| 45 | { |
||||
| 46 | 1 | $fields = collect([ |
|||
| 47 | 1 | $this->model->getCodeColumn() => $code, |
|||
| 48 | 1 | $this->model->getTypeColumn() => CouponContract::TYPE_PERCENTAGE, |
|||
| 49 | 1 | $this->model->getRedeemerTypeColumn() => $redeemer->getMorphClass(), |
|||
| 50 | 1 | $this->model->getRedeemerIdColumn() => $redeemer->id, |
|||
| 51 | 1 | ]); |
|||
| 52 | |||||
| 53 | 1 | return $this->model->create($fields->merge($attributes)->toArray()); |
|||
|
0 ignored issues
–
show
$attributes of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::merge().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 54 | } |
||||
| 55 | } |
||||
| 56 |