Skip to content

Commit 0ca19d4

Browse files
authored
Add the file endpoint to download documents (#169)
* Add the file endpoint to download documents * CS
1 parent 88c2817 commit 0ca19d4

File tree

10 files changed

+135
-144
lines changed

10 files changed

+135
-144
lines changed

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<issueHandlers>
1919
<PropertyNotSetInConstructor errorLevel="suppress" />
2020
<DeprecatedMethod errorLevel="suppress" />
21+
<DeprecatedTrait errorLevel="suppress" />
2122
<RedundantPropertyInitializationCheck errorLevel="suppress" />
2223
<MissingOverrideAttribute errorLevel="suppress" />
2324
<ClassMustBeFinal>

src/Clients/Traits/DocumentClientTrait.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,26 @@
55
namespace Sysix\LexOffice\Clients\Traits;
66

77
use Psr\Http\Message\ResponseInterface;
8-
use Sysix\LexOffice\Clients\File;
9-
use Sysix\LexOffice\Utils;
108

9+
/**
10+
* @deprecated use the FileClientTrait instead
11+
*/
1112
trait DocumentClientTrait
1213
{
14+
use FileClientTrait;
15+
1316
public function document(string $id, bool $asContent = false, string $acceptHeader = '*/*'): ResponseInterface
1417
{
15-
$response = $this->api
16-
->newRequest('GET', $this->resource . '/' . rawurlencode($id) . '/document')
17-
->getResponse();
18+
trigger_error(__METHOD__.' should not be called anymore, in future versions this method WILL not exist', E_USER_DEPRECATED);
1819

1920
if ($asContent === false) {
20-
return $response;
21-
}
22-
23-
if ($response->getStatusCode() !== 200) {
24-
return $response;
25-
}
21+
$response = $this->api
22+
->newRequest('GET', $this->resource . '/' . rawurlencode($id) . '/document')
23+
->getResponse();
2624

27-
$content = Utils::getJsonFromResponse($response);
28-
29-
if ($content === null || !is_object($content) || !property_exists($content, 'documentFileId') || !is_string($content->documentFileId)) {
3025
return $response;
3126
}
3227

33-
$fileClient = new File($this->api);
34-
35-
return $fileClient->get($content->documentFileId, $acceptHeader);
28+
return $this->file($id, $acceptHeader);
3629
}
3730
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sysix\LexOffice\Clients\Traits;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
trait FileClientTrait
10+
{
11+
public function file(string $id, string $acceptHeader = '*/*'): ResponseInterface
12+
{
13+
$this->api->newRequest('GET', $this->resource . '/' . rawurlencode($id) . '/file');
14+
15+
return $this->api
16+
->setRequest($this->api->getRequest()->withHeader('Accept', $acceptHeader))
17+
->getResponse();
18+
}
19+
}

tests/Clients/CreditNoteTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public function testGetVoucherListClient(): void
143143

144144
public function testDocument(): void
145145
{
146+
$this->expectDeprecationV1Warning('document');
147+
146148
[$api, $stub] = $this->createClientMockObject(CreditNote::class);
147149

148150
$response = $stub->document('resource-id');
@@ -158,38 +160,33 @@ public function testDocument(): void
158160

159161
public function testDocumentContent(): void
160162
{
161-
[$api, $stub] = $this->createClientMultiMockObject(
162-
CreditNote::class,
163-
[
164-
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
165-
new Response()
166-
]
167-
);
163+
$this->expectDeprecationV1Warning('document');
164+
165+
[$api, $stub] = $this->createClientMockObject(CreditNote::class);
168166

169167
$response = $stub->document('resource-id', true);
170168

171169
$this->assertInstanceOf(ResponseInterface::class, $response);
172170

173171
$this->assertEquals('GET', $api->getRequest()->getMethod());
174172
$this->assertEquals(
175-
$api->apiUrl . '/v1/files/fake-id',
173+
$api->apiUrl . '/v1/credit-notes/resource-id/file',
176174
$api->getRequest()->getUri()->__toString()
177175
);
178176
}
179177

180-
public function testFailedDocumentContent(): void
178+
public function testFileContent(): void
181179
{
182-
[, $stub] = $this->createClientMultiMockObject(
183-
CreditNote::class,
184-
[
185-
new Response(500),
186-
new Response()
187-
]
188-
);
180+
[$api, $stub] = $this->createClientMockObject(CreditNote::class);
189181

190-
$response = $stub->document('resource-id', true);
182+
$response = $stub->file('resource-id');
191183

192184
$this->assertInstanceOf(ResponseInterface::class, $response);
193-
$this->assertEquals(500, $response->getStatusCode());
185+
186+
$this->assertEquals('GET', $api->getRequest()->getMethod());
187+
$this->assertEquals(
188+
$api->apiUrl . '/v1/credit-notes/resource-id/file',
189+
$api->getRequest()->getUri()->__toString()
190+
);
194191
}
195192
}

tests/Clients/DeliveryNoteTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public function testGetVoucherListClient(): void
109109

110110
public function testDocument(): void
111111
{
112+
$this->expectDeprecationV1Warning('document');
113+
112114
[$api, $stub] = $this->createClientMockObject(DeliveryNote::class);
113115

114116
$response = $stub->document('resource-id');
@@ -124,38 +126,33 @@ public function testDocument(): void
124126

125127
public function testDocumentContent(): void
126128
{
127-
[$api, $stub] = $this->createClientMultiMockObject(
128-
DeliveryNote::class,
129-
[
130-
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
131-
new Response()
132-
]
133-
);
129+
$this->expectDeprecationV1Warning('document');
130+
131+
[$api, $stub] = $this->createClientMockObject(DeliveryNote::class);
134132

135133
$response = $stub->document('resource-id', true);
136134

137135
$this->assertInstanceOf(ResponseInterface::class, $response);
138136

139137
$this->assertEquals('GET', $api->getRequest()->getMethod());
140138
$this->assertEquals(
141-
$api->apiUrl . '/v1/files/fake-id',
139+
$api->apiUrl . '/v1/delivery-notes/resource-id/file',
142140
$api->getRequest()->getUri()->__toString()
143141
);
144142
}
145143

146-
public function testFailedDocumentContent(): void
144+
public function testFileContent(): void
147145
{
148-
[, $stub] = $this->createClientMultiMockObject(
149-
DeliveryNote::class,
150-
[
151-
new Response(500),
152-
new Response()
153-
]
154-
);
146+
[$api, $stub] = $this->createClientMockObject(DeliveryNote::class);
155147

156-
$response = $stub->document('resource-id', true);
148+
$response = $stub->file('resource-id');
157149

158150
$this->assertInstanceOf(ResponseInterface::class, $response);
159-
$this->assertEquals(500, $response->getStatusCode());
151+
152+
$this->assertEquals('GET', $api->getRequest()->getMethod());
153+
$this->assertEquals(
154+
$api->apiUrl . '/v1/delivery-notes/resource-id/file',
155+
$api->getRequest()->getUri()->__toString()
156+
);
160157
}
161158
}

tests/Clients/DownPaymentInvoiceTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public function testGetVoucherListClient(): void
5858

5959
public function testDocument(): void
6060
{
61+
$this->expectDeprecationV1Warning('document');
62+
6163
[$api, $stub] = $this->createClientMockObject(DownPaymentInvoice::class);
6264

6365
$response = $stub->document('resource-id');
@@ -73,38 +75,33 @@ public function testDocument(): void
7375

7476
public function testDocumentContent(): void
7577
{
76-
[$api, $stub] = $this->createClientMultiMockObject(
77-
DownPaymentInvoice::class,
78-
[
79-
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
80-
new Response()
81-
]
82-
);
78+
$this->expectDeprecationV1Warning('document');
79+
80+
[$api, $stub] = $this->createClientMockObject(DownPaymentInvoice::class);
8381

8482
$response = $stub->document('resource-id', true);
8583

8684
$this->assertInstanceOf(ResponseInterface::class, $response);
8785

8886
$this->assertEquals('GET', $api->getRequest()->getMethod());
8987
$this->assertEquals(
90-
$api->apiUrl . '/v1/files/fake-id',
88+
$api->apiUrl . '/v1/down-payment-invoices/resource-id/file',
9189
$api->getRequest()->getUri()->__toString()
9290
);
9391
}
9492

95-
public function testFailedDocumentContent(): void
93+
public function testFileContent(): void
9694
{
97-
[, $stub] = $this->createClientMultiMockObject(
98-
DownPaymentInvoice::class,
99-
[
100-
new Response(500),
101-
new Response()
102-
]
103-
);
95+
[$api, $stub] = $this->createClientMockObject(DownPaymentInvoice::class);
10496

105-
$response = $stub->document('resource-id', true);
97+
$response = $stub->file('resource-id');
10698

10799
$this->assertInstanceOf(ResponseInterface::class, $response);
108-
$this->assertEquals(500, $response->getStatusCode());
100+
101+
$this->assertEquals('GET', $api->getRequest()->getMethod());
102+
$this->assertEquals(
103+
$api->apiUrl . '/v1/down-payment-invoices/resource-id/file',
104+
$api->getRequest()->getUri()->__toString()
105+
);
109106
}
110107
}

tests/Clients/DunningTest.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Sysix\LexOffice\Tests\Clients;
66

7-
use GuzzleHttp\Psr7\Response;
87
use Psr\Http\Message\ResponseInterface;
98
use Sysix\LexOffice\Clients\Dunning;
109
use Sysix\LexOffice\Tests\TestClient;
@@ -45,6 +44,8 @@ public function testPursue(): void
4544

4645
public function testDocument(): void
4746
{
47+
$this->expectDeprecationV1Warning('document');
48+
4849
[$api, $stub] = $this->createClientMockObject(Dunning::class);
4950

5051
$response = $stub->document('resource-id');
@@ -60,38 +61,33 @@ public function testDocument(): void
6061

6162
public function testDocumentContent(): void
6263
{
63-
[$api, $stub] = $this->createClientMultiMockObject(
64-
Dunning::class,
65-
[
66-
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
67-
new Response()
68-
]
69-
);
64+
$this->expectDeprecationV1Warning('document');
65+
66+
[$api, $stub] = $this->createClientMockObject(Dunning::class);
7067

7168
$response = $stub->document('resource-id', true);
7269

7370
$this->assertInstanceOf(ResponseInterface::class, $response);
7471

7572
$this->assertEquals('GET', $api->getRequest()->getMethod());
7673
$this->assertEquals(
77-
$api->apiUrl . '/v1/files/fake-id',
74+
$api->apiUrl . '/v1/dunnings/resource-id/file',
7875
$api->getRequest()->getUri()->__toString()
7976
);
8077
}
8178

82-
public function testFailedDocumentContent(): void
79+
public function testFileContent(): void
8380
{
84-
[, $stub] = $this->createClientMultiMockObject(
85-
Dunning::class,
86-
[
87-
new Response(500),
88-
new Response()
89-
]
90-
);
81+
[$api, $stub] = $this->createClientMockObject(Dunning::class);
9182

92-
$response = $stub->document('resource-id', true);
83+
$response = $stub->file('resource-id');
9384

9485
$this->assertInstanceOf(ResponseInterface::class, $response);
95-
$this->assertEquals(500, $response->getStatusCode());
86+
87+
$this->assertEquals('GET', $api->getRequest()->getMethod());
88+
$this->assertEquals(
89+
$api->apiUrl . '/v1/dunnings/resource-id/file',
90+
$api->getRequest()->getUri()->__toString()
91+
);
9692
}
9793
}

tests/Clients/InvoiceTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public function testGetVoucherListClient(): void
143143

144144
public function testDocument(): void
145145
{
146+
$this->expectDeprecationV1Warning('document');
147+
146148
[$api, $stub] = $this->createClientMockObject(Invoice::class);
147149

148150
$response = $stub->document('resource-id');
@@ -158,38 +160,33 @@ public function testDocument(): void
158160

159161
public function testDocumentContent(): void
160162
{
161-
[$api, $stub] = $this->createClientMultiMockObject(
162-
Invoice::class,
163-
[
164-
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
165-
new Response()
166-
]
167-
);
163+
$this->expectDeprecationV1Warning('document');
164+
165+
[$api, $stub] = $this->createClientMockObject(Invoice::class);
168166

169167
$response = $stub->document('resource-id', true);
170168

171169
$this->assertInstanceOf(ResponseInterface::class, $response);
172170

173171
$this->assertEquals('GET', $api->getRequest()->getMethod());
174172
$this->assertEquals(
175-
$api->apiUrl . '/v1/files/fake-id',
173+
$api->apiUrl . '/v1/invoices/resource-id/file',
176174
$api->getRequest()->getUri()->__toString()
177175
);
178176
}
179177

180-
public function testFailedDocumentContent(): void
178+
public function testFileContent(): void
181179
{
182-
[, $stub] = $this->createClientMultiMockObject(
183-
Invoice::class,
184-
[
185-
new Response(500),
186-
new Response()
187-
]
188-
);
180+
[$api, $stub] = $this->createClientMockObject(Invoice::class);
189181

190-
$response = $stub->document('resource-id', true);
182+
$response = $stub->file('resource-id');
191183

192184
$this->assertInstanceOf(ResponseInterface::class, $response);
193-
$this->assertEquals(500, $response->getStatusCode());
185+
186+
$this->assertEquals('GET', $api->getRequest()->getMethod());
187+
$this->assertEquals(
188+
$api->apiUrl . '/v1/invoices/resource-id/file',
189+
$api->getRequest()->getUri()->__toString()
190+
);
194191
}
195192
}

0 commit comments

Comments
 (0)