Passed
Pull Request — master (#87)
by Daniel
02:52
created

Message::preload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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\Filterable;
8
use Dacastro4\LaravelGmail\Traits\SendsParameters;
9
use Google_Service_Gmail;
10
11
class Message
12
{
13
14
	use SendsParameters,
15
		Filterable;
16
17
	public $service;
18
19
	public $preload = false;
20
21
	public $pageToken;
22
23
	public $client;
24
25
	/**
26
	 * Optional parameter for getting single and multiple emails
27
	 *
28
	 * @var array
29
	 */
30
	protected $params = [];
31
32
	/**
33
	 * Message constructor.
34
	 *
35
	 * @param  LaravelGmailClass  $client
36
	 */
37
	public function __construct(LaravelGmailClass $client)
38
	{
39
		$this->client = $client;
40
		$this->service = new Google_Service_Gmail($client);
41
	}
42
43
	/**
44
	 * Returns next page if available of messages or an empty collection
45
	 *
46
	 * @return \Illuminate\Support\Collection
47
	 */
48
	public function next()
49
	{
50
		if ($this->pageToken) {
51
			return $this->all($this->pageToken);
52
		} else {
53
			return collect([]);
54
		}
55
	}
56
57
	/**
58
	 * Returns a collection of Mail instances
59
	 *
60
	 * @param  null|string  $pageToken
61
	 *
62
	 * @return \Illuminate\Support\Collection
63
	 */
64
	public function all($pageToken = null)
65
	{
66
		if (!is_null($pageToken)) {
67
			$this->add($pageToken, 'pageToken');
68
		}
69
70
		$messages = [];
71
		$response = $this->service->users_messages->listUsersMessages('me', $this->params);
72
73
		$this->pageToken = $response->getNextPageToken();
74
75
		$allMessages = $response->getMessages();
76
77
		if(!$this->preload) {
78
			foreach ($allMessages as $message) {
79
				$messages[] = new Mail($message, $this->preload);
80
			}
81
		} else {
82
			$messages = $this->batchRequest($allMessages);
83
		}
84
85
		return collect($messages);
86
	}
87
88
	/**
89
	 * Returns boolean if the page token variable is null or not
90
	 *
91
	 * @return bool
92
	 */
93
	public function hasNextPage()
94
	{
95
		return !!$this->pageToken;
96
	}
97
98
	/**
99
	 * Limit the messages coming from the query
100
	 *
101
	 * @param  int  $number
102
	 *
103
	 * @return Message
104
	 */
105
	public function take($number)
106
	{
107
		$this->params['maxResults'] = abs((int) $number);
108
109
		return $this;
110
	}
111
112
	/**
113
	 * @param $id
114
	 *
115
	 * @return Mail
116
	 */
117
	public function get($id)
118
	{
119
		$message = $this->getRequest($id);
120
121
		return new Mail($message);
122
	}
123
124
	/**
125
	 * Creates a batch request to get all emails in a single call
126
	 *
127
	 * @param $allMessages
128
	 *
129
	 * @return array|null
130
	 */
131
	public function batchRequest($allMessages)
132
	{
133
		$this->client->setUseBatch(true);
134
135
		$batch = $this->service->createBatch();
136
137
		foreach ($allMessages as $key => $message) {
138
			$batch->add($this->getRequest($message->getId()), $key);
0 ignored issues
show
Bug introduced by
$this->getRequest($message->getId()) of type Google_Service_Gmail_Message is incompatible with the type Psr\Http\Message\RequestInterface expected by parameter $request of Google_Http_Batch::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
			$batch->add(/** @scrutinizer ignore-type */ $this->getRequest($message->getId()), $key);
Loading history...
139
		}
140
141
		$messagesBatch = $batch->execute();
142
143
		$messages = [];
144
145
		foreach ($messagesBatch as $message) {
146
			$messages[] = new Mail($message);
147
		}
148
149
		return $messages;
150
	}
151
152
	/**
153
	 * Preload the information on each Mail objects.
154
	 * If is not preload you will have to call the load method from the Mail class
155
	 * @return $this
156
	 * @see Mail::load()
157
	 *
158
	 */
159
	public function preload()
160
	{
161
		$this->preload = true;
162
163
		return $this;
164
	}
165
166
	public function getUser()
167
	{
168
		return $this->client->user();
169
	}
170
171
	/**
172
	 * @param $id
173
	 *
174
	 * @return \Google_Service_Gmail_Message
175
	 */
176
	private function getRequest($id)
177
	{
178
		return $this->service->users_messages->get('me', $id);
179
	}
180
}
181