Passed
Push — master ( e92096...389c1a )
by Daniel
02:07
created

Message::next()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Services;
4
5
use Dacastro4\LaravelGmail\LaravelGmailClass;
6
use Dacastro4\LaravelGmail\Services\Message\Mail;
7
use Dacastro4\LaravelGmail\Traits\SendsParameters;
8
use Google_Service_Gmail;
9
10
class Message
11
{
12
13
	use SendsParameters;
14
15
	public $service;
16
17
	public $preload = false;
18
19
	public $pageToken;
20
21
	/**
22
	 * Optional parameter for getting single and multiple emails
23
	 *
24
	 * @var array
25
	 */
26
	protected $params = [];
27
28
	/**
29
	 * Message constructor.
30
	 *
31
	 * @param LaravelGmailClass $client
32
	 */
33
	public function __construct( LaravelGmailClass $client )
34
	{
35
		$this->service = new Google_Service_Gmail( $client );
36
	}
37
38
	/**
39
	 * Returns a collection of Mail instances
40
	 *
41
	 * @param null|string $pageToken
42
	 *
43
	 * @return \Illuminate\Support\Collection
44
	 */
45
	public function all( $pageToken = null )
46
	{
47
		if ( ! is_null( $pageToken ) ) {
48
			$this->add( $pageToken, 'pageToken' );
49
		}
50
51
		$messages = [];
52
		$response = $this->service->users_messages->listUsersMessages( 'me', $this->params );
53
54
		$this->pageToken = $response->getNextPageToken();
55
56
		$allMessages = $response->getMessages();
57
58
		foreach ( $allMessages as $message ) {
59
			$messages[] = new Mail( $message, $this->preload );
60
		}
61
62
		return collect( $messages );
63
	}
64
65
	/**
66
	 * Returns next page if available of messages or an empty collection
67
	 *
68
	 * @return \Illuminate\Support\Collection
69
	 */
70
	public function next()
71
	{
72
		if($this->pageToken) {
73
			return $this->all($this->pageToken);
74
		} else {
75
			return collect([]);
76
		}
77
	}
78
79
	/**
80
	 * Returns boolean if the page token variable is null or not
81
	 *
82
	 * @return bool
83
	 */
84
	public function hasNextPage()
85
	{
86
		return !!$this->pageToken;
87
	}
88
89
	/**
90
	 * Limit the messages coming from the query
91
	 *
92
	 * @param int $number
93
	 *
94
	 * @return Message
95
	 */
96
	public function take( $number )
97
	{
98
		$this->params[ 'maxResults' ] = abs( (int) $number );
99
100
		return $this;
101
	}
102
103
	/**
104
	 * @param $id
105
	 *
106
	 * @return Mail
107
	 */
108
	public function get( $id )
109
	{
110
		$message = $this->service->users_messages->get( 'me', $id );
111
112
		return new Mail( $message );
113
	}
114
115
	/**
116
	 * Filter to get only unread emalis
117
	 *
118
	 * @return $this
119
	 */
120
	public function unread()
121
	{
122
		$this->add( 'is:unread' );
123
124
		return $this;
125
	}
126
127
	/**
128
	 * Filter to get only unread emalis
129
	 *
130
	 * @param $query
131
	 *
132
	 * @return $this
133
	 */
134
	public function subject( $query )
135
	{
136
		$this->add( "[{$query}]" );
137
138
		return $this;
139
	}
140
141
	/**
142
	 * Filter to get only emails from a specific email address
143
	 *
144
	 * @param $email
145
	 *
146
	 * @return $this
147
	 */
148
	public function from( $email )
149
	{
150
		$this->add( "from:{$email}" );
151
152
		return $this;
153
	}
154
155
	/**
156
	 * Filters emails by tag
157
	 * Example:
158
	 * * starred
159
	 * * inbox
160
	 * * spam
161
	 * * chats
162
	 * * sent
163
	 * * draft
164
	 * * trash
165
	 *
166
	 * @param $box
167
	 *
168
	 * @return Message
169
	 */
170
	public function in( $box = 'inbox' )
171
	{
172
		$this->add( "in:{$box}" );
173
174
		return $this;
175
	}
176
177
	/**
178
	 * Determines if the email has attachments
179
	 *
180
	 * @return $this
181
	 */
182
	public function hasAttachment()
183
	{
184
		$this->add( 'has:attachment' );
185
186
		return $this;
187
	}
188
189
	/**
190
	 * Preload the information on each Mail objects.
191
	 * If is not preload you will have to call the load method from the Mail class
192
	 * @see Mail::load()
193
	 *
194
	 * @return $this
195
	 */
196
	public function preload()
197
	{
198
		$this->preload = true;
199
200
		return $this;
201
	}
202
}