diff --git a/lib/Core/SubscriptionChange/SubscriptionChangeRequestParser.php b/lib/Core/SubscriptionChange/SubscriptionChangeRequestParser.php index f9aee83..5305f3d 100644 --- a/lib/Core/SubscriptionChange/SubscriptionChangeRequestParser.php +++ b/lib/Core/SubscriptionChange/SubscriptionChangeRequestParser.php @@ -15,14 +15,14 @@ class SubscriptionChangeRequestParser { } /** - * @param string $urlsSubscribed - * @param string $urlsUnsubscribed + * @param array $urlsSubscribed + * @param array $urlsUnsubscribed * * @return SubscriptionChange[] */ - public function createSubscriptionChangeList(string $urlsSubscribed, string $urlsUnsubscribed): array { - $urlsToSubscribe = $this->subscriptionChangeReader->fromString($urlsSubscribed, true); - $urlsToDelete = $this->subscriptionChangeReader->fromString($urlsUnsubscribed, false); + public function createSubscriptionChangeList(array $urlsSubscribed, array $urlsUnsubscribed): array { + $urlsToSubscribe = $this->subscriptionChangeReader->mapToSubscriptionsChanges($urlsSubscribed, true); + $urlsToDelete = $this->subscriptionChangeReader->mapToSubscriptionsChanges($urlsUnsubscribed, false); /** @var \OCA\GPodderSync\Core\SubscriptionChange\SubscriptionChange[] $subscriptionChanges */ return array_merge($urlsToSubscribe, $urlsToDelete); diff --git a/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php b/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php index 70a39f4..588214f 100644 --- a/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php +++ b/lib/Core/SubscriptionChange/SubscriptionChangeSaver.php @@ -37,7 +37,7 @@ class SubscriptionChangeSaver { $this->subscriptionChangeRequestParser = $subscriptionChangeRequestParser; } - public function saveSubscriptionChanges(string $urlsSubscribed, string $urlsUnsubscribed, string $userId): void { + public function saveSubscriptionChanges(array $urlsSubscribed, array $urlsUnsubscribed, string $userId): void { $subscriptionChanges = $this->subscriptionChangeRequestParser->createSubscriptionChangeList($urlsSubscribed, $urlsUnsubscribed); foreach ($subscriptionChanges as $urlChangedSubscriptionStatus) { $subscriptionChangeEntity = new SubscriptionChangeEntity(); diff --git a/lib/Core/SubscriptionChange/SubscriptionChangesReader.php b/lib/Core/SubscriptionChange/SubscriptionChangesReader.php index a6603b0..e63e122 100644 --- a/lib/Core/SubscriptionChange/SubscriptionChangesReader.php +++ b/lib/Core/SubscriptionChange/SubscriptionChangesReader.php @@ -6,19 +6,14 @@ namespace OCA\GPodderSync\Core\SubscriptionChange; class SubscriptionChangesReader { /** - * @param string $raw + * @param array $urls + * @param bool $subscribed * - * @return array|SubscriptionChange[] + * @return SubscriptionChange[] */ - public function fromString(string $raw, bool $subscribed):? array { - $urls = str_replace(["[", "]", " "], "", $raw); - $urlList = explode(",", $urls); - - if ($urlList[0] === "") { - return []; - } + public static function mapToSubscriptionsChanges(array $urls, bool $subscribed): array { $subscriptionChanges = []; - foreach ($urlList as $url) { + foreach ($urls as $url) { $subscriptionChanges[] = new SubscriptionChange($url, $subscribed); } diff --git a/tests/Unit/Core/SubscriptionChange/SubscriptionChangeReaderTest.php b/tests/Unit/Core/SubscriptionChange/SubscriptionChangeReaderTest.php index 7e14dfd..0a47a53 100644 --- a/tests/Unit/Core/SubscriptionChange/SubscriptionChangeReaderTest.php +++ b/tests/Unit/Core/SubscriptionChange/SubscriptionChangeReaderTest.php @@ -7,17 +7,11 @@ use OCA\GPodderSync\Core\SubscriptionChange\SubscriptionChangesReader; use Test\TestCase; class SubscriptionChangeReaderTest extends TestCase { - public function testCreateFromString(): void { - $reader = new SubscriptionChangesReader(); - $subscriptionChange = $reader->fromString('[https://feeds.megaphone.fm/HSW8286374095]', true); - $this->assertCount(1, $subscriptionChange); + public function testMapUrlsToSubscriptionChanges(): void { + $subscriptionChange = SubscriptionChangesReader::mapToSubscriptionsChanges(["https://feeds.megaphone.fm/HSW8286374095", "https://feeds.megaphone.fm/another"], true); + $this->assertCount(2, $subscriptionChange); $this->assertSame("https://feeds.megaphone.fm/HSW8286374095", $subscriptionChange[0]->getUrl()); - } - - public function testCreateFromEmptyString(): void { - $reader = new SubscriptionChangesReader(); - $subscriptionChange = $reader->fromString('[]', true); - $this->assertCount(0, $subscriptionChange); + $this->assertSame("https://feeds.megaphone.fm/another", $subscriptionChange[1]->getUrl()); } } diff --git a/tests/Unit/Core/SubscriptionChange/SubscriptionChangeRequestParserTest.php b/tests/Unit/Core/SubscriptionChange/SubscriptionChangeRequestParserTest.php index ea6150a..49f93b0 100644 --- a/tests/Unit/Core/SubscriptionChange/SubscriptionChangeRequestParserTest.php +++ b/tests/Unit/Core/SubscriptionChange/SubscriptionChangeRequestParserTest.php @@ -13,20 +13,13 @@ class SubscriptionChangeRequestParserTest extends TestCase { new SubscriptionChangesReader(), ); - $subscriptionChanges = $subscriptionChangesParser->createSubscriptionChangeList('[https://feeds.simplecast.com/54nAGcIl]','[]'); - $this->assertCount(1, $subscriptionChanges); - $this->assertSame('https://feeds.simplecast.com/54nAGcIl', $subscriptionChanges[0]->getUrl()); + $subscriptionChanges = $subscriptionChangesParser->createSubscriptionChangeList(["https://feeds.simplecast.com/54nAGcIl", "https://feeds.simplecast.com/another"],["https://i.am-removed/GcIl"]); + $this->assertCount(3, $subscriptionChanges); + $this->assertSame("https://feeds.simplecast.com/54nAGcIl", $subscriptionChanges[0]->getUrl()); + $this->assertSame("https://feeds.simplecast.com/another", $subscriptionChanges[1]->getUrl()); + $this->assertSame("https://i.am-removed/GcIl", $subscriptionChanges[2]->getUrl()); $this->assertTrue($subscriptionChanges[0]->isSubscribed()); + $this->assertFalse($subscriptionChanges[2]->isSubscribed()); } - public function testSubscriptionRequestWithMultipleChangesConvertsToSubscriptionChangeList() { - $subscriptionChangesParser = new SubscriptionChangeRequestParser( - new SubscriptionChangesReader(), - ); - - $subscriptionChanges = $subscriptionChangesParser->createSubscriptionChangeList( - '[https://podcastfeeds.nbcnews.com/dateline-nbc,https://feeds.megaphone.fm/ADL9840290619]', - '[]'); - $this->assertCount(2, $subscriptionChanges); - } }