1
|
|
|
<?php |
2
|
|
|
namespace App\Http\Controllers; |
3
|
|
|
|
4
|
|
|
use App\Repositories\SubscribeRepository; |
5
|
|
|
use App\Http\Requests\SubscribeRequest; |
6
|
|
|
use App\Subscribe; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Mail\Message; |
9
|
|
|
|
10
|
|
|
class SubscribeController extends Controller |
11
|
|
|
{ |
12
|
|
|
protected $subscribe; |
13
|
|
|
|
14
|
|
|
public function __construct(SubscribeRepository $subscribeRepository) |
15
|
|
|
{ |
16
|
|
|
$this->subscribe = $subscribeRepository; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function index(SubscribeRequest $request) |
20
|
|
|
{ |
21
|
|
|
if (!$this->subscribe->checkSubscriber($request['email'])) |
22
|
|
|
{ |
23
|
|
|
$subscribe = $this->subscribe->sendSubscribe($request->all()); |
24
|
|
|
|
25
|
|
|
$this->sendEmail($subscribe); |
26
|
|
|
} else { |
27
|
|
|
$subscribe = $this->subscribe->getByEmail($request['email']); |
28
|
|
|
|
29
|
|
|
if (!$subscribe->active) { |
|
|
|
|
30
|
|
|
$subscribe->fill([ |
31
|
|
|
'active' => (int)true, |
32
|
|
|
])->save(); |
33
|
|
|
|
34
|
|
|
$this->sendEmail($subscribe); |
35
|
|
|
} else { |
36
|
|
|
return redirect()->back()->withStatus('You already subscribed.'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return back()->withStatus('You are subscribed to Amma!'); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Send email. |
45
|
|
|
* |
46
|
|
|
* @param \App\Subscribe $subscribe |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
private function sendEmail(Subscribe $subscribe) |
50
|
|
|
{ |
51
|
|
|
\Mail::send('email.subscribe', compact('subscribe'), function (Message $message) use ($subscribe) { |
52
|
|
|
$message->to($subscribe->email, sprintf('%s %s', $subscribe->email, $subscribe->token)) |
|
|
|
|
53
|
|
|
->subject("Amma subscribed message!"); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function unscribe(Request $request, $token) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->subscribe->unscribe($token); |
60
|
|
|
|
61
|
|
|
return redirect()->route('home')->withStatus('You have unsubscribed!'); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.