Passed
Push — main ( e83696...37db01 )
by Michael
03:45
created

GeneratesCoupons::generateCouponFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
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
     *
20
     * @return Collection
21
     */
22 1
    public function generateCoupons(int $times = 5, int $length = 7): Collection
23
    {
24 1
        return Collection::times($times, fn () => $this->model->create([
25 1
            $this->model->getCodeColumn() => Str::random($length),
26
        ]));
27
    }
28
29
    /**
30
     * Generate the coupon code to redeem only by the specified model.
31
     *
32
     * @param Model  $redeemer
33
     * @param string $code
34
     * @param array  $attributes
35
     *
36
     * @return CouponContract
37
     */
38 1
    public function generateCouponFor(Model $redeemer, string $code, array $attributes = []): CouponContract
39
    {
40 1
        $fields = collect([
41 1
            $this->model->getCodeColumn()         => $code,
42 1
            $this->model->getRedeemerTypeColumn() => $redeemer->getMorphClass(),
43 1
            $this->model->getRedeemerIdColumn()   => $redeemer->id,
44 1
        ])->merge($attributes)->toArray();
0 ignored issues
show
Bug introduced by
$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 ignore-type  annotation

44
        ])->merge(/** @scrutinizer ignore-type */ $attributes)->toArray();
Loading history...
45
46 1
        return $this->model->create($fields);
47
    }
48
}
49