@@ -53,12 +53,34 @@ public function handle(Command $command): void
5353 $ allActivityIds ->map (fn (ActivityId $ activityId ): string => (string ) $ activityId ),
5454 $ allActivityIds ->toArray (),
5555 );
56- $ stravaActivities = $ this ->strava ->getActivities ();
57- $ countTotalStravaActivities = count ($ stravaActivities );
5856
59- $ command ->getOutput ()->writeln (
60- sprintf ('Status: %d out of %d activities imported ' , count ($ allActivityIds ), $ countTotalStravaActivities )
61- );
57+ try {
58+ if ($ command ->isFullImport ()) {
59+ // No restriction on activity ids, we need to execute a full import.
60+ $ stravaActivities = $ this ->strava ->getActivities ();
61+ } else {
62+ // Restriction on activity ids, we want to execute a partial import.
63+ $ stravaActivities = array_map (
64+ $ this ->strava ->getActivity (...),
65+ $ command ->getRestrictToActivityIds ()->toArray ()
66+ );
67+ }
68+ } catch (StravaRateLimitHasBeenReached $ exception ) {
69+ $ command ->getOutput ()->writeln (sprintf ('<error>%s</error> ' , $ exception ->getMessage ()));
70+
71+ return ;
72+ } catch (ClientException |RequestException $ exception ) {
73+ $ command ->getOutput ()->writeln (sprintf ('<error>Strava API threw error: %s</error> ' , $ exception ->getMessage ()));
74+
75+ return ;
76+ }
77+
78+ $ countTotalStravaActivitiesToImport = count ($ stravaActivities );
79+ if ($ command ->isFullImport ()) {
80+ $ command ->getOutput ()->writeln (
81+ sprintf ('Status: %d out of %d activities imported ' , count ($ allActivityIds ), $ countTotalStravaActivitiesToImport )
82+ );
83+ }
6284
6385 $ delta = 1 ;
6486 foreach ($ stravaActivities as $ rawStravaData ) {
@@ -94,10 +116,11 @@ public function handle(Command $command): void
94116 }
95117
96118 try {
97- $ isNewActivity = false ;
98- if (!$ this ->activityWithRawDataRepository ->exists ($ activityId )) {
119+ $ isNewActivity = !$ this ->activityWithRawDataRepository ->exists ($ activityId );
120+ if ($ isNewActivity && $ command ->isFullImport ()) {
121+ // When a partial import is triggered we fetch the activity from Strava beforehand.
122+ // We only need to fetch activity details when running a full import.
99123 $ rawStravaData = $ this ->strava ->getActivity ($ activityId );
100- $ isNewActivity = true ;
101124 }
102125
103126 $ context = ActivityImportContext::create (
@@ -130,7 +153,7 @@ public function handle(Command $command): void
130153 $ command ->getOutput ()->writeln (sprintf (
131154 ' => [%d/%d] Imported activity: "%s - %s" ' ,
132155 $ delta ,
133- $ countTotalStravaActivities ,
156+ $ countTotalStravaActivitiesToImport ,
134157 $ activity ->getName (),
135158 $ activity ->getStartDate ()->format ('d-m-Y ' ))
136159 );
@@ -151,7 +174,7 @@ public function handle(Command $command): void
151174 $ command ->getOutput ()->writeln (sprintf (
152175 ' => [%d/%d] %s activity: "%s - %s" ' ,
153176 $ delta ,
154- $ countTotalStravaActivities ,
177+ $ countTotalStravaActivitiesToImport ,
155178 $ context ->isNewActivity () ? 'Imported ' : 'Updated ' ,
156179 $ activity ->getName (),
157180 $ activity ->getStartDate ()->format ('d-m-Y ' ))
@@ -166,20 +189,23 @@ public function handle(Command $command): void
166189 return ;
167190 }
168191
169- if (count ($ activityIdsToDelete ) === count ($ allActivityIds ) && array_values ($ activityIdsToDelete ) == $ allActivityIds ->toArray ()) {
170- throw new \RuntimeException ('All activities appear to be marked for deletion. This seems like a configuration issue. Aborting to prevent data loss ' );
171- }
192+ if ($ command ->isFullImport ()) {
193+ if (count ($ activityIdsToDelete ) === count ($ allActivityIds ) && array_values ($ activityIdsToDelete ) == $ allActivityIds ->toArray ()) {
194+ throw new \RuntimeException ('All activities appear to be marked for deletion. This seems like a configuration issue. Aborting to prevent data loss ' );
195+ }
172196
173- foreach ($ activityIdsToDelete as $ activityId ) {
174- $ activity = $ this ->activityRepository ->find ($ activityId );
175- $ activity ->delete ();
176- $ this ->activityRepository ->delete ($ activity );
197+ // Only delete activities during full imports to avoid accidental deletion of data.
198+ foreach ($ activityIdsToDelete as $ activityId ) {
199+ $ activity = $ this ->activityRepository ->find ($ activityId );
200+ $ activity ->delete ();
201+ $ this ->activityRepository ->delete ($ activity );
177202
178- $ command ->getOutput ()->writeln (sprintf (
179- ' => Deleted activity "%s - %s" ' ,
180- $ activity ->getName (),
181- $ activity ->getStartDate ()->format ('d-m-Y ' ))
182- );
203+ $ command ->getOutput ()->writeln (sprintf (
204+ ' => Deleted activity "%s - %s" ' ,
205+ $ activity ->getName (),
206+ $ activity ->getStartDate ()->format ('d-m-Y ' ))
207+ );
208+ }
183209 }
184210 }
185211}
0 commit comments