Passed
Push — master ( 2a03e0...18afc3 )
by Daniel
02:41
created

Message::getMessagesResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 12
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
	 * @throws \Google_Exception
48
	 */
49
	public function next()
50
	{
51
		if ($this->pageToken) {
52
			return $this->all($this->pageToken);
53
		} else {
54
			return new MessageCollection([], $this);
55
		}
56
	}
57
58
	/**
59
	 * Returns a collection of Mail instances
60
	 *
61
	 * @param null|string $pageToken
62
	 *
63
	 * @return \Illuminate\Support\Collection
64
	 * @throws \Google_Exception
65
	 */
66
	public function all($pageToken = null)
67
	{
68
		if (!is_null($pageToken)) {
69
			$this->add($pageToken, 'pageToken');
70
		}
71
72
		$messages = [];
73
		$response = $this->getMessagesResponse();
74
		$this->pageToken = method_exists( $response, 'getNextPageToken' ) ? $response->getNextPageToken() : null;
75
76
		$allMessages = $response->getMessages();
77
78
		if (!$this->preload) {
79
			foreach ($allMessages as $message) {
80
				$messages[] = new Mail($message, $this->preload);
81
			}
82
		} else {
83
			$messages = $this->batchRequest($allMessages);
84
		}
85
86
		$all = new MessageCollection($messages, $this);
87
88
		return $all;
89
	}
90
91
	/**
92
	 * Returns boolean if the page token variable is null or not
93
	 *
94
	 * @return bool
95
	 */
96
	public function hasNextPage()
97
	{
98
		return !!$this->pageToken;
99
	}
100
101
	/**
102
	 * Limit the messages coming from the queryxw
103
	 *
104
	 * @param  int  $number
105
	 *
106
	 * @return Message
107
	 */
108
	public function take($number)
109
	{
110
		$this->params['maxResults'] = abs((int) $number);
111
112
		return $this;
113
	}
114
115
	/**
116
	 * @param $id
117
	 *
118
	 * @return Mail
119
	 */
120
	public function get($id)
121
	{
122
		$message = $this->getRequest($id);
123
124
		return new Mail($message);
125
	}
126
127
	/**
128
	 * Creates a batch request to get all emails in a single call
129
	 *
130
	 * @param $allMessages
131
	 *
132
	 * @return array|null
133
	 */
134
	public function batchRequest($allMessages)
135
	{
136
		$this->client->setUseBatch(true);
137
138
		$batch = $this->service->createBatch();
139
140
		foreach ($allMessages as $key => $message) {
141
			$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

141
			$batch->add(/** @scrutinizer ignore-type */ $this->getRequest($message->getId()), $key);
Loading history...
142
		}
143
144
		$messagesBatch = $batch->execute();
145
146
		$messages = [];
147
148
		foreach ($messagesBatch as $message) {
149
			$messages[] = new Mail($message);
150
		}
151
152
		return $messages;
153
	}
154
155
	/**
156
	 * Preload the information on each Mail objects.
157
	 * If is not preload you will have to call the load method from the Mail class
158
	 * @return $this
159
	 * @see Mail::load()
160
	 *
161
	 */
162
	public function preload()
163
	{
164
		$this->preload = true;
165
166
		return $this;
167
	}
168
169
	public function getUser()
170
	{
171
		return $this->client->user();
172
	}
173
174
	/**
175
	 * @param $id
176
	 *
177
	 * @return \Google_Service_Gmail_Message
178
	 */
179
	private function getRequest($id)
180
	{
181
		return $this->service->users_messages->get('me', $id);
182
	}
183
184
	/**
185
	 * @return \Google_Service_Gmail_ListMessagesResponse|object
186
	 * @throws \Google_Exception
187
	 */
188
	private function getMessagesResponse()
189
	{
190
//		dd($this->params);
191
		$responseOrRequest = $this->service->users_messages->listUsersMessages( 'me', $this->params );
192
193
		if ( get_class( $responseOrRequest ) === "GuzzleHttp\Psr7\Request" ) {
194
			$response = $this->service->getClient()->execute( $responseOrRequest, 'Google_Service_Gmail_ListMessagesResponse' );
0 ignored issues
show
Bug introduced by
$responseOrRequest of type Google_Service_Gmail_ListMessagesResponse is incompatible with the type Psr\Http\Message\RequestInterface expected by parameter $request of Google_Client::execute(). ( Ignorable by Annotation )

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

194
			$response = $this->service->getClient()->execute( /** @scrutinizer ignore-type */ $responseOrRequest, 'Google_Service_Gmail_ListMessagesResponse' );
Loading history...
195
196
			return $response;
197
		}
198
199
		return $responseOrRequest;
200
	}
201
}
202