Skip to content

Commit

Permalink
Solving psalm errors and adding dependent files
Browse files Browse the repository at this point in the history
  • Loading branch information
kedarkhaire committed Dec 28, 2023
1 parent 53c05d8 commit 8ac0603
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 55 deletions.
46 changes: 0 additions & 46 deletions src/Api/Monetization/Controller/AcceptedRatePlanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* Copyright 2018 Google LLC
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,11 +18,13 @@

namespace Apigee\Edge\Api\Monetization\Controller;

use Apigee\Edge\Api\Monetization\Entity\CompanyEligibleRatePlan;
use Apigee\Edge\Api\Monetization\Normalizer\EntityNormalizer;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Serializer\EntitySerializerInterface;
use Psr\Http\Message\UriInterface;

class CompanyEligibleRatePlanController extends AcceptedRatePlanController
class CompanyEligibleRatePlanController extends EligibleRatePlanController
{
/**
* Name of the company.
Expand Down Expand Up @@ -53,6 +55,25 @@ protected function getBaseEndpointUri(): UriInterface
return $this->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}
*/
Expand Down
171 changes: 171 additions & 0 deletions src/Api/Monetization/Controller/EligibleRatePlanController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\Monetization\Controller;

use Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface;
use Apigee\Edge\Api\Monetization\Entity\RatePlanInterface;
use Apigee\Edge\Api\Monetization\Serializer\AcceptedRatePlanSerializer;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Controller\EntityListingControllerTrait;
use Apigee\Edge\Controller\EntityLoadOperationControllerTrait;
use Apigee\Edge\Serializer\EntitySerializerInterface;
use DateTimeImmutable;
use Psr\Http\Message\UriInterface;
use ReflectionClass;

abstract class EligibleRatePlanController extends OrganizationAwareEntityController implements EligibleRatePlanControllerInterface
{
use EntityListingControllerTrait;
use EntityLoadOperationControllerTrait;
use PaginatedListingHelperTrait;

/**
* EligibleRatePlanController constructor.
*
* @param string $organization
* @param \Apigee\Edge\ClientInterface $client
* @param \Apigee\Edge\Serializer\EntitySerializerInterface|null $entitySerializer
*/
public function __construct(string $organization, ClientInterface $client, ?EntitySerializerInterface $entitySerializer = null)
{
$entitySerializer = $entitySerializer ?? new AcceptedRatePlanSerializer();
parent::__construct($organization, $client, $entitySerializer);
}

/**
* {@inheritdoc}
*/
public function getAllEligibleRatePlans(): array
{
return $this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\Monetization\Controller;

use Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface;
use Apigee\Edge\Api\Monetization\Entity\RatePlanInterface;
use Apigee\Edge\Controller\EntityControllerInterface;
use Apigee\Edge\Controller\EntityLoadOperationControllerInterface;
use DateTimeImmutable;

/**
* Interface EligibleRatePlanControllerInterface.
*
* @see https://apidocs.apigee.com/monetize/apis/
* @see https://docs.apigee.com/api-platform/monetization/subscribe-published-rate-plan-using-api
* @see https://docs.apigee.com/api-platform/monetization/view-rate-plans#viewingrateplansusingtheapi-viewingallacceptedrateplansforadeveloperusingtheapi
*/
interface EligibleRatePlanControllerInterface extends EntityControllerInterface,
EntityLoadOperationControllerInterface
{
/**
* Gets all eligible rate plans.
*
* @return \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface[]
*/
public function getAllEligibleRatePlans(): array;

/**
* Accepts a rate plan.
*
* @param \Apigee\Edge\Api\Monetization\Entity\RatePlanInterface $ratePlan
* The rate plan to be accepted.
* @param \DateTimeImmutable $startDate
* Date when the rate plan starts.
* @param \DateTimeImmutable|null $endDate
* Date when the rate plan ends.
* @param int|null $quotaTarget
* (This property is valid for adjustable notification rate plans only.)
* Target number of transactions allowed for the app developer.
* @param bool|null $suppressWarning
* Flag that specifies whether to suppress the error if the developer
* attempts to accept a rate plan that overlaps another accepted
* rate plan.
* @param bool|null $waveTerminationCharge
* Flag that specifies whether termination fees are waved when an active
* rate plan is terminated as part of activating the new rate plan.
*
* @return \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface
*/
public function acceptRatePlan(RatePlanInterface $ratePlan, DateTimeImmutable $startDate, ?DateTimeImmutable $endDate = null, ?int $quotaTarget = null, ?bool $suppressWarning = null, ?bool $waveTerminationCharge = null): AcceptedRatePlanInterface;

/**
* Update a rate plan that has been accepted by a developer.
*
* @param \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface $acceptedRatePlan
* Previously accepted rate plan that should be modified.
* @param bool|null $suppressWarning
* Flag that specifies whether to suppress the error if the developer
* attempts to accept a rate plan that overlaps another accepted
* rate plan.
* @param bool|null $waveTerminationCharge
* Flag that specifies whether termination fees are waved when an active
* rate plan is terminated as part of activating the new rate plan.
*/
public function updateSubscription(AcceptedRatePlanInterface $acceptedRatePlan, ?bool $suppressWarning = null, ?bool $waveTerminationCharge = null): void;
}
Loading

0 comments on commit 8ac0603

Please sign in to comment.