Completed
Branch feature/job-plans (e266db)
by Adam
05:31
created

ongoingPaymentsWithBoostBenefit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.4285
1
<?php
2
3
namespace Coyote\Repositories\Eloquent;
4
5
use Carbon\Carbon;
6
use Coyote\Payment;
7
use Coyote\Repositories\Contracts\PaymentRepositoryInterface;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Query\JoinClause;
10
11
class PaymentRepository extends Repository implements PaymentRepositoryInterface
12
{
13
    /**
14
     * @return string
15
     */
16
    public function model()
17
    {
18
        return Payment::class;
19
    }
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function hasRecentlyPaid(int $userId, int $days = 7)
25
    {
26
        return $this
27
            ->model
28
            ->join('jobs', function (JoinClause $join) use ($userId) {
29
                return $join->on('jobs.id', '=', 'job_id')->on('user_id', $this->raw($userId));
30
            })
31
            ->where('payments.created_at', '>', Carbon::now()->subDay($days))
32
            ->where('status_id', Payment::PAID)
33
            ->exists();
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function ongoingPaymentsWithBoostBenefit()
40
    {
41
        return $this
42
            ->model
43
            ->select(['days', 'job_id', 'ends_at'])
44
            ->where('ends_at', '>', Carbon::now())
45
            ->with(['job' => function (BelongsTo $builder) {
46
                // shouldn't laravel do this for us? anyway, no deleted offers!
47
                return $builder->whereNull('deleted_at')->where('is_boost', true);
48
            }])
49
            ->get();
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function filter()
56
    {
57
        return $this
58
            ->model
59
            ->select([
60
                'payments.id',
61
                'payments.created_at',
62
                'status_id',
63
                'job_id',
64
                'invoice_id'
65
            ])
66
            ->with(['job', 'invoice']);
67
    }
68
}
69