LauLamanApps /
ecurring-api
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace LauLamanApps\eCurring; |
||||
| 6 | |||||
| 7 | use LauLamanApps\eCurring\Http\ClientInterface; |
||||
| 8 | use LauLamanApps\eCurring\Http\Endpoint\MapperInterface; |
||||
| 9 | use LauLamanApps\eCurring\Http\Resource\Creatable; |
||||
| 10 | use LauLamanApps\eCurring\Http\Resource\CreateParserInterface; |
||||
| 11 | use LauLamanApps\eCurring\Http\Resource\Deletable; |
||||
| 12 | use LauLamanApps\eCurring\Http\Resource\Updatable; |
||||
| 13 | use LauLamanApps\eCurring\Http\Resource\UpdateParserInterface; |
||||
| 14 | use LauLamanApps\eCurring\Resource\Curser\Pagination; |
||||
| 15 | use LauLamanApps\eCurring\Resource\Customer; |
||||
| 16 | use LauLamanApps\eCurring\Resource\CustomerCollection; |
||||
| 17 | use LauLamanApps\eCurring\Resource\Factory\CustomerFactoryInterface; |
||||
| 18 | use LauLamanApps\eCurring\Resource\Factory\ProductFactoryInterface; |
||||
| 19 | use LauLamanApps\eCurring\Resource\Factory\SubscriptionFactoryInterface; |
||||
| 20 | use LauLamanApps\eCurring\Resource\Factory\TransactionFactoryInterface; |
||||
| 21 | use LauLamanApps\eCurring\Resource\Product; |
||||
| 22 | use LauLamanApps\eCurring\Resource\ProductCollection; |
||||
| 23 | use LauLamanApps\eCurring\Resource\Subscription; |
||||
| 24 | use LauLamanApps\eCurring\Resource\SubscriptionCollection; |
||||
| 25 | use LauLamanApps\eCurring\Resource\Transaction; |
||||
| 26 | use LauLamanApps\eCurring\Resource\TransactionCollection; |
||||
| 27 | use Ramsey\Uuid\UuidInterface; |
||||
| 28 | |||||
| 29 | final class eCurringClient implements eCurringClientInterface |
||||
| 30 | { |
||||
| 31 | /** |
||||
| 32 | * @var ClientInterface |
||||
| 33 | */ |
||||
| 34 | private $httpClient; |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * @var ProductFactoryInterface |
||||
| 38 | */ |
||||
| 39 | private $subscriptionPlanFactory; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * @var TransactionFactoryInterface |
||||
| 43 | */ |
||||
| 44 | private $transactionFactory; |
||||
| 45 | |||||
| 46 | /** |
||||
| 47 | * @var SubscriptionFactoryInterface |
||||
| 48 | */ |
||||
| 49 | private $subscriptionFactory; |
||||
| 50 | |||||
| 51 | /** |
||||
| 52 | * @var CustomerFactoryInterface |
||||
| 53 | */ |
||||
| 54 | private $customerFactory; |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * @var CreateParserInterface |
||||
| 58 | */ |
||||
| 59 | private $createParser; |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * @var UpdateParserInterface |
||||
| 63 | */ |
||||
| 64 | private $updateParser; |
||||
| 65 | |||||
| 66 | public function __construct( |
||||
| 67 | ClientInterface $httpClient, |
||||
| 68 | CustomerFactoryInterface $customerFactory, |
||||
| 69 | SubscriptionFactoryInterface $subscriptionFactory, |
||||
| 70 | ProductFactoryInterface $subscriptionPlanFactory, |
||||
| 71 | TransactionFactoryInterface $transactionFactory, |
||||
| 72 | CreateParserInterface $createParser, |
||||
| 73 | UpdateParserInterface $updateParser |
||||
| 74 | ) { |
||||
| 75 | $this->httpClient = $httpClient; |
||||
| 76 | $this->customerFactory = $customerFactory; |
||||
| 77 | $this->subscriptionFactory = $subscriptionFactory; |
||||
| 78 | $this->subscriptionPlanFactory = $subscriptionPlanFactory; |
||||
| 79 | $this->transactionFactory = $transactionFactory; |
||||
| 80 | $this->createParser = $createParser; |
||||
| 81 | $this->updateParser = $updateParser; |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | public function create(Creatable $entity): Creatable |
||||
| 85 | { |
||||
| 86 | switch (true) { |
||||
| 87 | case $entity instanceof Customer: |
||||
| 88 | return $this->createCustomer($entity); |
||||
| 89 | case $entity instanceof Subscription: |
||||
| 90 | return $this->createSubscription($entity); |
||||
| 91 | case $entity instanceof Transaction: |
||||
| 92 | return $this->createTransaction($entity); |
||||
| 93 | } |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | public function update(Updatable $entity): Updatable |
||||
| 97 | { |
||||
| 98 | switch (true) { |
||||
| 99 | case $entity instanceof Customer: |
||||
| 100 | return $this->updateCustomer($entity); |
||||
| 101 | case $entity instanceof Subscription: |
||||
| 102 | return $this->updateSubscription($entity); |
||||
| 103 | } |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | public function delete(Deletable $entity): void |
||||
| 107 | { |
||||
| 108 | switch (true) { |
||||
| 109 | case $entity instanceof Transaction: |
||||
| 110 | $this->deleteTransaction($entity); |
||||
| 111 | } |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | public function getCustomers(?Pagination $pagination = null): CustomerCollection |
||||
| 115 | { |
||||
| 116 | $json = $this->httpClient->getJson( |
||||
| 117 | $this->httpClient->getEndpoint(MapperInterface::GET_CUSTOMERS, [], $pagination) |
||||
| 118 | ); |
||||
| 119 | |||||
| 120 | return $this->customerFactory->fromArray( |
||||
| 121 | $this, |
||||
| 122 | $this->decodeJsonToArray($json), |
||||
| 123 | $pagination ?? new Pagination(10) |
||||
| 124 | ); |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | public function getCustomer(string $id): Customer |
||||
| 128 | { |
||||
| 129 | $json = $this->httpClient->getJson( |
||||
| 130 | $this->httpClient->getEndpoint(MapperInterface::GET_CUSTOMER, [$id]) |
||||
| 131 | ); |
||||
| 132 | |||||
| 133 | return $this->customerFactory->fromData($this, $this->decodeJsonToArray($json)['data']); |
||||
| 134 | } |
||||
| 135 | |||||
| 136 | public function getProducts(?Pagination $pagination = null): ProductCollection |
||||
| 137 | { |
||||
| 138 | $json = $this->httpClient->getJson( |
||||
| 139 | $this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION_PLANS, [], $pagination) |
||||
| 140 | ); |
||||
| 141 | |||||
| 142 | return $this->subscriptionPlanFactory->fromArray( |
||||
| 143 | $this, |
||||
| 144 | $this->decodeJsonToArray($json), |
||||
| 145 | $pagination ?? new Pagination(10) |
||||
| 146 | ); |
||||
| 147 | } |
||||
| 148 | |||||
| 149 | public function getProduct(string $id): Product |
||||
| 150 | { |
||||
| 151 | $json = $this->httpClient->getJson( |
||||
| 152 | $this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION_PLAN, [$id]) |
||||
| 153 | ); |
||||
| 154 | |||||
| 155 | return $this->subscriptionPlanFactory->fromData($this, $this->decodeJsonToArray($json)['data']); |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | public function getSubscriptions(?Pagination $page = null): SubscriptionCollection |
||||
| 159 | { |
||||
| 160 | $json = $this->httpClient->getJson( |
||||
| 161 | $this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTIONS) |
||||
| 162 | ); |
||||
| 163 | |||||
| 164 | return $this->subscriptionFactory->fromArray( |
||||
| 165 | $this, |
||||
| 166 | $this->decodeJsonToArray($json), |
||||
| 167 | $page ?? new Pagination(10) |
||||
| 168 | ); |
||||
| 169 | } |
||||
| 170 | |||||
| 171 | public function getSubscription(string $id): Subscription |
||||
| 172 | { |
||||
| 173 | $json = $this->httpClient->getJson( |
||||
| 174 | $this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION, [$id]) |
||||
| 175 | ); |
||||
| 176 | |||||
| 177 | return $this->subscriptionFactory->fromData($this, $this->decodeJsonToArray($json)['data']); |
||||
| 178 | } |
||||
| 179 | |||||
| 180 | public function getSubscriptionTransactions(Subscription $subscription, ?Pagination $pagination = null): TransactionCollection |
||||
| 181 | { |
||||
| 182 | $json = $this->httpClient->getJson( |
||||
| 183 | $this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION_TRANSACTIONS, [$subscription->getId()]) |
||||
| 184 | ); |
||||
| 185 | |||||
| 186 | return $this->transactionFactory->fromSubscriptionArray( |
||||
| 187 | $this, |
||||
| 188 | $this->decodeJsonToArray($json), |
||||
| 189 | $subscription, |
||||
| 190 | $pagination ?? new Pagination(10) |
||||
| 191 | ); |
||||
| 192 | } |
||||
| 193 | |||||
| 194 | public function getTransaction(UuidInterface $id): Transaction |
||||
| 195 | { |
||||
| 196 | $json = $this->httpClient->getJson( |
||||
| 197 | $this->httpClient->getEndpoint(MapperInterface::GET_SUBSCRIPTION_PLAN, [$id]) |
||||
| 198 | ); |
||||
| 199 | |||||
| 200 | return $this->transactionFactory->fromData($this->decodeJsonToArray($json)['data']); |
||||
| 201 | } |
||||
| 202 | |||||
| 203 | private function createCustomer(Customer $customer): Customer |
||||
| 204 | { |
||||
| 205 | $data = $this->createParser->parse($customer); |
||||
| 206 | |||||
| 207 | $json = $this->httpClient->getJson( |
||||
| 208 | $this->httpClient->postEndpoint(MapperInterface::POST_CUSTOMER, $data) |
||||
| 209 | ); |
||||
| 210 | |||||
| 211 | return $this->customerFactory->fromData($this, $this->decodeJsonToArray($json)['data']); |
||||
| 212 | } |
||||
| 213 | private function updateCustomer(Customer $customer): Customer |
||||
| 214 | { |
||||
| 215 | $data = $this->updateParser->parse($customer); |
||||
| 216 | |||||
| 217 | $json = $this->httpClient->getJson( |
||||
| 218 | $this->httpClient->patchEndpoint(MapperInterface::PATCH_CUSTOMER, $data, [$customer->getId()]) |
||||
| 219 | ); |
||||
| 220 | |||||
| 221 | return $this->customerFactory->fromData($this, $this->decodeJsonToArray($json)); |
||||
| 222 | } |
||||
| 223 | private function createSubscription(Subscription $subscription): Subscription |
||||
| 224 | { |
||||
| 225 | $data = $this->createParser->parse($subscription); |
||||
| 226 | |||||
| 227 | $json = $this->httpClient->getJson( |
||||
| 228 | $this->httpClient->postEndpoint(MapperInterface::POST_SUBSCRIPTION, $data) |
||||
| 229 | ); |
||||
| 230 | |||||
| 231 | return $this->subscriptionFactory->fromData($this, $this->decodeJsonToArray($json)['data']); |
||||
| 232 | } |
||||
| 233 | |||||
| 234 | private function updateSubscription(Subscription $subscription): Subscription |
||||
| 235 | { |
||||
| 236 | $data = $this->updateParser->parse($subscription); |
||||
| 237 | |||||
| 238 | $json = $this->httpClient->getJson( |
||||
| 239 | $this->httpClient->patchEndpoint(MapperInterface::PATCH_SUBSCRIPTION, $data, [$subscription->getId()]) |
||||
| 240 | ); |
||||
| 241 | |||||
| 242 | return $this->subscriptionFactory->fromData($this, $this->decodeJsonToArray($json)['data']); |
||||
| 243 | } |
||||
| 244 | |||||
| 245 | private function createTransaction(Transaction $transaction): Transaction |
||||
| 246 | { |
||||
| 247 | $data = $this->createParser->parse($transaction); |
||||
| 248 | |||||
| 249 | $json = $this->httpClient->getJson( |
||||
| 250 | $this->httpClient->postEndpoint(MapperInterface::POST_TRANSACTION, $data) |
||||
| 251 | ); |
||||
| 252 | |||||
| 253 | return $this->transactionFactory->fromData($this, $this->decodeJsonToArray($json)['data']); |
||||
|
0 ignored issues
–
show
$this of type LauLamanApps\eCurring\eCurringClient is incompatible with the type array expected by parameter $data of LauLamanApps\eCurring\Re...ryInterface::fromData().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 254 | } |
||||
| 255 | |||||
| 256 | private function deleteTransaction(Transaction $transaction): void |
||||
| 257 | { |
||||
| 258 | $this->httpClient->deleteEndpoint(MapperInterface::DELETE_TRANSACTION, [$transaction->getId()]); |
||||
| 259 | } |
||||
| 260 | |||||
| 261 | private function decodeJsonToArray(string $json): array |
||||
| 262 | { |
||||
| 263 | return json_decode($json, true); |
||||
| 264 | } |
||||
| 265 | } |
||||
| 266 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.