@@ -55,9 +55,11 @@ public function __construct(
5555 * @return \Generator<Story>
5656 * @throws StoryblokApiException
5757 */
58- public function all (?StoriesParams $ params = null , ?QueryFilters $ filters = null , int $ itemsPerPage = self ::DEFAULT_ITEMS_PER_PAGE ): \Generator
59- {
60-
58+ public function all (
59+ ?StoriesParams $ params = null ,
60+ ?QueryFilters $ filters = null ,
61+ int $ itemsPerPage = self ::DEFAULT_ITEMS_PER_PAGE ,
62+ ): \Generator {
6163 $ totalPages = null ;
6264 $ retryCount = 0 ;
6365 $ page = new PaginationParams (self ::DEFAULT_PAGE , $ itemsPerPage );
@@ -71,7 +73,11 @@ public function all(?StoriesParams $params = null, ?QueryFilters $filters = null
7173 );
7274
7375 if ($ response ->isOk ()) {
74- $ totalPages = $ this ->handleSuccessfulResponse ($ response , $ totalPages , $ itemsPerPage );
76+ $ totalPages = $ this ->handleSuccessfulResponse (
77+ $ response ,
78+ $ totalPages ,
79+ $ itemsPerPage ,
80+ );
7581 yield from $ this ->getStoriesFromResponse ($ response );
7682 $ page ->incrementPage ();
7783 $ retryCount = 0 ;
@@ -80,13 +86,17 @@ public function all(?StoriesParams $params = null, ?QueryFilters $filters = null
8086 ++$ retryCount ;
8187 }
8288 } catch (\Exception $ e ) {
83- $ this ->logger ->error (' Error fetching stories ' , [
84- ' error ' => $ e ->getMessage (),
85- ' page ' => $ page ->page (),
89+ $ this ->logger ->error (" Error fetching stories " , [
90+ " error " => $ e ->getMessage (),
91+ " page " => $ page ->page (),
8692 ]);
87- throw new StoryblokApiException ('Failed to fetch stories: ' . $ e ->getMessage (), 0 , $ e );
93+ throw new StoryblokApiException (
94+ "Failed to fetch stories: " . $ e ->getMessage (),
95+ 0 ,
96+ $ e ,
97+ );
8898 }
89- } while ($ page ->page () <= $ totalPages );
99+ } while ($ totalPages === null || $ page ->page () <= $ totalPages );
90100 }
91101
92102 /**
@@ -104,27 +114,35 @@ public function createStories(array $stories): \Generator
104114 while (true ) {
105115 try {
106116 $ response = $ this ->api ->create ($ storyData );
107- $ this ->logger ->warning ('Story created ' . $ response ->getResponseStatusCode ());
117+ $ this ->logger ->warning (
118+ "Story created " . $ response ->getResponseStatusCode (),
119+ );
108120 yield $ response ->data ();
109121 $ retryCount = 0 ;
110122 break ;
111123 } catch (\Exception $ e ) {
112124 if ($ e ->getCode () === self ::RATE_LIMIT_STATUS_CODE ) {
113125 if ($ retryCount >= self ::MAX_RETRIES ) {
114- $ this ->logger ->error ('Max retries reached while creating story ' , [
115- 'story_name ' => $ storyData ->name (),
116- ]);
126+ $ this ->logger ->error (
127+ "Max retries reached while creating story " ,
128+ [
129+ "story_name " => $ storyData ->name (),
130+ ],
131+ );
117132 throw new StoryblokApiException (
118- ' Rate limit exceeded maximum retries ' ,
133+ " Rate limit exceeded maximum retries " ,
119134 self ::RATE_LIMIT_STATUS_CODE ,
120135 );
121136 }
122137
123- $ this ->logger ->warning ('Rate limit reached while creating story, retrying... ' , [
124- 'retry_count ' => $ retryCount + 1 ,
125- 'max_retries ' => self ::MAX_RETRIES ,
126- 'story_name ' => $ storyData ->name (),
127- ]);
138+ $ this ->logger ->warning (
139+ "Rate limit reached while creating story, retrying... " ,
140+ [
141+ "retry_count " => $ retryCount + 1 ,
142+ "max_retries " => self ::MAX_RETRIES ,
143+ "story_name " => $ storyData ->name (),
144+ ],
145+ );
128146
129147 $ this ->handleRateLimit ();
130148 ++$ retryCount ;
@@ -146,9 +164,8 @@ private function handleSuccessfulResponse(
146164 int $ itemsPerPage ,
147165 ): int {
148166 if ($ totalPages === null ) {
149-
150167 $ totalPages = (int ) ceil ($ response ->total () / $ itemsPerPage );
151- $ this ->logger ->info (' Total stories found: ' . $ response ->total ());
168+ $ this ->logger ->info (" Total stories found: " . $ response ->total ());
152169 }
153170
154171 return $ totalPages ;
@@ -159,18 +176,24 @@ private function handleSuccessfulResponse(
159176 *
160177 * @throws StoryblokApiException
161178 */
162- private function handleErrorResponse (StoryblokResponseInterface $ response , int $ retryCount ): void
163- {
164- if ($ response ->getResponseStatusCode () === self ::RATE_LIMIT_STATUS_CODE ) {
179+ private function handleErrorResponse (
180+ StoryblokResponseInterface $ response ,
181+ int $ retryCount ,
182+ ): void {
183+ if (
184+ $ response ->getResponseStatusCode () === self ::RATE_LIMIT_STATUS_CODE
185+ ) {
165186 if ($ retryCount < self ::MAX_RETRIES ) {
166187 $ this ->handleRateLimit ();
167188 } else {
168- throw new StoryblokApiException ('Rate limit exceeded maximum retries ' );
189+ throw new StoryblokApiException (
190+ "Rate limit exceeded maximum retries " ,
191+ );
169192 }
170193 } else {
171- $ this ->logger ->error (' API error ' , [
172- ' status ' => $ response ->getResponseStatusCode (),
173- ' message ' => $ response ->getErrorMessage (),
194+ $ this ->logger ->error (" API error " , [
195+ " status " => $ response ->getResponseStatusCode (),
196+ " message " => $ response ->getErrorMessage (),
174197 ]);
175198 throw new StoryblokApiException ($ response ->getErrorMessage ());
176199 }
@@ -181,7 +204,7 @@ private function handleErrorResponse(StoryblokResponseInterface $response, int $
181204 */
182205 protected function handleRateLimit (): void
183206 {
184- $ this ->logger ->warning (' Rate limit reached, waiting before retry... ' );
207+ $ this ->logger ->warning (" Rate limit reached, waiting before retry... " );
185208 sleep (self ::RETRY_DELAY );
186209 }
187210
@@ -190,8 +213,9 @@ protected function handleRateLimit(): void
190213 *
191214 * @return \Generator<int, Story>
192215 */
193- private function getStoriesFromResponse (StoryblokResponseInterface $ response ): \Generator
194- {
216+ private function getStoriesFromResponse (
217+ StoryblokResponseInterface $ response ,
218+ ): \Generator {
195219 /** @var Stories $stories */
196220 $ stories = $ response ->data ();
197221 foreach ($ stories as $ story ) {
0 commit comments