diff --git a/src/Api/Monetization/Controller/AcceptedRatePlanController.php b/src/Api/Monetization/Controller/AcceptedRatePlanController.php index e9634c17..b40c89f7 100644 --- a/src/Api/Monetization/Controller/AcceptedRatePlanController.php +++ b/src/Api/Monetization/Controller/AcceptedRatePlanController.php @@ -72,14 +72,6 @@ public function getPaginatedAcceptedRatePlanList(int $limit = null, int $page = return $this->getAcceptedRatePlans($query_params); } - /** - * {@inheritdoc} - */ - public function getAllEligibleRatePlans(): array - { - return $this->getEligibleRatePlan(); - } - /** * {@inheritdoc} */ @@ -156,17 +148,6 @@ abstract protected function buildContextForEntityTransformerInCreate(): array; */ abstract protected function getAcceptedRatePlansEndpoint(): UriInterface; - /** - * Returns the URI for listing rate plans eligible to access. - * - * Gets the API products that a company is eligible to access, including: - * API products for which a company has accepted a rate plan. - * API products that do not have a published rate plan. - * - * @return \Psr\Http\Message\UriInterface - */ - abstract protected function getEligibleRatePlanEndpoint(): UriInterface; - /** * Allows to alter payload before it gets sent to the API. * @@ -203,31 +184,4 @@ private function getAcceptedRatePlans(array $query_params = []): array return $entities; } - - /** - * Helper function for listing eligible rate plans. - * - * @param array $query_params - * Additional query parameters. - * - * @return \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface[] - * - * @psalm-suppress PossiblyNullArrayOffset - id() does not return null here. - */ - private function getEligibleRatePlan(): array - { - $entities = []; - - foreach ($this->getRawList($this->getEligibleRatePlanEndpoint()) as $item) { - /** @var \Apigee\Edge\Entity\EntityInterface $tmp */ - $tmp = $this->getEntitySerializer()->denormalize( - $item, - AcceptedRatePlanInterface::class, - 'json' - ); - $entities[$tmp->id()] = $tmp; - } - - return $entities; - } } diff --git a/src/Api/Monetization/Controller/AcceptedRatePlanControllerInterface.php b/src/Api/Monetization/Controller/AcceptedRatePlanControllerInterface.php index c119e956..4f976761 100644 --- a/src/Api/Monetization/Controller/AcceptedRatePlanControllerInterface.php +++ b/src/Api/Monetization/Controller/AcceptedRatePlanControllerInterface.php @@ -41,13 +41,6 @@ interface AcceptedRatePlanControllerInterface extends EntityControllerInterface, */ public function getAllAcceptedRatePlans(): array; - /** - * Gets all eligible rate plans. - * - * @return \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface[] - */ - public function getAllEligibleRatePlans(): array; - /** * Gets accepted rate plans in the provided range. * diff --git a/src/Api/Monetization/Controller/CompanyEligibleRatePlanController.php b/src/Api/Monetization/Controller/CompanyEligibleRatePlanController.php index fbb60358..b40c421f 100644 --- a/src/Api/Monetization/Controller/CompanyEligibleRatePlanController.php +++ b/src/Api/Monetization/Controller/CompanyEligibleRatePlanController.php @@ -1,7 +1,7 @@ client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/companies/{$this->companyName}/eligible-products"); } + /** + * {@inheritdoc} + */ + protected function getEntityClass(): string + { + return CompanyEligibleRatePlan::class; + } + + /** + * {@inheritdoc} + */ + protected function buildContextForEntityTransformerInCreate(): array + { + $context = []; + $context[EntityNormalizer::MINT_ENTITY_REFERENCE_PROPERTY_VALUES]['developer'] = $this->companyName; + + return $context; + } + /** * {@inheritdoc} */ diff --git a/src/Api/Monetization/Controller/EligibleRatePlanController.php b/src/Api/Monetization/Controller/EligibleRatePlanController.php new file mode 100644 index 00000000..bff33eda --- /dev/null +++ b/src/Api/Monetization/Controller/EligibleRatePlanController.php @@ -0,0 +1,171 @@ +getEligibleRatePlan(); + } + + /** + * {@inheritdoc} + */ + public function acceptRatePlan(RatePlanInterface $ratePlan, DateTimeImmutable $startDate, ?DateTimeImmutable $endDate = null, ?int $quotaTarget = null, ?bool $suppressWarning = null, ?bool $waveTerminationCharge = null): AcceptedRatePlanInterface + { + $rc = new ReflectionClass($this->getEntityClass()); + /** @var \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface $acceptedRatePlan */ + $acceptedRatePlan = $rc->newInstance( + [ + 'ratePlan' => $ratePlan, + 'startDate' => $startDate, + ] + ); + if (null !== $quotaTarget) { + $acceptedRatePlan->setQuotaTarget($quotaTarget); + } + if (null !== $endDate) { + $acceptedRatePlan->setEndDate($endDate); + } + $payload = $this->getEntitySerializer()->serialize($acceptedRatePlan, 'json', $this->buildContextForEntityTransformerInCreate()); + $tmp = json_decode($payload, true); + if (null !== $suppressWarning) { + $tmp['suppressWarning'] = $suppressWarning ? 'true' : 'false'; + } + if (null !== $waveTerminationCharge) { + $tmp['waveTerminationCharge'] = $waveTerminationCharge ? 'true' : 'false'; + } + $payload = json_encode($tmp); + $response = $this->client->post($this->getBaseEndpointUri(), $payload); + $this->getEntitySerializer()->setPropertiesFromResponse($response, $acceptedRatePlan); + + return $acceptedRatePlan; + } + + /** + * {@inheritdoc} + * + * @psalm-suppress PossiblyNullArgument - id is not null in this context. + */ + public function updateSubscription(AcceptedRatePlanInterface $acceptedRatePlan, ?bool $suppressWarning = null, ?bool $waveTerminationCharge = null): void + { + $payload = $this->getEntitySerializer()->serialize($acceptedRatePlan, 'json', $this->buildContextForEntityTransformerInCreate()); + $tmp = json_decode($payload, true); + if (null !== $suppressWarning) { + $tmp['suppressWarning'] = $suppressWarning ? 'true' : 'false'; + } + if (null !== $waveTerminationCharge) { + $tmp['waveTerminationCharge'] = $waveTerminationCharge ? 'true' : 'false'; + } + $this->alterRequestPayload($tmp, $acceptedRatePlan); + $payload = json_encode($tmp); + // Update an existing entity. + $response = $this->client->put($this->getEntityEndpointUri($acceptedRatePlan->id()), $payload); + $this->getEntitySerializer()->setPropertiesFromResponse($response, $acceptedRatePlan); + } + + /** + * Returns the URI for listing rate plans eligible to access. + * + * Gets the API products that a company is eligible to access, including: + * API products for which a company has accepted a rate plan. + * API products that do not have a published rate plan. + * + * @return \Psr\Http\Message\UriInterface + */ + abstract protected function getEligibleRatePlanEndpoint(): UriInterface; + + /** + * Builds context for the entity normalizer. + * + * Allows controllers to add extra metadata to the payload. + * + * @return array + */ + abstract protected function buildContextForEntityTransformerInCreate(): array; + + /** + * Allows to alter payload before it gets sent to the API. + * + * @param array $payload + * API request payload. + */ + protected function alterRequestPayload(array &$payload, AcceptedRatePlanInterface $acceptedRatePlan): void + { + } + + /** + * Helper function for listing eligible rate plans. + * + * @param array $query_params + * Additional query parameters. + * + * @return \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface[] + * + * @psalm-suppress PossiblyNullArrayOffset - id() does not return null here. + */ + private function getEligibleRatePlan(): array + { + $entities = []; + + foreach ($this->getRawList($this->getEligibleRatePlanEndpoint()) as $item) { + /** @var \Apigee\Edge\Entity\EntityInterface $tmp */ + $tmp = $this->getEntitySerializer()->denormalize( + $item, + AcceptedRatePlanInterface::class, + 'json' + ); + $entities[$tmp->id()] = $tmp; + } + + return $entities; + } +} diff --git a/src/Api/Monetization/Controller/EligibleRatePlanControllerInterface.php b/src/Api/Monetization/Controller/EligibleRatePlanControllerInterface.php new file mode 100644 index 00000000..e871133b --- /dev/null +++ b/src/Api/Monetization/Controller/EligibleRatePlanControllerInterface.php @@ -0,0 +1,82 @@ +