Test Setup Failed
Push — development ( ac5058...9fa3be )
by Ashutosh
11:12
created

BaseCronController::getSubscriptions()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 23
rs 9.2222
c 0
b 0
f 0
cc 6
nc 32
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Order\Invoice;
7
use App\Model\Order\Order;
8
use App\Model\Product\Subscription;
9
use App\User;
10
use Carbon\Carbon;
11
12
class BaseCronController extends Controller
13
{
14
    public function getUserById($id)
15
    {
16
        $user = User::find($id);
17
18
        return $user;
19
    }
20
21
    public function getOrderById($id)
22
    {
23
        $order = Order::find($id);
24
25
        return $order;
26
    }
27
28
    public function getInvoiceByOrderId($orderid)
29
    {
30
        $order = Order::find($orderid);
31
        $invoice = $order->invoice()->first();
32
33
        return $invoice;
34
    }
35
36
    public function getInvoiceItemByInvoiceId($invoiceid)
37
    {
38
        $invoice = Invoice::find($invoiceid);
39
        $item_id = $invoice->invoiceItem()->first();
40
41
        return $item_id;
42
    }
43
44
    public function getSubscriptions()
45
    {
46
        $sub = [];
47
        if (count($this->get30DaysSubscription())) {
48
            array_push($sub, $this->get30DaysSubscription());
49
        }
50
51
        if (count($this->get15DaysUsers())) {
52
            array_push($sub, $this->get15DaysSubscription());
53
        }
54
55
        if (count($this->get1DaysUsers())) {
56
            array_push($sub, $this->get1DaysSubscription());
57
        }
58
59
        if (count($this->get0DaysUsers())) {
60
            array_push($sub, $this->get0DaysSubscription());
61
        }
62
        if (count($this->getPlus1Users())) {
63
            array_push($sub, $this->getPlus1Subscription());
64
        }
65
66
        return $sub;
67
    }
68
69
    public function get30DaysSubscription()
70
    {
71
        $users = [];
72
        $users = $this->get30DaysExpiryUsers();
73
        if (count($users) > 0) {
74
            return $users[0]['subscription'];
75
        }
76
77
        return $users;
78
    }
79
80
    public function get15DaysUsers()
81
    {
82
        $users = [];
83
        $users = $this->get15DaysExpiryUsers();
84
        if (count($users) > 0) {
85
            return $users[0]['users'];
86
        }
87
88
        return $users;
89
    }
90
91
    public function get1DaysUsers()
92
    {
93
        $users = [];
94
        $users = $this->getOneDayExpiryUsers();
95
        if (count($users) > 0) {
96
            return $users[0]['users'];
97
        }
98
99
        return $users;
100
    }
101
102
    public function get0DaysUsers()
103
    {
104
        $users = [];
105
        $users = $this->getOnDayExpiryUsers();
106
        if (count($users) > 0) {
107
            return $users[0]['users'];
108
        }
109
110
        return $users;
111
    }
112
113
    public function getPlus1Users()
114
    {
115
        $users = [];
116
        $users = $this->getExpiredUsers();
117
        if (count($users) > 0) {
118
            return $users[0]['users'];
119
        }
120
121
        return $users;
122
    }
123
124
    public function get30DaysUsers()
125
    {
126
        $users = $this->get30DaysExpiryUsers();
127
        //dd($users);
128
        if (count($users) > 0) {
129
            return $users[0]['users'];
130
        }
131
132
        return $users;
133
    }
134
135
    public function getExpiredInfo()
136
    {
137
        $yesterday = new Carbon('today');
138
        $tomorrow = new Carbon('+2 days');
139
        $sub = Subscription:: where('ends_at', '!=', '0000-00-00 00:00:00')
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after double colon; 1 found

This check looks for references to static members where there are spaces between the name of the type and the actual member.

An example:

Certificate:: TRIPLEDES_CBC

will actually work, but is not very readable.

Loading history...
140
                ->whereNotNull('ends_at')
141
                ->whereBetween('ends_at', [$yesterday, $tomorrow]);
142
143
        return $sub;
144
    }
145
146
    public function getOnDayExpiryInfo()
147
    {
148
        $yesterday = new Carbon('yesterday');
149
        $tomorrow = new Carbon('tomorrow');
150
        $sub = Subscription::where('ends_at', '!=', '0000-00-00 00:00:00')
151
            ->whereNotNull('ends_at')
152
            ->whereBetween('ends_at', [$yesterday, $tomorrow]);
153
154
        return $sub;
155
    }
156
157
    public function getOneDayExpiryInfo()
158
    {
159
        $yesterday = new Carbon('-2 days');
160
        $today = new Carbon('today');
161
        $sub = Subscription::where('ends_at', '!=', '0000-00-00 00:00:00')
162
                ->whereNotNull('ends_at')
163
                ->whereBetween('ends_at', [$yesterday, $today]);
164
165
        return $sub;
166
    }
167
168
    public function get15DaysExpiryInfo()
169
    {
170
        $plus14days = new Carbon('+14 days');
171
        $plus16days = new Carbon('+16 days');
172
        $sub = Subscription::where('ends_at', '!=', '0000-00-00 00:00:00')
173
            ->whereNotNull('ends_at')
174
            ->whereBetween('ends_at', [$plus14days, $plus16days]);
175
176
        return $sub;
177
    }
178
179
    public function get30DaysExpiryInfo()
180
    {
181
        $plus29days = new Carbon('+29 days');
182
        $plus31days = new Carbon('+31 days');
183
184
        $sub = Subscription::where('ends_at', '!=', '0000-00-00 00:00:00')
185
            ->whereNotNull('ends_at')
186
            ->whereBetween('ends_at', [$plus29days, $plus31days]);
187
188
        return $sub;
189
    }
190
191
    public function mail($user, $end, $product, $order, $sub)
192
    {
193
        //check in the settings
194
        $settings = new \App\Model\Common\Setting();
195
        $setting = $settings->where('id', 1)->first();
196
        $url = url('my-orders');
197
        //template
198
        $templates = new \App\Model\Common\Template();
199
        $temp_id = $setting->subscription_going_to_end;
200
201
        if ($end < date('Y-m-d H:m:i')) {
202
            $temp_id = $setting->subscription_over;
203
        }
204
205
        $template = $templates->where('id', $temp_id)->first();
206
        $from = $setting->email;
207
        $to = $user->email;
208
        $subject = $template->name;
209
        $data = $template->data;
210
        $date = date_create($end);
211
        $end = date_format($date, 'l, F j, Y H:m A');
212
        $replace = ['name' => ucfirst($user->first_name).' '.ucfirst($user->last_name),
213
            'expiry'       => $end,
214
            'product'      => $product,
215
            'number'       => $order->number,
216
            'url'          => $url,
217
        ];
218
        $type = '';
219
        if ($template) {
220
            $type_id = $template->type;
221
            $temp_type = new \App\Model\Common\TemplateType();
222
            $type = $temp_type->where('id', $type_id)->first()->name;
223
        }
224
        $templateController = new \App\Http\Controllers\Common\TemplateController();
225
        $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
226
227
        return $mail;
228
    }
229
}
230