@@ -68,26 +68,33 @@ class Api
6868 protected $ options = self ::AUTOMAP_FIELDS ;
6969
7070 /**
71- * Fields .
71+ * Client-side cache of fields. List of fields when loaded, null when nothing is fetched yet .
7272 *
73- * @var array
73+ * @var array|null
7474 */
7575 protected $ fields ;
7676
7777 /**
78- * Priorities .
78+ * Client-side cache of priorities. List of priorities when loaded, null when nothing is fetched yet .
7979 *
80- * @var array
80+ * @var array|null
8181 */
8282 protected $ priorities ;
8383
8484 /**
85- * Statuses .
85+ * Client-side cache of statuses. List of statuses when loaded, null when nothing is fetched yet .
8686 *
87- * @var array
87+ * @var array|null
8888 */
8989 protected $ statuses ;
9090
91+ /**
92+ * Client-side cache of resolutions. List of resolutions when loaded, null when nothing is fetched yet.
93+ *
94+ * @var array|null
95+ */
96+ protected $ resolutions ;
97+
9198 /**
9299 * Create a JIRA API client.
93100 *
@@ -141,12 +148,26 @@ public function getEndpoint()
141148 */
142149 public function setEndpoint ($ url )
143150 {
144- $ this ->fields = array ();
145-
146151 // Remove trailing slash in the url.
147152 $ url = rtrim ($ url , '/ ' );
148153
149- $ this ->endpoint = $ url ;
154+ if ( $ url !== $ this ->endpoint ) {
155+ $ this ->endpoint = $ url ;
156+ $ this ->clearLocalCaches ();
157+ }
158+ }
159+
160+ /**
161+ * Helper method to clear the local caches. Is called when switching endpoints
162+ *
163+ * @return void
164+ */
165+ protected function clearLocalCaches ()
166+ {
167+ $ this ->fields = null ;
168+ $ this ->priorities = null ;
169+ $ this ->statuses = null ;
170+ $ this ->resolutions = null ;
150171 }
151172
152173 /**
@@ -156,13 +177,14 @@ public function setEndpoint($url)
156177 */
157178 public function getFields ()
158179 {
159- if ( !count ($ this ->fields ) ) {
180+ // Fetch fields when the method is called for the first time.
181+ if ( $ this ->fields === null ) {
160182 $ fields = array ();
161- $ _fields = $ this ->api (self ::REQUEST_GET , '/rest/api/2/field ' , array ());
183+ $ result = $ this ->api (self ::REQUEST_GET , '/rest/api/2/field ' , array (), true );
162184
163185 /* set hash key as custom field id */
164- foreach ( $ _fields -> getResult () as $ k => $ v ) {
165- $ fields [$ v ['id ' ]] = $ v ;
186+ foreach ( $ result as $ field ) {
187+ $ fields [$ field ['id ' ]] = $ field ;
166188 }
167189
168190 $ this ->fields = $ fields ;
@@ -456,13 +478,14 @@ public function findVersionByName($project_key, $name)
456478 */
457479 public function getPriorities ()
458480 {
459- if ( !count ($ this ->priorities ) ) {
481+ // Fetch priorities when the method is called for the first time.
482+ if ( $ this ->priorities === null ) {
460483 $ priorities = array ();
461- $ result = $ this ->api (self ::REQUEST_GET , '/rest/api/2/priority ' , array ());
484+ $ result = $ this ->api (self ::REQUEST_GET , '/rest/api/2/priority ' , array (), true );
462485
463486 /* set hash key as custom field id */
464- foreach ( $ result-> getResult () as $ k => $ v ) {
465- $ priorities [$ v ['id ' ]] = $ v ;
487+ foreach ( $ result as $ priority ) {
488+ $ priorities [$ priority ['id ' ]] = $ priority ;
466489 }
467490
468491 $ this ->priorities = $ priorities ;
@@ -478,13 +501,14 @@ public function getPriorities()
478501 */
479502 public function getStatuses ()
480503 {
481- if ( !count ($ this ->statuses ) ) {
504+ // Fetch statuses when the method is called for the first time.
505+ if ( $ this ->statuses === null ) {
482506 $ statuses = array ();
483- $ result = $ this ->api (self ::REQUEST_GET , '/rest/api/2/status ' , array ());
507+ $ result = $ this ->api (self ::REQUEST_GET , '/rest/api/2/status ' , array (), true );
484508
485509 /* set hash key as custom field id */
486- foreach ( $ result-> getResult () as $ k => $ v ) {
487- $ statuses [$ v ['id ' ]] = $ v ;
510+ foreach ( $ result as $ status ) {
511+ $ statuses [$ status ['id ' ]] = $ status ;
488512 }
489513
490514 $ this ->statuses = $ statuses ;
@@ -862,14 +886,24 @@ public function getProjectIssueTypes($project_key)
862886 /**
863887 * Returns a list of all resolutions.
864888 *
865- * @param string $project_key Project key.
866- *
867- * @return array|false
889+ * @return array
868890 * @since 2.0.0
869891 */
870892 public function getResolutions ()
871893 {
872- return $ this ->api (self ::REQUEST_GET , '/rest/api/2/resolution ' , array (), true );
894+ // Fetch resolutions when the method is called for the first time.
895+ if ( $ this ->resolutions === null ) {
896+ $ resolutions = array ();
897+ $ result = $ this ->api (self ::REQUEST_GET , '/rest/api/2/resolution ' , array (), true );
898+
899+ foreach ( $ result as $ resolution ) {
900+ $ resolutions [$ resolution ['id ' ]] = $ resolution ;
901+ }
902+
903+ $ this ->resolutions = $ resolutions ;
904+ }
905+
906+ return $ this ->resolutions ;
873907 }
874908
875909}
0 commit comments