Skip to content

Commit 593a308

Browse files
committed
fix: accessing related resource collections
1 parent 174098f commit 593a308

12 files changed

+62
-54
lines changed

phpstan-baseline.neon

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ parameters:
77
path: examples/capabilities/get-capability.php
88

99
-
10-
message: "#^Variable \\$mollie might not be defined\\.$#"
10+
message: '#^Variable \$mollie might not be defined\.$#'
11+
identifier: variable.undefined
1112
count: 1
1213
path: examples/capabilities/list-capabilities.php
1314

1415
-
15-
message: "#^Variable \\$mollie might not be defined\\.$#"
16+
message: '#^Variable \$mollie might not be defined\.$#'
17+
identifier: variable.undefined
1618
count: 1
1719
path: examples/captures/create-capture.php
1820

@@ -292,6 +294,12 @@ parameters:
292294
count: 5
293295
path: src/Http/Data/DataCollection.php
294296

297+
-
298+
message: '#^Unsafe usage of new static\(\)\.$#'
299+
identifier: new.static
300+
count: 1
301+
path: src/Resources/ResourceCollection.php
302+
295303
-
296304
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertTrue\(\) with true will always evaluate to true\.$#'
297305
identifier: method.alreadyNarrowedType

src/Contracts/IsResponseAware.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ interface IsResponseAware extends ViableResponse
88
{
99
public function getResponse(): Response;
1010

11-
public function setResponse(Response $response): self;
11+
/**
12+
* @return $this
13+
*/
14+
public function setResponse(Response $response);
1215
}

src/Fake/ResourceResponseBuilder.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
use Mollie\Api\Exceptions\LogicException;
66
use Mollie\Api\Resources\BaseResource;
77
use Mollie\Api\Traits\ForwardsCalls;
8+
use ReflectionClass;
89

