diff --git a/src/Api/Packages.php b/src/Api/Packages.php index e8978b7..2108ee3 100644 --- a/src/Api/Packages.php +++ b/src/Api/Packages.php @@ -11,6 +11,9 @@ use PrivatePackagist\ApiClient\Api\Packages\Artifacts; use PrivatePackagist\ApiClient\Exception\InvalidArgumentException; +use PrivatePackagist\ApiClient\Payload\ArtifactPackageConfig; +use PrivatePackagist\ApiClient\Payload\CustomPackageConfig; +use PrivatePackagist\ApiClient\Payload\VcsPackageConfig; class Packages extends AbstractApi { @@ -55,23 +58,25 @@ public function show($packageName) return $this->get(sprintf('/packages/%s/', $packageName)); } - public function createVcsPackage($url, $credentialId = null, $type = 'vcs') + public function createVcsPackage($url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null) { - return $this->post('/packages/', ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]); + $data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess); + + return $this->post('/packages/', $data->toParameters()); } - public function createCustomPackage($customJson, $credentialId = null) + public function createCustomPackage($customJson, $credentialId = null, $defaultSubrepositoryAccess = null) { - if (is_array($customJson) || is_object($customJson)) { - $customJson = json_encode($customJson); - } + $data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess); - return $this->post('/packages/', ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]); + return $this->post('/packages/', $data->toParameters()); } - public function createArtifactPackage(array $artifactPackageFileIds) + public function createArtifactPackage(array $artifactPackageFileIds, $defaultSubrepositoryAccess = null) { - return $this->post('/packages/', ['repoType' => 'artifact', 'artifactIds' => $artifactPackageFileIds]); + $data = new ArtifactPackageConfig($artifactPackageFileIds, $defaultSubrepositoryAccess); + + return $this->post('/packages/', $data->toParameters()); } /** @@ -82,14 +87,18 @@ public function updateVcsPackage($packageName, $url, $credentialId = null) return $this->editVcsPackage($packageName, $url, $credentialId); } - public function editVcsPackage($packageName, $url, $credentialId = null, $type = 'vcs') + public function editVcsPackage($packageName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null) { - return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]); + $data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess); + + return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters()); } - public function editArtifactPackage($packageName, array $artifactPackageFileIds) + public function editArtifactPackage($packageName, array $artifactPackageFileIds, $defaultSubrepositoryAccess = null) { - return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'artifact', 'artifactIds' => $artifactPackageFileIds]); + $data = new ArtifactPackageConfig($artifactPackageFileIds, $defaultSubrepositoryAccess); + + return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters()); } /** @@ -97,12 +106,14 @@ public function editArtifactPackage($packageName, array $artifactPackageFileIds) */ public function updateCustomPackage($packageName, $customJson, $credentialId = null) { - return $this->editVcsPackage($packageName, $customJson, $credentialId); + return $this->editCustomPackage($packageName, $customJson, $credentialId); } - public function editCustomPackage($packageName, $customJson, $credentialId = null) + public function editCustomPackage($packageName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null) { - return $this->put(sprintf('/packages/%s/', $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]); + $data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess); + + return $this->put(sprintf('/packages/%s/', $packageName), $data->toParameters()); } public function remove($packageName) diff --git a/src/Api/Subrepositories/Packages.php b/src/Api/Subrepositories/Packages.php index c8480c0..b4bd36b 100644 --- a/src/Api/Subrepositories/Packages.php +++ b/src/Api/Subrepositories/Packages.php @@ -11,6 +11,8 @@ use PrivatePackagist\ApiClient\Api\AbstractApi; use PrivatePackagist\ApiClient\Exception\InvalidArgumentException; +use PrivatePackagist\ApiClient\Payload\CustomPackageConfig; +use PrivatePackagist\ApiClient\Payload\VcsPackageConfig; class Packages extends AbstractApi { @@ -28,28 +30,32 @@ public function show($subrepositoryName, $packageName) return $this->get(sprintf('/subrepositories/%s/packages/%s', $subrepositoryName, $packageName)); } - public function createVcsPackage($subrepositoryName, $url, $credentialId = null, $type = 'vcs') + public function createVcsPackage($subrepositoryName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null) { - return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]); + $data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess); + + return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), $data->toParameters()); } - public function createCustomPackage($subrepositoryName, $customJson, $credentialId = null) + public function createCustomPackage($subrepositoryName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null) { - if (is_array($customJson) || is_object($customJson)) { - $customJson = json_encode($customJson); - } + $data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess); - return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]); + return $this->post(sprintf('/subrepositories/%s/packages/', $subrepositoryName), $data->toParameters()); } - public function editVcsPackage($subrepositoryName, $packageName, $url, $credentialId = null, $type = 'vcs') + public function editVcsPackage($subrepositoryName, $packageName, $url, $credentialId = null, $type = 'vcs', $defaultSubrepositoryAccess = null) { - return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), ['repoType' => $type, 'repoUrl' => $url, 'credentials' => $credentialId]); + $data = new VcsPackageConfig($url, $credentialId, $type, $defaultSubrepositoryAccess); + + return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), $data->toParameters()); } - public function editCustomPackage($subrepositoryName, $packageName, $customJson, $credentialId = null) + public function editCustomPackage($subrepositoryName, $packageName, $customJson, $credentialId = null, $defaultSubrepositoryAccess = null) { - return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), ['repoType' => 'package', 'repoConfig' => $customJson, 'credentials' => $credentialId]); + $data = new CustomPackageConfig($customJson, $credentialId, $defaultSubrepositoryAccess); + + return $this->put(sprintf('/subrepositories/%s/packages/%s/', $subrepositoryName, $packageName), $data->toParameters()); } public function remove($subrepositoryName, $packageName) diff --git a/src/Payload/ArtifactPackageConfig.php b/src/Payload/ArtifactPackageConfig.php new file mode 100644 index 0000000..9501685 --- /dev/null +++ b/src/Payload/ArtifactPackageConfig.php @@ -0,0 +1,42 @@ +artifactPackageFileIds = $artifactPackageFileIds; + $this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess; + } + + /** + * @return array{repoType: string, artifactIds: int[], defaultSubrepositoryAccess?: string} + */ + public function toParameters(): array + { + $data = [ + 'repoType' => 'artifact', + 'artifactIds' => $this->artifactPackageFileIds, + ]; + + if ($this->defaultSubrepositoryAccess) { + $data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess; + } + + return $data; + } +} diff --git a/src/Payload/CustomPackageConfig.php b/src/Payload/CustomPackageConfig.php new file mode 100644 index 0000000..0b54886 --- /dev/null +++ b/src/Payload/CustomPackageConfig.php @@ -0,0 +1,51 @@ +customJson = $customJson; + $this->credentialId = $credentialId; + $this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess; + } + + /** + * @return array{repoType: string, repoConfig: string, credentials: ?int, defaultSubrepositoryAccess?: string} + */ + public function toParameters(): array + { + $data = [ + 'repoType' => 'package', + 'repoConfig' => $this->customJson, + 'credentials' => $this->credentialId, + ]; + + if ($this->defaultSubrepositoryAccess) { + $data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess; + } + + return $data; + } +} diff --git a/src/Payload/VcsPackageConfig.php b/src/Payload/VcsPackageConfig.php new file mode 100644 index 0000000..5aa2eb7 --- /dev/null +++ b/src/Payload/VcsPackageConfig.php @@ -0,0 +1,51 @@ +url = $url; + $this->credentialId = $credentialId; + $this->type = $type; + $this->defaultSubrepositoryAccess = $defaultSubrepositoryAccess; + } + + /** + * @return array{repoType: string, repoUrl: string, credentials: ?int, defaultSubrepositoryAccess?: string} + */ + public function toParameters(): array + { + $data = [ + 'repoType' => $this->type, + 'repoUrl' => $this->url, + 'credentials' => $this->credentialId, + ]; + + if ($this->defaultSubrepositoryAccess) { + $data['defaultSubrepositoryAccess'] = $this->defaultSubrepositoryAccess; + } + + return $data; + } +} diff --git a/tests/Api/PackagesTest.php b/tests/Api/PackagesTest.php index 0291c6d..8cc5dac 100644 --- a/tests/Api/PackagesTest.php +++ b/tests/Api/PackagesTest.php @@ -106,10 +106,27 @@ public function testCreateVcsPackage() $this->assertSame($expected, $api->createVcsPackage('localhost')); } + public function testCreateVcsPackageWithDefaultSubrepositoryAccess() + { + $expected = [ + 'id' => 'job-id', + 'status' => 'queued', + ]; + + /** @var Packages&MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'vcs', 'repoUrl' => 'localhost', 'credentials' => null, 'defaultSubrepositoryAccess' => 'no-access'])) + ->willReturn($expected); + + $this->assertSame($expected, $api->createVcsPackage('localhost', null, 'vcs', 'no-access')); + } + /** * @dataProvider customProvider */ - public function testCreateCustomPackage($customJson) + public function testCreateCustomPackage($customJson, $defaultSubrepositoryAccess, array $expectedPayload) { $expected = [ 'id' => 'job-id', @@ -120,13 +137,23 @@ public function testCreateCustomPackage($customJson) $api = $this->getApiMock(); $api->expects($this->once()) ->method('post') - ->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null])) + ->with($this->equalTo('/packages/'), $this->equalTo($expectedPayload)) ->willReturn($expected); - $this->assertSame($expected, $api->createCustomPackage($customJson)); + $this->assertSame($expected, $api->createCustomPackage($customJson, null, $defaultSubrepositoryAccess)); } - public function testCreateArtifactPackage() + public function customProvider() + { + return [ + ['{}', null, ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]], + [new \stdClass(), null, ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null]], + [[], null, ['repoType' => 'package', 'repoConfig' => '[]', 'credentials' => null]], + ['{}', 'no-access', ['repoType' => 'package', 'repoConfig' => '{}', 'credentials' => null, 'defaultSubrepositoryAccess' => 'no-access']], + ]; + } + + public function testCreateArtifactPackageWithDefaultSubrepositoryAccess() { $expected = [ 'id' => 'job-id', @@ -137,18 +164,27 @@ public function testCreateArtifactPackage() $api = $this->getApiMock(); $api->expects($this->once()) ->method('post') - ->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42]])) + ->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42], 'defaultSubrepositoryAccess' => 'no-access'])) ->willReturn($expected); - $this->assertSame($expected, $api->createArtifactPackage([42])); + $this->assertSame($expected, $api->createArtifactPackage([42], 'no-access')); } - public function customProvider() + public function testCreateArtifactPackage() { - return [ - ['{}'], - [new \stdClass()], + $expected = [ + 'id' => 'job-id', + 'status' => 'queued', ]; + + /** @var Packages&MockObject $api */ + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with($this->equalTo('/packages/'), $this->equalTo(['repoType' => 'artifact', 'artifactIds' => [42]])) + ->willReturn($expected); + + $this->assertSame($expected, $api->createArtifactPackage([42])); } public function testEditVcsPackage()