1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Neo\EarlyAccess\SubscriptionServices\Repositories\Database; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
use Neo\EarlyAccess\Contracts\Subscription\SubscriptionRepository; |
8
|
|
|
|
9
|
|
|
class EloquentRepository extends Model implements SubscriptionRepository |
10
|
|
|
{ |
11
|
|
|
use SoftDeletes; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
public $timestamps = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $dates = [ |
22
|
|
|
'verified_at', |
23
|
|
|
'subscribed_at', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $guarded = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* EloquentRepository constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $attributes |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $attributes = []) |
37
|
|
|
{ |
38
|
|
|
$this->setTable(config('early-access.services.database.table_name')); |
39
|
|
|
|
40
|
|
|
parent::__construct($attributes); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds a new subscriber to the repository. |
45
|
|
|
* |
46
|
|
|
* @param string $email |
47
|
|
|
* @param string|null $name |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function addSubscriber(string $email, ?string $name): bool |
51
|
|
|
{ |
52
|
|
|
return (bool) static::firstOrCreate( |
53
|
|
|
['email' => $email], |
54
|
|
|
['name' => $name, 'subscribed_at' => now()] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Removes a subscriber. |
60
|
|
|
* |
61
|
|
|
* @param string $email |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function removeSubscriber(string $email): bool |
65
|
|
|
{ |
66
|
|
|
return with(static::byEmail($email)->first(), function (?self $subscriber) { |
67
|
|
|
return $subscriber ? $subscriber->delete() : false; |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Verify the subscriber. |
73
|
|
|
* |
74
|
|
|
* @param string $email |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function verify(string $email): bool |
78
|
|
|
{ |
79
|
|
|
return with(static::byEmail($email)->first(), function (?self $subscriber) { |
80
|
|
|
return $subscriber ? $subscriber->update(['verified_at' => now()]) : false; |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Find subscriber by email. |
86
|
|
|
* |
87
|
|
|
* @param string $email |
88
|
|
|
* @return array|false |
89
|
|
|
*/ |
90
|
|
|
public function findByEmail(string $email) |
91
|
|
|
{ |
92
|
|
|
return with(static::byEmail($email)->first(), function (?self $subscriber) { |
93
|
|
|
return $subscriber ? $subscriber->toArray() : false; |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Query by email. |
99
|
|
|
* |
100
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
101
|
|
|
* @param string $email |
102
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
103
|
|
|
*/ |
104
|
|
|
public function scopeByEmail($query, string $email) |
105
|
|
|
{ |
106
|
|
|
return $query->where('email', $email); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|