910
/**
1011
* Builder for creating mock responses for Mollie API resources.
1112
*
1213
* @method self embed(string $collectionClass) Embed a collection of resources into the response
14+
* @method self add(array $data) Add a resource to the embedded response
15+
* @method self addMany(array $data) Add multiple resources to the embedded response
1316
*/
1417
class ResourceResponseBuilder
1518
{
@@ -49,7 +52,12 @@ public function with(array $data): self
4952
*/
5053
public function create(): MockResponse
5154
{
52-
$data = $this->data;
55+
$reflection = new ReflectionClass($this->resourceClass);
56+
$baseName = strtolower($reflection->getShortName());
57+
58+
$sampleContents = json_decode(FakeResponseLoader::load($baseName), true);
59+
60+
$data = array_merge(['_links' => $sampleContents['_links']], $this->data);
5361
$data['_embedded'] = [];
5462

5563
foreach ($this->embeddedBuilders as $key => $builder) {
@@ -63,20 +71,6 @@ public function create(): MockResponse
6371
unset($data['_embedded']);
6472
}
6573

66-
// add standard links
67-
if (empty($data['_links'])) {
68-
$data['_links'] = [
69-
'self' => [
70-
'href' => '...',
71-
'type' => 'application/hal+json',
72-
],
73-
'documentation' => [
74-
'href' => '...',
75-
'type' => 'text/html',
76-
],
77-
];
78-
}
79-
8074
return new MockResponse($data);
8175
}
8276

src/Resources/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Client extends BaseResource implements EmbeddedResourcesContract
3131
public $_links;
3232

3333
/**
34-
* @var \stdClass[]
34+
* @var \stdClass
3535
*/
3636
public $_embedded;
3737

src/Resources/Payment.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class Payment extends BaseResource implements EmbeddedResourcesContract
295295
public $_links;
296296

297297
/**
298-
* @var \stdClass[]
298+
* @var \stdClass
299299
*/
300300
public $_embedded;
301301

@@ -594,7 +594,7 @@ public function hasSplitPayments(): bool
594594
public function refunds(): RefundCollection
595595
{
596596
if (! isset($this->_links->refunds->href)) {
597-
return new RefundCollection($this->connector, $this->response);
597+
return RefundCollection::withResponse($this->response, $this->connector);
598598
}
599599

600600
return $this
@@ -636,7 +636,7 @@ public function listRefunds(array $parameters = []): RefundCollection
636636
public function captures(): CaptureCollection
637637
{
638638
if (! isset($this->_links->captures->href)) {
639-
return new CaptureCollection($this->connector, $this->response);
639+
return CaptureCollection::withResponse($this->response, $this->connector);
640640
}
641641

642642
return $this
@@ -667,7 +667,7 @@ public function getCapture($captureId, array $parameters = []): Capture
667667
public function chargebacks(): ChargebackCollection
668668
{
669669
if (! isset($this->_links->chargebacks->href)) {
670-
return new ChargebackCollection($this->connector, $this->response);
670+
return ChargebackCollection::withResponse($this->response, $this->connector);
671671
}
672672

673673
return $this
@@ -720,7 +720,7 @@ public function update(): Payment
720720
$this->redirectUrl,
721721
$this->cancelUrl,
722722
$this->webhookUrl,
723-
new Metadata($this->metadata),
723+
new Metadata((array) $this->metadata),
724724
$this->method,
725725
$this->locale,
726726
$this->restrictPaymentMethodsToCountry,

src/Resources/PaymentLink.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
namespace Mollie\Api\Resources;
44

5+
use Mollie\Api\Traits\HasMode;
6+
57
/**
68
* @property \Mollie\Api\MollieApiClient $connector
79
*/
810
class PaymentLink extends BaseResource
911
{
12+
use HasMode;
13+
1014
/**
1115
* Id of the payment link (on the Mollie platform).
1216
*
@@ -136,12 +140,10 @@ public function getCheckoutUrl(): ?string
136140
*/
137141
public function update(): ?PaymentLink
138142
{
139-
$body = $this->withTestmode([
143+
return $this->connector->paymentLinks->update($this->id, $this->withMode([
140144
'description' => $this->description,
141145
'archived' => $this->archived,
142-
]);
143-
144-
return $this->connector->paymentLinks->update($this->id, $body);
146+
]));
145147
}
146148

147149
/**
@@ -153,11 +155,9 @@ public function update(): ?PaymentLink
153155
*/
154156
public function archive()
155157
{
156-
$data = $this->withTestmode([
158+
return $this->connector->paymentLinks->update($this->id, $this->withMode([
157159
'archived' => true,
158-
]);
159-
160-
return $this->connector->paymentLinks->update($this->id, $data);
160+
]));
161161
}
162162

163163
/**
@@ -171,19 +171,7 @@ public function payments(?string $from = null, ?int $limit = null, array $filter
171171
$this,
172172
$from,
173173
$limit,
174-
$this->withTestmode($filters)
174+
$this->withMode($filters)
175175
);
176176
}
177-
178-
/**
179-
* Apply the preset options.
180-
*
181-
* @return array
182-
*/
183-
private function withTestmode(array $options)
184-
{
185-
return array_merge([
186-
'testmode' => $this->mode === 'test',
187-
], $options);
188-
}
189177
}

src/Resources/Profile.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function update(): ?Profile
124124
public function chargebacks(): ChargebackCollection
125125
{
126126
if (! isset($this->_links->chargebacks->href)) {
127-
return new ChargebackCollection($this->connector, $this->response);
127+
return ChargebackCollection::withResponse($this->response, $this->connector);
128128
}
129129

130130
return $this
@@ -140,7 +140,7 @@ public function chargebacks(): ChargebackCollection
140140
public function methods(): MethodCollection
141141
{
142142
if (! isset($this->_links->methods->href)) {
143-
return new MethodCollection($this->connector, $this->response);
143+
return MethodCollection::withResponse($this->response, $this->connector);
144144
}
145145

146146
return $this
@@ -178,7 +178,7 @@ public function disableMethod(string $methodId): void
178178
public function payments(): PaymentCollection
179179
{
180180
if (! isset($this->_links->payments->href)) {
181-
return new PaymentCollection($this->connector, $this->response);
181+
return PaymentCollection::withResponse($this->response, $this->connector);
182182
}
183183

184184
return $this
@@ -194,7 +194,7 @@ public function payments(): PaymentCollection
194194
public function refunds(): RefundCollection
195195
{
196196
if (! isset($this->_links->refunds->href)) {
197-
return new RefundCollection($this->connector, $this->response);
197+
return RefundCollection::withResponse($this->response, $this->connector);
198198
}
199199

200200
return $this

src/Resources/ResourceCollection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Mollie\Api\Resources;
44

5+
use Mollie\Api\Contracts\Connector;
6+
use Mollie\Api\Http\Response;
7+
58
abstract class ResourceCollection extends BaseCollection
69
{
710
/**
@@ -17,4 +20,14 @@ public static function getResourceClass(): string
1720

1821
return static::$resource;
1922
}
23+
24+
/**
25+
* @return $this
26+
*/
27+
public static function withResponse(Response $response, Connector $connector, $items = [], ?\stdClass $_links = null): self
28+
{
29+
$collection = new static($connector, $items, $_links);
30+
31+
return $collection->setResponse($response);
32+
}
2033
}

src/Resources/Subscription.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function cancel(): ?Subscription
199199
public function payments(): PaymentCollection
200200
{
201201
if (! isset($this->_links->payments->href)) {
202-
return new PaymentCollection($this->connector, $this->response);
202+
return PaymentCollection::withResponse($this->response, $this->connector);
203203
}
204204

205205
return $this

src/Traits/HasResponse.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public function getResponse(): Response
1414
return $this->response;
1515
}
1616

17-
public function setResponse(Response $response): self
17+
/**
18+
* @return $this
19+
*/
20+
public function setResponse(Response $response)
1821
{
1922
$this->response = $response;
2023

0 commit comments

Comments
 (0)