DiscountRepositoryEloquent   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 42
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A model() 0 4 1
A findActive() 0 22 2
1
<?php
2
3
/*
4
 * This file is part of ibrand/discount.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Component\Discount\Repositories\Eloquent;
13
14
use Carbon\Carbon;
15
use iBrand\Component\Discount\Models\Discount;
16
use iBrand\Component\Discount\Repositories\DiscountRepository;
17
use Prettus\Repository\Eloquent\BaseRepository;
18
19
class DiscountRepositoryEloquent extends BaseRepository implements DiscountRepository
20
{
21
    /**
22
     * Specify Model class name.
23
     *
24
     * @return string
25
     */
26 14
    public function model()
27
    {
28 14
        return Discount::class;
29
    }
30
31
    /**
32
     * get active discount.
33
     *
34
     * @param int $isCoupon 0:discount 1:coupon 2:all
35
     *
36
     * @return mixed
37
     */
38 14
    public function findActive($isCoupon = 0)
39
    {
40 14
        $query = $this->model->where('status', 1);
41
42 14
        if (2 != $isCoupon) {
43 14
            $query = $query->where('coupon_based', $isCoupon);
44
        }
45
46
        return $query
47 14
            ->where(function ($query) {
48 14
                $query->whereNull('starts_at')
49 14
                    ->orWhere(function ($query) {
50 14
                        $query->where('starts_at', '<', Carbon::now());
51 14
                    });
52 14
            })
53 14
            ->where(function ($query) {
54 14
                $query->whereNull('ends_at')
55 14
                    ->orWhere(function ($query) {
56 14
                        $query->where('ends_at', '>', Carbon::now());
57 14
                    });
58 14
            })->with('rules', 'actions')->get();
59
    }
60
}
61