1 | <?php |
||
17 | class Subscriber implements Arrayable |
||
18 | { |
||
19 | use Notifiable; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $attributes = []; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $exists = false; |
||
30 | |||
31 | /** |
||
32 | * @var \Neo\EarlyAccess\Contracts\Subscription\SubscriptionProvider |
||
33 | */ |
||
34 | protected $subscriber; |
||
35 | |||
36 | /** |
||
37 | * Subscriber constructor. |
||
38 | * |
||
39 | * @param \Neo\EarlyAccess\Contracts\Subscription\SubscriptionProvider $subscriber |
||
40 | */ |
||
41 | public function __construct(SubscriptionProvider $subscriber) |
||
42 | { |
||
43 | $this->subscriber = $subscriber; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Shortcut for instantiating the object. |
||
48 | * |
||
49 | * @param array $attributes |
||
50 | * @return \Neo\EarlyAccess\Subscriber |
||
51 | */ |
||
52 | public static function make(array $attributes = []) |
||
53 | { |
||
54 | /** @var self $instance */ |
||
55 | $instance = resolve(self::class); |
||
56 | $instance->fillAttributes($attributes); |
||
57 | |||
58 | return $instance; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Subscribe the user. |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function subscribe(): bool |
||
67 | { |
||
68 | if ($this->subscribed()) { |
||
69 | return true; |
||
70 | } |
||
71 | |||
72 | $subscribed = $this->subscriber->add($this->email, $this->name); |
||
73 | |||
74 | $this->exists = $subscribed; |
||
75 | |||
76 | $this->subscribed_at = (string) now(); |
||
77 | |||
78 | event(new UserSubscribed($this)); |
||
79 | |||
80 | return $subscribed; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Unsubscribe the user. |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | public function unsubscribe(): bool |
||
89 | { |
||
90 | $unsubscribed = $this->subscriber->remove($this->email); |
||
91 | |||
92 | if ($this->exists && $unsubscribed) { |
||
93 | event(new UserUnsubscribed($this)); |
||
94 | } |
||
95 | |||
96 | $this->exists = $unsubscribed === false; |
||
97 | |||
98 | $this->subscribed_at = null; |
||
99 | |||
100 | return $unsubscribed; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Checks if user is subscribed. |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function subscribed(): bool |
||
109 | { |
||
110 | return $this->exists; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Set the subscription status. |
||
115 | * |
||
116 | * @param bool $subscribed |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function setSubscribed(bool $subscribed = true) |
||
120 | { |
||
121 | $this->exists = $subscribed; |
||
122 | |||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Find a user by email. |
||
128 | * |
||
129 | * @param string|null $email |
||
130 | * @return \Neo\EarlyAccess\Subscriber|null |
||
131 | */ |
||
132 | public function findByEmail(string $email = null): ?self |
||
133 | { |
||
134 | if (! $email and ! $this->email) { |
||
135 | return null; |
||
136 | } |
||
137 | |||
138 | if ($this->subscribed() and ! $email) { |
||
139 | return $this; |
||
140 | } |
||
141 | |||
142 | $subscriber = $this->subscriber->findByEmail($email ?? $this->email); |
||
143 | |||
144 | return $subscriber ? $subscriber->setSubscribed() : null; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Verify the subscriber. |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function verify() |
||
153 | { |
||
154 | $this->verified = $this->subscriber->verify($this->email); |
||
155 | |||
156 | if (! $this->exists and $this->verified) { |
||
157 | $this->exists = true; |
||
158 | } |
||
159 | |||
160 | return $this->verified; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Get the instance as an array. |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | public function toArray() |
||
169 | { |
||
170 | return $this->attributes; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return mixed |
||
175 | */ |
||
176 | public function getKey() |
||
177 | { |
||
178 | return $this->email; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param $name |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public function __get($name) |
||
186 | { |
||
187 | return $this->attributes[$name] ?? null; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param $name |
||
192 | * @param $value |
||
193 | */ |
||
194 | public function __set($name, $value) |
||
200 | |||
201 | public function fillAttributes(array $attributes): self |
||
202 | { |
||
203 | $this->attributes = $attributes; |
||
204 | return $this; |
||
205 | } |
||
206 | } |
||
207 |