myint-oo /
laravel-gmail
| 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 | 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 string|null $pageToken |
||||
| 62 | * |
||||
| 63 | * @return \Illuminate\Support\Collection |
||||
| 64 | * @throws \Google_Exception |
||||
| 65 | */ |
||||
| 66 | public function all(string $pageToken = null) |
||||
| 67 | { |
||||
| 68 | if (!is_null($pageToken)) { |
||||
| 69 | $this->add($pageToken, 'pageToken'); |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | $mails = []; |
||||
| 73 | $response = $this->getMessagesResponse(); |
||||
| 74 | $this->pageToken = method_exists($response, 'getNextPageToken') ? $response->getNextPageToken() : null; |
||||
| 75 | |||||
| 76 | $messages = $response->getMessages(); |
||||
| 77 | |||||
| 78 | if (!$this->preload) { |
||||
| 79 | foreach ($messages as $message) { |
||||
| 80 | $mails[] = new Mail($message, $this->preload, $this->client->userId); |
||||
| 81 | } |
||||
| 82 | } else { |
||||
| 83 | $mails = count($messages) > 0 ? $this->batchRequest($messages) : []; |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | return new MessageCollection($mails, $this); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | /** |
||||
| 90 | * Returns boolean if the page token variable is null or not |
||||
| 91 | * |
||||
| 92 | * @return bool |
||||
| 93 | */ |
||||
| 94 | public function hasNextPage() |
||||
| 95 | { |
||||
| 96 | return !!$this->pageToken; |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | /** |
||||
| 100 | * Limit the messages coming from the queryxw |
||||
| 101 | * |
||||
| 102 | * @param int $number |
||||
| 103 | * |
||||
| 104 | * @return Message |
||||
| 105 | */ |
||||
| 106 | public function take($number) |
||||
| 107 | { |
||||
| 108 | $this->params['maxResults'] = abs((int)$number); |
||||
| 109 | |||||
| 110 | return $this; |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | /** |
||||
| 114 | * @param $id |
||||
| 115 | * |
||||
| 116 | * @return Mail |
||||
| 117 | */ |
||||
| 118 | public function get($id) |
||||
| 119 | { |
||||
| 120 | $message = $this->getRequest($id); |
||||
| 121 | |||||
| 122 | return new Mail($message, false, $this->client->userId); |
||||
| 123 | } |
||||
| 124 | |||||
| 125 | /** |
||||
| 126 | * Creates a batch request to get all emails in a single call |
||||
| 127 | * |
||||
| 128 | * @param $allMessages |
||||
| 129 | * |
||||
| 130 | * @return array|null |
||||
| 131 | */ |
||||
| 132 | public function batchRequest($allMessages) |
||||
| 133 | { |
||||
| 134 | $this->client->setUseBatch(true); |
||||
| 135 | |||||
| 136 | $batch = $this->service->createBatch(); |
||||
| 137 | |||||
| 138 | foreach ($allMessages as $key => $message) { |
||||
| 139 | $batch->add($this->getRequest($message->getId()), $key); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 140 | } |
||||
| 141 | |||||
| 142 | $messagesBatch = $batch->execute(); |
||||
| 143 | |||||
| 144 | $this->client->setUseBatch(false); |
||||
| 145 | |||||
| 146 | $messages = []; |
||||
| 147 | |||||
| 148 | foreach ($messagesBatch as $message) { |
||||
| 149 | $messages[] = new Mail($message, false, $this->client->userId); |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | return $messages; |
||||
| 153 | } |
||||
| 154 | |||||
| 155 | /** |
||||
| 156 | * Preload the information on each Mail objects. |
||||
| 157 | * If is not preload you will have to call the load method from the Mail class |
||||
| 158 | * @return $this |
||||
| 159 | * @see Mail::load() |
||||
| 160 | * |
||||
| 161 | */ |
||||
| 162 | public function preload() |
||||
| 163 | { |
||||
| 164 | $this->preload = true; |
||||
| 165 | |||||
| 166 | return $this; |
||||
| 167 | } |
||||
| 168 | |||||
| 169 | public function getUser() |
||||
| 170 | { |
||||
| 171 | return $this->client->user(); |
||||
| 172 | } |
||||
| 173 | |||||
| 174 | /** |
||||
| 175 | * @param $id |
||||
| 176 | * |
||||
| 177 | * @return \Google_Service_Gmail_Message |
||||
| 178 | */ |
||||
| 179 | private function getRequest($id) |
||||
| 180 | { |
||||
| 181 | return $this->service->users_messages->get('me', $id); |
||||
| 182 | } |
||||
| 183 | |||||
| 184 | /** |
||||
| 185 | * @return \Google_Service_Gmail_ListMessagesResponse|object |
||||
| 186 | * @throws \Google_Exception |
||||
| 187 | */ |
||||
| 188 | private function getMessagesResponse() |
||||
| 189 | { |
||||
| 190 | $responseOrRequest = $this->service->users_messages->listUsersMessages('me', $this->params); |
||||
| 191 | |||||
| 192 | if (get_class($responseOrRequest) === "GuzzleHttp\Psr7\Request") { |
||||
| 193 | $response = $this->service->getClient()->execute($responseOrRequest, |
||||
|
0 ignored issues
–
show
$responseOrRequest of type Google\Service\Gmail\ListMessagesResponse is incompatible with the type Psr\Http\Message\RequestInterface expected by parameter $request of Google\Client::execute().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 194 | 'Google_Service_Gmail_ListMessagesResponse'); |
||||
| 195 | |||||
| 196 | return $response; |
||||
| 197 | } |
||||
| 198 | |||||
| 199 | return $responseOrRequest; |
||||
| 200 | } |
||||
| 201 | } |
||||
| 202 |