Skip to content

Commit 6e1aba0

Browse files
authored
Merge pull request #305 from dacastro4/codex/update-message.php-with-type-declarations
Refactor message service with typed properties
2 parents 5eb0abd + 9376dc9 commit 6e1aba0

File tree

2 files changed

+31
-56
lines changed

2 files changed

+31
-56
lines changed

src/Services/Message.php

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88
use Dacastro4\LaravelGmail\Traits\SendsParameters;
99
use Google_Service_Gmail;
1010
use Google_Service_Gmail_ListMessagesResponse;
11+
use Google_Service_Gmail_Message;
12+
use Illuminate\Support\Collection;
1113

1214
class Message
1315
{
1416
use Filterable,
1517
SendsParameters;
1618

17-
public $service;
19+
private Google_Service_Gmail $service;
1820

19-
public $preload = false;
21+
private bool $preload = false;
2022

21-
public $pageToken;
23+
private ?string $pageToken = null;
2224

23-
public $client;
25+
private LaravelGmailClass $client;
2426

2527
/**
2628
* Optional parameter for getting single and multiple emails
27-
*
28-
* @var array
2929
*/
30-
protected $params = [];
30+
protected array $params = [];
3131

3232
/**
3333
* Message constructor.
@@ -41,28 +41,21 @@ public function __construct(LaravelGmailClass $client)
4141
/**
4242
* Returns next page if available of messages or an empty collection
4343
*
44-
* @return \Illuminate\Support\Collection
45-
*
4644
* @throws \Google_Exception
4745
*/
48-
public function next()
46+
public function next(): Collection
4947
{
50-
if ($this->pageToken) {
51-
return $this->all($this->pageToken);
52-
} else {
53-
return new MessageCollection([], $this);
54-
}
48+
return $this->pageToken
49+
? $this->all($this->pageToken)
50+
: new MessageCollection([], $this);
5551
}
5652

5753
/**
5854
* Returns a collection of Mail instances
5955
*
60-
*
61-
* @return \Illuminate\Support\Collection
62-
*
6356
* @throws \Google_Exception
6457
*/
65-
public function all(?string $pageToken = null)
58+
public function all(?string $pageToken = null): Collection
6659
{
6760
if (! is_null($pageToken)) {
6861
$this->add($pageToken, 'pageToken');
@@ -87,31 +80,28 @@ public function all(?string $pageToken = null)
8780

8881
/**
8982
* Returns boolean if the page token variable is null or not
90-
*
91-
* @return bool
9283
*/
93-
public function hasNextPage()
84+
public function hasNextPage(): bool
9485
{
9586
return (bool) $this->pageToken;
9687
}
9788

89+
public function getPageToken(): ?string
90+
{
91+
return $this->pageToken;
92+
}
93+
9894
/**
9995
* Limit the messages coming from the query
100-
*
101-
* @param int $number
102-
* @return Message
10396
*/
104-
public function take($number)
97+
public function take(int $number): self
10598
{
106-
$this->params['maxResults'] = abs((int) $number);
99+
$this->params['maxResults'] = abs($number);
107100

108101
return $this;
109102
}
110103

111-
/**
112-
* @return Mail
113-
*/
114-
public function get($id)
104+
public function get(string $id): Mail
115105
{
116106
$message = $this->getRequest($id);
117107

@@ -120,11 +110,8 @@ public function get($id)
120110

121111
/**
122112
* Creates a batch request to get all emails in a single call
123-
*
124-
*
125-
* @return array|null
126113
*/
127-
public function batchRequest($allMessages)
114+
public function batchRequest(array $allMessages): array
128115
{
129116
$this->client->setUseBatch(true);
130117

@@ -151,26 +138,21 @@ public function batchRequest($allMessages)
151138
* Preload the information on each Mail objects.
152139
* If is not preload you will have to call the load method from the Mail class
153140
*
154-
* @return $this
155-
*
156141
* @see Mail::load()
157142
*/
158-
public function preload()
143+
public function preload(): self
159144
{
160145
$this->preload = true;
161146

162147
return $this;
163148
}
164149

165-
public function getUser()
150+
public function getUser(): string
166151
{
167152
return $this->client->user();
168153
}
169154

170-
/**
171-
* @return \Google_Service_Gmail_Message
172-
*/
173-
private function getRequest($id)
155+
private function getRequest(string $id): Google_Service_Gmail_Message
174156
{
175157
return $this->service->users_messages->get('me', $id);
176158
}

src/Services/MessageCollection.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
class MessageCollection extends Collection
88
{
9-
/**
10-
* @var Message
11-
*/
12-
private $message;
9+
private ?Message $message;
1310

1411
/**
1512
* MessageCollection constructor.
@@ -22,28 +19,24 @@ public function __construct($items = [], ?Message $message = null)
2219
$this->message = $message;
2320
}
2421

25-
public function next()
22+
public function next(): Collection
2623
{
2724
return $this->message->next();
2825
}
2926

3027
/**
3128
* Returns boolean if the page token variable is null or not
32-
*
33-
* @return bool
3429
*/
35-
public function hasNextPage()
30+
public function hasNextPage(): bool
3631
{
37-
return (bool) $this->message->pageToken;
32+
return $this->message->hasNextPage();
3833
}
3934

4035
/**
4136
* Returns the page token or null
42-
*
43-
* @return string
4437
*/
45-
public function getPageToken()
38+
public function getPageToken(): ?string
4639
{
47-
return $this->message->pageToken;
40+
return $this->message->getPageToken();
4841
}
4942
}

0 commit comments

Comments
 (0)