Passed
Push — master ( 07e522...542653 )
by Daniel
02:08
created

Message::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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\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
	/**
24
	 * Optional parameter for getting single and multiple emails
25
	 *
26
	 * @var array
27
	 */
28
	protected $params = [];
29
30
	/**
31
	 * Message constructor.
32
	 *
33
	 * @param LaravelGmailClass $client
34
	 */
35
	public function __construct( LaravelGmailClass $client )
36
	{
37
		$this->service = new Google_Service_Gmail( $client );
38
	}
39
40
	/**
41
	 * Returns a collection of Mail instances
42
	 *
43
	 * @param null|string $pageToken
44
	 *
45
	 * @return \Illuminate\Support\Collection
46
	 */
47
	public function all( $pageToken = null )
48
	{
49
		if ( ! is_null( $pageToken ) ) {
50
			$this->add( $pageToken, 'pageToken' );
51
		}
52
53
		$messages = [];
54
		$response = $this->service->users_messages->listUsersMessages( 'me', $this->params );
55
56
		$this->pageToken = $response->getNextPageToken();
57
58
		$allMessages = $response->getMessages();
59
60
		foreach ( $allMessages as $message ) {
61
			$messages[] = new Mail( $message, $this->preload );
62
		}
63
64
		return collect( $messages );
65
	}
66
67
	/**
68
	 * Returns next page if available of messages or an empty collection
69
	 *
70
	 * @return \Illuminate\Support\Collection
71
	 */
72
	public function next()
73
	{
74
		if ( $this->pageToken ) {
75
			return $this->all( $this->pageToken );
76
		} else {
77
			return collect( [] );
78
		}
79
	}
80
81
	/**
82
	 * Returns boolean if the page token variable is null or not
83
	 *
84
	 * @return bool
85
	 */
86
	public function hasNextPage()
87
	{
88
		return !!$this->pageToken;
89
	}
90
91
	/**
92
	 * Limit the messages coming from the query
93
	 *
94
	 * @param int $number
95
	 *
96
	 * @return Message
97
	 */
98
	public function take( $number )
99
	{
100
		$this->params[ 'maxResults' ] = abs( (int) $number );
101
102
		return $this;
103
	}
104
105
	/**
106
	 * @param $id
107
	 *
108
	 * @return Mail
109
	 */
110
	public function get( $id )
111
	{
112
		$message = $this->service->users_messages->get( 'me', $id );
113
114
		return new Mail( $message );
115
	}
116
117
	/**
118
	 * Preload the information on each Mail objects.
119
	 * If is not preload you will have to call the load method from the Mail class
120
	 * @see Mail::load()
121
	 *
122
	 * @return $this
123
	 */
124
	public function preload()
125
	{
126
		$this->preload = true;
127
128
		return $this;
129
	}
130
}
131