Skip to content

Commit fbc7af6

Browse files
authored
Merge pull request #105 from codebar-ag/feature-readme
Feature readme.md update
2 parents 09bf3ea + 28e2ffd commit fbc7af6

File tree

8 files changed

+147
-6
lines changed

8 files changed

+147
-6
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ $config = Config::make([
9595
'request_timeout_in_seconds' => 15,
9696
]);
9797

98-
$connector = new DocuWareStaticConnector($config);
98+
$connector = new DocuWareDynamicConnector($config);
9999

100100
/**
101101
* Return an organization.
@@ -518,6 +518,11 @@ logout with DocuWare:
518518
```php
519519
use CodebarAg\DocuWare\Facades\DocuWare;
520520

521+
/**
522+
* Receive a cookie
523+
*/
524+
DocuWare::cookie(string $url, string $username, string $password);
525+
521526
/**
522527
* Login with your credentials. You only need to login once. Afterwards the
523528
* authentication cookie is stored in the cache as `docuware.cookies` and
@@ -580,7 +585,6 @@ $request->disableCaching();
580585
$response = $connector->send($request);
581586
```
582587

583-
584588
## 💥 Exceptions explained
585589

586590
- `CodebarAg\DocuWare\Exceptions\UnableToMakeRequest`

src/DTO/Cookie.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace CodebarAg\DocuWare\DTO;
4+
5+
use CodebarAg\DocuWare\Exceptions\UnableToLoginNoCookies;
6+
use GuzzleHttp\Cookie\CookieJar;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Carbon;
9+
10+
final class Cookie
11+
{
12+
public function __construct(
13+
public string $cookie,
14+
public Carbon $cookie_created_at,
15+
public Carbon $cookie_lifetime_until,
16+
) {
17+
}
18+
19+
public static function make(CookieJar $cookieJar): self
20+
{
21+
throw_if($cookieJar->toArray() === [], UnableToLoginNoCookies::create());
22+
23+
$data = collect($cookieJar->toArray())
24+
->reject(fn (array $cookie) => Arr::get($cookie, 'Value') === '')
25+
->firstWhere('Name', '.DWPLATFORMAUTH');
26+
27+
$now = now();
28+
29+
return new self(
30+
cookie: Arr::get($data, 'Value'),
31+
cookie_created_at: $now,
32+
cookie_lifetime_until: $now->addSeconds(525600 * 0.75),
33+
);
34+
}
35+
}

src/DocuWare.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,50 @@
22

33
namespace CodebarAg\DocuWare;
44

5+
use CodebarAg\DocuWare\DTO\Cookie;
56
use CodebarAg\DocuWare\Events\DocuWareResponseLog;
67
use CodebarAg\DocuWare\Requests\Auth\GetLogoffRequest;
8+
use CodebarAg\DocuWare\Requests\Auth\PostLoginRequest;
79
use CodebarAg\DocuWare\Support\Auth;
810
use CodebarAg\DocuWare\Support\EnsureValidCookie;
911
use CodebarAg\DocuWare\Support\EnsureValidCredentials;
12+
use CodebarAg\DocuWare\Support\EnsureValidResponse;
13+
use GuzzleHttp\Cookie\CookieJar;
1014
use Saloon\Exceptions\InvalidResponseClassException;
1115
use Saloon\Exceptions\PendingRequestException;
1216

1317
class DocuWare
1418
{
19+
/**
20+
* @throws InvalidResponseClassException
21+
* @throws \Throwable
22+
* @throws \ReflectionException
23+
* @throws PendingRequestException
24+
*/
25+
public function cookie(string $url, string $username, string $password, $rememberMe = false, $redirectToMyselfInCaseOfError = false, $licenseType = null): Cookie
26+
{
27+
$cookieJar = new CookieJar();
28+
29+
$request = new PostLoginRequest(
30+
$url,
31+
$username,
32+
$password,
33+
$rememberMe,
34+
$redirectToMyselfInCaseOfError,
35+
$licenseType);
36+
37+
$request->config()->add('cookies', $cookieJar);
38+
39+
$response = $request->send();
40+
41+
event(new DocuWareResponseLog($response));
42+
43+
EnsureValidResponse::from($response);
44+
45+
return Cookie::make($cookieJar);
46+
47+
}
48+
1549
/**
1650
* @throws InvalidResponseClassException
1751
* @throws \Throwable
@@ -21,7 +55,6 @@ class DocuWare
2155
public function login(): void
2256
{
2357
EnsureValidCredentials::check();
24-
2558
// Checks if already logged in, if not, logs in
2659
EnsureValidCookie::check();
2760
}

src/Facades/DocuWare.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Carbon\Carbon;
66
use CodebarAg\DocuWare\DocuWareSearchRequestBuilder;
77
use CodebarAg\DocuWare\DocuWareUrl;
8+
use CodebarAg\DocuWare\DTO\Cookie;
89
use CodebarAg\DocuWare\DTO\Dialog;
910
use CodebarAg\DocuWare\DTO\Document;
1011
use CodebarAg\DocuWare\DTO\DocumentThumbnail;
@@ -18,7 +19,7 @@
1819
/**
1920
* @see \CodebarAg\DocuWare\DocuWare
2021
*
21-
* @method static self cookie()
22+
* @method static Cookie cookie(string $url, string $username, string $password, $rememberMe = false, $redirectToMyselfInCaseOfError = false, $licenseType = null)
2223
* @method static string login()
2324
* @method static void logout()
2425
* @method static Organization getOrganization(string $organizationId)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace CodebarAg\DocuWare\Requests\Auth;
4+
5+
use CodebarAg\DocuWare\Events\DocuWareResponseLog;
6+
use CodebarAg\DocuWare\Support\EnsureValidResponse;
7+
use Saloon\Contracts\Body\HasBody;
8+
use Saloon\Contracts\Response;
9+
use Saloon\Enums\Method;
10+
use Saloon\Http\SoloRequest;
11+
use Saloon\Traits\Body\HasFormBody;
12+
13+
class PostLoginRequest extends SoloRequest implements HasBody
14+
{
15+
use HasFormBody;
16+
17+
protected Method $method = Method::POST;
18+
19+
public function __construct(
20+
protected readonly string $url,
21+
protected readonly string $username,
22+
protected readonly string $password,
23+
protected readonly bool $rememberMe = false,
24+
protected readonly bool $redirectToMyselfInCaseOfError = false,
25+
protected readonly ?string $licenseType = null,
26+
) {
27+
}
28+
29+
public function resolveEndpoint(): string
30+
{
31+
return $this->url.'/DocuWare/Platform/Account/Logon';
32+
}
33+
34+
protected function defaultBody(): array
35+
{
36+
return [
37+
'UserName' => $this->username,
38+
'Password' => $this->password,
39+
'RememberMe' => $this->rememberMe,
40+
'RedirectToMyselfInCaseOfError' => $this->redirectToMyselfInCaseOfError,
41+
'LicenseType' => $this->licenseType,
42+
];
43+
}
44+
45+
public function createDtoFromResponse(Response $response): Response
46+
{
47+
event(new DocuWareResponseLog($response));
48+
49+
EnsureValidResponse::from($response);
50+
51+
return $response;
52+
}
53+
}

src/Responses/FileCabinets/GetFileCabinetsResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static function fromResponse(Response $response): Collection|Enumerable
1717

1818
EnsureValidResponse::from($response);
1919

20-
$cabinets = $response->throw()->json('FileCabinet');
20+
$cabinets = $response->throw()->body();
2121

2222
return collect($cabinets)->map(fn (array $cabinet) => FileCabinet::fromJson($cabinet));
2323
}

src/Support/EnsureValidCookie.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function check(): void
2323

2424
EnsureValidCredentials::check();
2525

26-
event(new DocuWareCookieCreatedLog('Creating new authenticaion cookie for caching'));
26+
event(new DocuWareCookieCreatedLog('Creating new authentication cookie for caching'));
2727

2828
$cookieJar = new CookieJar();
2929

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use CodebarAg\DocuWare\Facades\DocuWare;
4+
5+
uses()->group('docuware');
6+
7+
it('cookie method', function () {
8+
9+
$url = config('docuware.credentials.url');
10+
$username = config('docuware.credentials.username');
11+
$password = config('docuware.credentials.password');
12+
13+
$cookie = DocuWare::cookie($url, $username, $password);
14+
expect($cookie)->not()->toBeEmpty();
15+
});

0 commit comments

Comments
 (0)