1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail\Traits; |
4
|
|
|
|
5
|
|
|
use Dacastro4\LaravelGmail\Services\Message\Mail; |
6
|
|
|
use Google_Service_Gmail; |
7
|
|
|
use Google_Service_Gmail_ModifyMessageRequest; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property Google_Service_Gmail $service |
11
|
|
|
*/ |
12
|
|
|
trait Modifiable |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
//TODO search for a naming convention for all the methods |
16
|
|
|
|
17
|
|
|
use ModifiesLabels { |
18
|
|
|
ModifiesLabels::__construct as private __mlConstruct(); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private $messageRequest; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->__mlConstruct(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Marks emails as "READ". Returns string of message if fail |
30
|
|
|
* |
31
|
|
|
* @return Mail|string |
32
|
|
|
*/ |
33
|
|
|
public function markAsRead() |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
|
|
return $this->removeSingleLabel( 'UNREAD' ); |
37
|
|
|
} catch ( \Exception $e ) { |
38
|
|
|
return "Couldn't mark email as read: {$e->getMessage()}"; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Marks emails as unread |
44
|
|
|
* |
45
|
|
|
* @return Mail|string |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
public function markAsUnread() |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
|
|
return $this->addSingleLabel( 'UNREAD' ); |
52
|
|
|
} catch ( \Exception $e ) { |
53
|
|
|
throw new \Exception( "Couldn't mark email as unread: {$e->getMessage()}" ); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return Mail|string |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function markAsImportant() |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
return $this->addSingleLabel( 'IMPORTANT' ); |
65
|
|
|
} catch ( \Exception $e ) { |
66
|
|
|
throw new \Exception( "Couldn't remove mark email as important.: {$e->getMessage()}" ); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return Mail|string |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
public function markAsNotImportant() |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
return $this->removeSingleLabel( 'IMPORTANT' ); |
78
|
|
|
} catch ( \Exception $e ) { |
79
|
|
|
throw new \Exception( "Couldn't mark email as unread: {$e->getMessage()}" ); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Mail|string |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
|
|
public function star() |
88
|
|
|
{ |
89
|
|
|
try { |
90
|
|
|
return $this->addSingleLabel( 'STARRED' ); |
91
|
|
|
} catch ( \Exception $e ) { |
92
|
|
|
throw new \Exception( "Couldn't remove mark email as important.: {$e->getMessage()}" ); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Mail|string |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
|
|
public function unStart() |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
|
|
return $this->removeSingleLabel( 'STARRED' ); |
104
|
|
|
} catch ( \Exception $e ) { |
105
|
|
|
throw new \Exception( "Couldn't mark email as unread: {$e->getMessage()}" ); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Send the email to the trash |
111
|
|
|
* |
112
|
|
|
* @return \Dacastro4\LaravelGmail\Services\Message\Mail|\Exception |
113
|
|
|
*/ |
114
|
|
|
public function trash() |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
return $this->addSingleLabel( 'TRASH' ); |
118
|
|
|
} catch ( \Exception $e ) { |
119
|
|
|
return new \Exception( "Couldn't mark email as trash: {$e->getMessage()}" ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function untrash() |
124
|
|
|
{ |
125
|
|
|
try { |
126
|
|
|
return $this->removeSingleLabel( 'TRASH' ); |
127
|
|
|
} catch ( \Exception $e ) { |
128
|
|
|
return new \Exception( "Couldn't untrash the email: {$e->getMessage()}" ); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |