Passed
Push — master ( b6b2c6...139034 )
by Daniel
02:50
created

Message::subject()   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 9.4285
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
	 * @param $id
61
	 *
62
	 * @return Mail
63
	 */
64
	public function get( $id )
65
	{
66
		$message = $this->service->users_messages->get( 'me', $id );
67
68
		return new Mail( $message );
69
	}
70
71
	/**
72
	 * Filter to get only unread emalis
73
	 *
74
	 * @return $this
75
	 */
76
	public function unread()
77
	{
78
		$this->add( 'is%3Aunread' );
79
80
		return $this;
81
	}
82
83
	/**
84
	 * Filter to get only unread emalis
85
	 *
86
	 * @param $query
87
	 *
88
	 * @return $this
89
	 */
90
	public function subject($query)
91
	{
92
		$this->add( "[{$query}]" );
93
94
		return $this;
95
	}
96
97
	/**
98
	 * Filter to get only emails from a specific email address
99
	 *
100
	 * @param $email
101
	 *
102
	 * @return $this
103
	 */
104
	public function from( $email )
105
	{
106
		$this->add( "from%3A{$email}" );
107
108
		return $this;
109
	}
110
111
	/**
112
	 * Filters emails by tag
113
	 * Example:
114
	 * * starred
115
	 * * inbox
116
	 * * spam
117
	 * * chats
118
	 * * sent
119
	 * * draft
120
	 * * trash
121
	 *
122
	 * @param $box
123
	 *
124
	 * @return Message
125
	 */
126
	public function in( $box = 'inbox' )
127
	{
128
		$this->add( "in%3A{$box}" );
129
130
		return $this;
131
	}
132
133
	/**
134
	 * Determines if the email has attachments
135
	 *
136
	 * @return $this
137
	 */
138
	public function hasAttachment()
139
	{
140
		$this->add( 'has%3Aattachment' );
141
142
		return $this;
143
	}
144
145
	/**
146
	 * Preload the information on each Mail objects.
147
	 * If is not preload you will have to call the load method from the Mail class
148
	 * @see Mail::load()
149
	 *
150
	 * @return $this
151
	 */
152
	public function preload()
153
	{
154
		$this->preload = true;
155
156
		return $this;
157
	}
158
}