22
33namespace AppBundle \Issues \GitHub ;
44
5+ use AppBundle \Repository \Repository ;
56use Github \Api \Issue \Labels ;
67
78/**
@@ -19,80 +20,75 @@ class CachedLabelsApi
1920 */
2021 private $ labelCache = [];
2122
22- /**
23- * @var string
24- */
25- private $ repositoryUsername ;
26-
27- /**
28- * @var string
29- */
30- private $ repositoryName ;
31-
32- public function __construct (Labels $ labelsApi , $ repository )
23+ public function __construct (Labels $ labelsApi )
3324 {
34- list ($ repositoryUsername , $ repositoryName ) = explode ('/ ' , $ repository );
35-
3625 $ this ->labelsApi = $ labelsApi ;
37- $ this ->repositoryUsername = $ repositoryUsername ;
38- $ this ->repositoryName = $ repositoryName ;
3926 }
4027
41- public function getIssueLabels ($ issueNumber )
28+ public function getIssueLabels ($ issueNumber, Repository $ repository )
4229 {
43- if (!isset ($ this ->labelCache [$ issueNumber ])) {
44- $ this ->labelCache [$ issueNumber ] = [];
30+ $ key = $ this ->getCacheKey ($ issueNumber , $ repository );
31+ if (!isset ($ this ->labelCache [$ key ])) {
32+ $ this ->labelCache [$ key ] = [];
4533
4634 $ labelsData = $ this ->labelsApi ->all (
47- $ this -> repositoryUsername ,
48- $ this -> repositoryName ,
35+ $ repository -> getVendor () ,
36+ $ repository -> getName () ,
4937 $ issueNumber
5038 );
5139
5240 // Load labels, keep only the first status label
5341 foreach ($ labelsData as $ labelData ) {
54- $ this ->labelCache [$ issueNumber ][$ labelData ['name ' ]] = true ;
42+ $ this ->labelCache [$ key ][$ labelData ['name ' ]] = true ;
5543 }
5644 }
5745
58- return array_keys ($ this ->labelCache [$ issueNumber ]);
46+ return array_keys ($ this ->labelCache [$ key ]);
5947 }
6048
61- public function addIssueLabel ($ issueNumber , $ label )
49+ public function addIssueLabel ($ issueNumber , $ label, Repository $ repository )
6250 {
63- if (isset ($ this ->labelCache [$ issueNumber ][$ label ])) {
51+ $ key = $ this ->getCacheKey ($ issueNumber , $ repository );
52+
53+ if (isset ($ this ->labelCache [$ key ][$ label ])) {
6454 return ;
6555 }
6656
6757 $ this ->labelsApi ->add (
68- $ this -> repositoryUsername ,
69- $ this -> repositoryName ,
58+ $ repository -> getVendor () ,
59+ $ repository -> getName () ,
7060 $ issueNumber ,
7161 $ label
7262 );
7363
7464 // Update cache if already loaded
75- if (isset ($ this ->labelCache [$ issueNumber ])) {
76- $ this ->labelCache [$ issueNumber ][$ label ] = true ;
65+ if (isset ($ this ->labelCache [$ key ])) {
66+ $ this ->labelCache [$ key ][$ label ] = true ;
7767 }
7868 }
7969
80- public function removeIssueLabel ($ issueNumber , $ label )
70+ public function removeIssueLabel ($ issueNumber , $ label, Repository $ repository )
8171 {
82- if (isset ($ this ->labelCache [$ issueNumber ]) && !isset ($ this ->labelCache [$ issueNumber ][$ label ])) {
72+ $ key = $ this ->getCacheKey ($ issueNumber , $ repository );
73+ if (isset ($ this ->labelCache [$ key ]) && !isset ($ this ->labelCache [$ key ][$ label ])) {
8374 return ;
8475 }
8576
8677 $ this ->labelsApi ->remove (
87- $ this -> repositoryUsername ,
88- $ this -> repositoryName ,
78+ $ repository -> getVendor () ,
79+ $ repository -> getName () ,
8980 $ issueNumber ,
9081 $ label
9182 );
9283
9384 // Update cache if already loaded
94- if (isset ($ this ->labelCache [$ issueNumber ])) {
95- unset($ this ->labelCache [$ issueNumber ][$ label ]);
85+ if (isset ($ this ->labelCache [$ key ])) {
86+ unset($ this ->labelCache [$ key ][$ label ]);
9687 }
9788 }
89+
90+ private function getCacheKey ($ issueNumber , Repository $ repository )
91+ {
92+ return sprintf ('%s_%s_%s ' , $ issueNumber , $ repository ->getVendor (), $ repository ->getName ());
93+ }
9894}
0 commit comments