@@ -34,26 +34,6 @@ public function __construct(
3434 protected ImapQueryBuilder $ query ,
3535 ) {}
3636
37- /**
38- * Execute an IMAP search request.
39- */
40- protected function search (): Collection
41- {
42- // If the query is empty, default to fetching all.
43- if ($ this ->query ->isEmpty ()) {
44- $ this ->query ->all ();
45- }
46-
47- $ response = $ this ->connection ()->search ([
48- $ this ->query ->toImap (),
49- ]);
50-
51- return new Collection (array_map (
52- fn (Atom $ token ) => $ token ->value ,
53- $ response ->tokensAfter (2 )
54- ));
55- }
56-
5737 /**
5838 * Count all available messages matching the current search criteria.
5939 */
@@ -62,81 +42,6 @@ public function count(): int
6242 return $ this ->search ()->count ();
6343 }
6444
65- /**
66- * Fetch a given id collection.
67- */
68- protected function fetch (Collection $ messages ): array
69- {
70- if ($ this ->fetchOrder === 'desc ' ) {
71- $ messages = $ messages ->reverse ();
72- }
73-
74- $ uids = $ messages ->forPage ($ this ->page , $ this ->limit )->toArray ();
75-
76- $ flags = $ this ->fetchFlags ? $ this ->connection ()
77- ->flags ($ uids )
78- ->mapWithKeys (MessageResponseParser::getFlags (...))->all () : [];
79-
80- $ headers = $ this ->fetchHeaders ? $ this ->connection ()
81- ->bodyHeader ($ uids , $ this ->fetchAsUnread )
82- ->mapWithKeys (MessageResponseParser::getBodyHeader (...))->all () : [];
83-
84- $ contents = $ this ->fetchBody ? $ this ->connection ()
85- ->bodyText ($ uids , $ this ->fetchAsUnread )
86- ->mapWithKeys (MessageResponseParser::getBodyText (...))->all () : [];
87-
88- return [
89- 'uids ' => $ uids ,
90- 'flags ' => $ flags ,
91- 'headers ' => $ headers ,
92- 'contents ' => $ contents ,
93- ];
94- }
95-
96- /**
97- * Make a new message from given raw components.
98- */
99- protected function newMessage (int $ uid , array $ flags , string $ headers , string $ contents ): Message
100- {
101- return new Message ($ this ->folder , $ uid , $ flags , $ headers , $ contents );
102- }
103-
104- /**
105- * Process the collection of messages.
106- */
107- protected function process (Collection $ messages ): MessageCollection
108- {
109- if ($ messages ->isNotEmpty ()) {
110- return $ this ->populate ($ messages );
111- }
112-
113- return MessageCollection::make ();
114- }
115-
116- /**
117- * Populate a given id collection and receive a fully fetched message collection.
118- */
119- protected function populate (Collection $ uids ): MessageCollection
120- {
121- $ messages = MessageCollection::make ();
122-
123- $ messages ->total ($ uids ->count ());
124-
125- $ rawMessages = $ this ->fetch ($ uids );
126-
127- foreach ($ rawMessages ['uids ' ] as $ uid ) {
128- $ flags = $ rawMessages ['flags ' ][$ uid ] ?? [];
129- $ headers = $ rawMessages ['headers ' ][$ uid ] ?? '' ;
130- $ contents = $ rawMessages ['contents ' ][$ uid ] ?? '' ;
131-
132- $ messages ->push (
133- $ this ->newMessage ($ uid , $ flags , $ headers , $ contents )
134- );
135- }
136-
137- return $ messages ;
138- }
139-
14045 /**
14146 * Get the first message in the resulting collection.
14247 */
@@ -257,7 +162,7 @@ public function paginate(int $perPage = 5, $page = null, string $pageName = 'pag
257162 public function findOrFail (int $ id , ImapFetchIdentifier $ identifier = ImapFetchIdentifier::Uid): MessageInterface
258163 {
259164 /** @var UntaggedResponse $response */
260- $ response = $ this ->uid ($ id , $ identifier )->firstOrFail ();
165+ $ response = $ this ->id ($ id , $ identifier )->firstOrFail ();
261166
262167 $ uid = $ response ->tokenAt (3 ) // ListData
263168 ->tokenAt (1 ) // Atom
@@ -272,7 +177,7 @@ public function findOrFail(int $id, ImapFetchIdentifier $identifier = ImapFetchI
272177 public function find (int $ id , ImapFetchIdentifier $ identifier = ImapFetchIdentifier::Uid): ?MessageInterface
273178 {
274179 /** @var UntaggedResponse $response */
275- if (! $ response = $ this ->uid ($ id , $ identifier )->first ()) {
180+ if (! $ response = $ this ->id ($ id , $ identifier )->first ()) {
276181 return null ;
277182 }
278183
@@ -299,10 +204,97 @@ public function destroy(array|int $uids, bool $expunge = false): void
299204 }
300205 }
301206
207+ /**
208+ * Process the collection of messages.
209+ */
210+ protected function process (Collection $ messages ): MessageCollection
211+ {
212+ if ($ messages ->isNotEmpty ()) {
213+ return $ this ->populate ($ messages );
214+ }
215+
216+ return MessageCollection::make ();
217+ }
218+
219+ /**
220+ * Populate a given id collection and receive a fully fetched message collection.
221+ */
222+ protected function populate (Collection $ uids ): MessageCollection
223+ {
224+ $ messages = MessageCollection::make ();
225+
226+ $ messages ->total ($ uids ->count ());
227+
228+ $ rawMessages = $ this ->fetch ($ uids );
229+
230+ foreach ($ rawMessages ['uids ' ] as $ uid ) {
231+ $ flags = $ rawMessages ['flags ' ][$ uid ] ?? [];
232+ $ headers = $ rawMessages ['headers ' ][$ uid ] ?? '' ;
233+ $ contents = $ rawMessages ['contents ' ][$ uid ] ?? '' ;
234+
235+ $ messages ->push (
236+ $ this ->newMessage ($ uid , $ flags , $ headers , $ contents )
237+ );
238+ }
239+
240+ return $ messages ;
241+ }
242+
243+ /**
244+ * Fetch a given id collection.
245+ */
246+ protected function fetch (Collection $ messages ): array
247+ {
248+ if ($ this ->fetchOrder === 'desc ' ) {
249+ $ messages = $ messages ->reverse ();
250+ }
251+
252+ $ uids = $ messages ->forPage ($ this ->page , $ this ->limit )->toArray ();
253+
254+ $ flags = $ this ->fetchFlags ? $ this ->connection ()
255+ ->flags ($ uids )
256+ ->mapWithKeys (MessageResponseParser::getFlags (...))->all () : [];
257+
258+ $ headers = $ this ->fetchHeaders ? $ this ->connection ()
259+ ->bodyHeader ($ uids , $ this ->fetchAsUnread )
260+ ->mapWithKeys (MessageResponseParser::getBodyHeader (...))->all () : [];
261+
262+ $ contents = $ this ->fetchBody ? $ this ->connection ()
263+ ->bodyText ($ uids , $ this ->fetchAsUnread )
264+ ->mapWithKeys (MessageResponseParser::getBodyText (...))->all () : [];
265+
266+ return [
267+ 'uids ' => $ uids ,
268+ 'flags ' => $ flags ,
269+ 'headers ' => $ headers ,
270+ 'contents ' => $ contents ,
271+ ];
272+ }
273+
274+ /**
275+ * Execute an IMAP search request.
276+ */
277+ protected function search (): Collection
278+ {
279+ // If the query is empty, default to fetching all.
280+ if ($ this ->query ->isEmpty ()) {
281+ $ this ->query ->all ();
282+ }
283+
284+ $ response = $ this ->connection ()->search ([
285+ $ this ->query ->toImap (),
286+ ]);
287+
288+ return new Collection (array_map (
289+ fn (Atom $ token ) => $ token ->value ,
290+ $ response ->tokensAfter (2 )
291+ ));
292+ }
293+
302294 /**
303295 * Get the UID for the given identifier.
304296 */
305- protected function uid (int $ id , ImapFetchIdentifier $ identifier = ImapFetchIdentifier::Uid): ResponseCollection
297+ protected function id (int $ id , ImapFetchIdentifier $ identifier = ImapFetchIdentifier::Uid): ResponseCollection
306298 {
307299 try {
308300 return $ this ->connection ()->uid ([$ id ], $ identifier );
@@ -323,6 +315,14 @@ protected function uid(int $id, ImapFetchIdentifier $identifier = ImapFetchIdent
323315 }
324316 }
325317
318+ /**
319+ * Make a new message from given raw components.
320+ */
321+ protected function newMessage (int $ uid , array $ flags , string $ headers , string $ contents ): Message
322+ {
323+ return new Message ($ this ->folder , $ uid , $ flags , $ headers , $ contents );
324+ }
325+
326326 /**
327327 * Get the connection instance.
328328 */
0 commit comments