Completed
Push — master ( 1617d8...e92096 )
by Daniel
10s
created

Message::in()   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\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
	/**
20
	 * Optional parameter for getting single and multiple emails
21
	 *
22
	 * @var array
23
	 */
24
	protected $params = [];
25
26
	/**
27
	 * Message constructor.
28
	 *
29
	 * @param LaravelGmailClass $client
30
	 */
31
	public function __construct( LaravelGmailClass $client )
32
	{
33
		$this->service = new Google_Service_Gmail( $client );
34
	}
35
36
	/**
37
	 * Returns a collection of Mail instances
38
	 *
39
	 * @param null|string $pageToken
40
	 *
41
	 * @return \Illuminate\Support\Collection
42
	 */
43
	public function all( $pageToken = null )
44
	{
45
		if ( ! is_null( $pageToken ) ) {
46
			$this->add( $pageToken, 'pageToken' );
47
		}
48
49
		$messages = [];
50
		$allMessages = $this->service->users_messages->listUsersMessages( 'me', $this->params )->getMessages();
51
52
		foreach ( $allMessages as $message ) {
53
			$messages[] = new Mail( $message, $this->preload );
54
		}
55
56
		return collect( $messages );
57
	}
58
59
	/**
60
	 * Limit the messages coming from the query
61
	 *
62
	 * @param int $number
63
	 *
64
	 * @return Message
65
	 */
66
	public function take( $number )
67
	{
68
		$this->params[ 'maxResults' ] = abs( (int) $number );
69
70
		return $this;
71
	}
72
73
	/**
74
	 * @param $id
75
	 *
76
	 * @return Mail
77
	 */
78
	public function get( $id )
79
	{
80
		$message = $this->service->users_messages->get( 'me', $id );
81
82
		return new Mail( $message );
83
	}
84
85
	/**
86
	 * Filter to get only unread emalis
87
	 *
88
	 * @return $this
89
	 */
90
	public function unread()
91
	{
92
		$this->add( 'is:unread' );
93
94
		return $this;
95
	}
96
97
	/**
98
	 * Filter to get only unread emalis
99
	 *
100
	 * @param $query
101
	 *
102
	 * @return $this
103
	 */
104
	public function subject( $query )
105
	{
106
		$this->add( "[{$query}]" );
107
108
		return $this;
109
	}
110
111
	/**
112
	 * Filter to get only emails from a specific email address
113
	 *
114
	 * @param $email
115
	 *
116
	 * @return $this
117
	 */
118
	public function from( $email )
119
	{
120
		$this->add( "from:{$email}" );
121
122
		return $this;
123
	}
124
125
	/**
126
	 * Filters emails by tag
127
	 * Example:
128
	 * * starred
129
	 * * inbox
130
	 * * spam
131
	 * * chats
132
	 * * sent
133
	 * * draft
134
	 * * trash
135
	 *
136
	 * @param $box
137
	 *
138
	 * @return Message
139
	 */
140
	public function in( $box = 'inbox' )
141
	{
142
		$this->add( "in:{$box}" );
143
144
		return $this;
145
	}
146
147
	/**
148
	 * Determines if the email has attachments
149
	 *
150
	 * @return $this
151
	 */
152
	public function hasAttachment()
153
	{
154
		$this->add( 'has:attachment' );
155
156
		return $this;
157
	}
158
159
	/**
160
	 * Preload the information on each Mail objects.
161
	 * If is not preload you will have to call the load method from the Mail class
162
	 * @see Mail::load()
163
	 *
164
	 * @return $this
165
	 */
166
	public function preload()
167
	{
168
		$this->preload = true;
169
170
		return $this;
171
	}
172
}