2323 */
2424class ImportService {
2525
26- /** @var resource */
27- private $ source ;
28-
2926 public function __construct (
3027 private CalDavBackend $ backend ,
3128 ) {
@@ -44,18 +41,15 @@ public function import($source, CalendarImpl $calendar, CalendarImportOptions $o
4441 if (!is_resource ($ source )) {
4542 throw new InvalidArgumentException ('Invalid import source must be a file resource ' );
4643 }
47-
48- $ this ->source = $ source ;
49-
5044 switch ($ options ->getFormat ()) {
5145 case 'ical ' :
52- return $ this ->importProcess ($ calendar , $ options , $ this ->importText (...));
46+ return $ this ->importProcess ($ source , $ calendar , $ options , $ this ->importText (...));
5347 break ;
5448 case 'jcal ' :
55- return $ this ->importProcess ($ calendar , $ options , $ this ->importJson (...));
49+ return $ this ->importProcess ($ source , $ calendar , $ options , $ this ->importJson (...));
5650 break ;
5751 case 'xcal ' :
58- return $ this ->importProcess ($ calendar , $ options , $ this ->importXml (...));
52+ return $ this ->importProcess ($ source , $ calendar , $ options , $ this ->importXml (...));
5953 break ;
6054 default :
6155 throw new InvalidArgumentException ('Invalid import format ' );
@@ -65,10 +59,15 @@ public function import($source, CalendarImpl $calendar, CalendarImportOptions $o
6559 /**
6660 * Generates object stream from a text formatted source (ical)
6761 *
62+ * @param resource $source
63+ *
6864 * @return Generator<\Sabre\VObject\Component\VCalendar>
6965 */
70- private function importText (): Generator {
71- $ importer = new TextImporter ($ this ->source );
66+ public function importText ($ source ): Generator {
67+ if (!is_resource ($ source )) {
68+ throw new InvalidArgumentException ('Invalid import source must be a file resource ' );
69+ }
70+ $ importer = new TextImporter ($ source );
7271 $ structure = $ importer ->structure ();
7372 $ sObjectPrefix = $ importer ::OBJECT_PREFIX ;
7473 $ sObjectSuffix = $ importer ::OBJECT_SUFFIX ;
@@ -113,10 +112,15 @@ private function importText(): Generator {
113112 /**
114113 * Generates object stream from a xml formatted source (xcal)
115114 *
115+ * @param resource $source
116+ *
116117 * @return Generator<\Sabre\VObject\Component\VCalendar>
117118 */
118- private function importXml (): Generator {
119- $ importer = new XmlImporter ($ this ->source );
119+ public function importXml ($ source ): Generator {
120+ if (!is_resource ($ source )) {
121+ throw new InvalidArgumentException ('Invalid import source must be a file resource ' );
122+ }
123+ $ importer = new XmlImporter ($ source );
120124 $ structure = $ importer ->structure ();
121125 $ sObjectPrefix = $ importer ::OBJECT_PREFIX ;
122126 $ sObjectSuffix = $ importer ::OBJECT_SUFFIX ;
@@ -155,11 +159,16 @@ private function importXml(): Generator {
155159 /**
156160 * Generates object stream from a json formatted source (jcal)
157161 *
162+ * @param resource $source
163+ *
158164 * @return Generator<\Sabre\VObject\Component\VCalendar>
159165 */
160- private function importJson (): Generator {
166+ public function importJson ($ source ): Generator {
167+ if (!is_resource ($ source )) {
168+ throw new InvalidArgumentException ('Invalid import source must be a file resource ' );
169+ }
161170 /** @var VCALENDAR $importer */
162- $ importer = Reader::readJson ($ this -> source );
171+ $ importer = Reader::readJson ($ source );
163172 // calendar time zones
164173 $ timezones = [];
165174 foreach ($ importer ->VTIMEZONE as $ timezone ) {
@@ -212,17 +221,18 @@ private function findTimeZones(VCalendar $vObject): array {
212221 *
213222 * @since 32.0.0
214223 *
224+ * @param resource $source
215225 * @param CalendarImportOptions $options
216226 * @param callable $generator<CalendarImportOptions>: Generator<\Sabre\VObject\Component\VCalendar>
217227 *
218228 * @return array<string,array<string,string|array<string>>>
219229 */
220- public function importProcess (CalendarImpl $ calendar , CalendarImportOptions $ options , callable $ generator ): array {
230+ public function importProcess ($ source , CalendarImpl $ calendar , CalendarImportOptions $ options , callable $ generator ): array {
221231 $ calendarId = $ calendar ->getKey ();
222232 $ calendarUri = $ calendar ->getUri ();
223233 $ principalUri = $ calendar ->getPrincipalUri ();
224234 $ outcome = [];
225- foreach ($ generator () as $ vObject ) {
235+ foreach ($ generator ($ source ) as $ vObject ) {
226236 $ components = $ vObject ->getBaseComponents ();
227237 // determine if the object has no base component types
228238 if (count ($ components ) === 0 ) {
0 commit comments