Completed
Push — master ( 2d3dcc...9dbdcf )
by Daniel
04:02
created

Message::all()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
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 $pageToken
40
	 *
41
	 * @return \Illuminate\Support\Collection
42
	 */
43
	public function all( $pageToken = null )
44
	{
45
		if ( $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 emails from a specific email address
85
	 *
86
	 * @param $email
87
	 *
88
	 * @return $this
89
	 */
90
	public function from( $email )
91
	{
92
		$this->add( "from:{$email}" );
93
94
		return $this;
95
	}
96
97
	/**
98
	 * Filters emails by tag
99
	 * Example:
100
	 * * starred
101
	 * * inbox
102
	 * * spam
103
	 * * chats
104
	 * * sent
105
	 * * draft
106
	 * * trash
107
	 *
108
	 * @param $box
109
	 *
110
	 * @return Message
111
	 */
112
	public function in( $box = 'inbox' )
113
	{
114
		$this->add( "in%3A{$box}" );
115
116
		return $this;
117
	}
118
119
	/**
120
	 * Determines if the email has attachments
121
	 *
122
	 * @return $this
123
	 */
124
	public function hasAttachment()
125
	{
126
		$this->add( 'has%3Aattachment' );
127
128
		return $this;
129
	}
130
131
	/**
132
	 * Preload the information on each Mail objects.
133
	 * If is not preload you will have to call the load method from the Mail class
134
	 * @see Mail::load()
135
	 *
136
	 * @return $this
137
	 */
138
	public function preload()
139
	{
140
		$this->preload = true;
141
142
		return $this;
143
	}
144
}