| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Ddomanskyi\LaravelGmail\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ddomanskyi\LaravelGmail\LaravelGmailClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Ddomanskyi\LaravelGmail\Services\Message\Mail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Ddomanskyi\LaravelGmail\Traits\Filterable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Ddomanskyi\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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		$messagesBatch = $batch->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		$this->client->setUseBatch(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		$messages = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		foreach ($messagesBatch as $message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			$messages[] = new Mail($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 		return $messages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * Preload the information on each Mail objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * If is not preload you will have to call the load method from the Mail class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 * @see Mail::load() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	public function preload() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		$this->preload = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	public function getUser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		return $this->client->user(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	 * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	 * @return \Google_Service_Gmail_Message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	private function getRequest($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		return $this->service->users_messages->get('me', $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	 * @return \Google_Service_Gmail_ListMessagesResponse|object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	 * @throws \Google_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	private function getMessagesResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 		$responseOrRequest = $this->service->users_messages->listUsersMessages( 'me', $this->params ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 		if ( get_class( $responseOrRequest ) === "GuzzleHttp\Psr7\Request" ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 			$response = $this->service->getClient()->execute( $responseOrRequest, 'Google_Service_Gmail_ListMessagesResponse' ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 			return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 		return $responseOrRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 202 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |  |