Passed
Push — master ( 48742a...8b43cc )
by Daniel
04:18
created

Message::fromThese()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 5
nop 1
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
	 * add an array of from addresses
157
	 *
158
	 * @param $email
159
	 *
160
	 * @return $this
161
	 */
162
	public function fromThese($emails)
163
	{
164
		for ($i=0; $i < count($emails); $i++) 
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
165
			!$i ? $this->add("{from:$emails[$i]") : $i == count($emails) - 1 ? $this->add("from:$emails[$i]}") : $this->from($emails[$i]);
166
167
		return $this;
168
	}
169
170
	/**
171
	 * Filters emails by tag
172
	 * Example:
173
	 * * starred
174
	 * * inbox
175
	 * * spam
176
	 * * chats
177
	 * * sent
178
	 * * draft
179
	 * * trash
180
	 *
181
	 * @param $box
182
	 *
183
	 * @return Message
184
	 */
185
	public function in( $box = 'inbox' )
186
	{
187
		$this->add( "in:{$box}" );
188
189
		return $this;
190
	}
191
192
	/**
193
	 * Determines if the email has attachments
194
	 *
195
	 * @return $this
196
	 */
197
	public function hasAttachment()
198
	{
199
		$this->add( 'has:attachment' );
200
201
		return $this;
202
	}
203
204
	/**
205
	 * Preload the information on each Mail objects.
206
	 * If is not preload you will have to call the load method from the Mail class
207
	 * @see Mail::load()
208
	 *
209
	 * @return $this
210
	 */
211
	public function preload()
212
	{
213
		$this->preload = true;
214
215
		return $this;
216
	}
217
}
218