Skip to content

Commit

Permalink
feat: improve docs and unify uninstall to delete
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed May 9, 2023
1 parent 05efadd commit d2ff1cf
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 19 deletions.
8 changes: 5 additions & 3 deletions docs/01-getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

## Installation

Install the Shopware APP SDK via composer:

```bash
composer req shopware/app-php-sdk
composer require shopware/app-php-sdk
```

The dependency `php-http/discovery` will automatically install a missing HTTP Client
After the package installation, Composer will automatically install http client if missing

## Registration
## Registration process

```php
$app = new AppConfiguration('Foo', 'test', 'http://localhost:6001/register/callback');
Expand Down
15 changes: 13 additions & 2 deletions docs/02-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@ To track correctly the state in our Database, we need to implement some lifecycl
- `deactivate`
- `uninstall`

The lifecycle registration in the `manifest.xml` would look like this:

```xml
<webhooks>
<webhook name="appActivate" url="https://app-server.com/app/activate" event="app.activated"/>
<webhook name="appDeactivated" url="https://app-server.com/app/deactivated" event="app.deactivated"/>
<webhook name="appDelete" url="https://app-server.com/app/delete" event="app.deleted"/>
</webhooks>
```

## Usage

The implementation is similar to [Registration](./getting_started.md)
The implementation is similar to [Registration](./01-getting_started.md)
and wraps the RegistrationService to inject only one controller for all lifecycle methods.

```php
$app = new AppConfiguration('Foo', 'test', 'http://localhost:6001/register/callback');
Expand All @@ -32,7 +43,7 @@ $response = match ($_SERVER['REQUEST_URI']) {
'/app/register/confirm' => $lifecycle->registerConfirm($psrRequest),
'/app/activate' => $lifecycle->activate($psrRequest),
'/app/deactivate' => $lifecycle->deactivate($psrRequest),
'/app/uninstall' => $lifecycle->uninstall($psrRequest),
'/app/delete' => $lifecycle->delete($psrRequest),
default => throw new \RuntimeException('Unknown route')
};

Expand Down
2 changes: 1 addition & 1 deletion docs/04-signing.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The signing is required for the following actions:
- Payment


To sign the response, you need to create a `ResponseSigner` and call the `signResponse` method.
To sign the response, you need to create a `ResponseSigner` and call the `signResponse` method with our PSR 7 Response.

```php
$app = new AppConfiguration('Foo', 'test', 'http://localhost:6001/register/callback');
Expand Down
8 changes: 6 additions & 2 deletions docs/05-http-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $httpClient = $clientFactory->createClient($shop);
$response = $httpClient->sendRequest($psrHttpRequest);
```

The client will automatically fetch the oauth token for the shop and add it to the request.
The client will automatically fetch the OAuth2 token for the shop and add it to the request.

## SimpleHttpClient

Expand All @@ -31,7 +31,11 @@ The SimpleHttpClient is a wrapper around the PSR18 ClientInterface and provides
```php
$simpleClient = new \Shopware\App\SDK\HttpClient\SimpleHttpClient\SimpleHttpClient($httpClient);

$simpleClient->get('https://shop.com/api/_info/version');
$response = $simpleClient->get('https://shop.com/api/_info/version');
$response->getHeader('Content-Type'); // application/json
$response->ok(); // true when 200 <= status code < 300
$body = $response->json(); // json decoded body
echo $body['version'];

$simpleClient->post('https://shop.com/api/_action/sync', [
'entity' => 'product',
Expand Down
2 changes: 1 addition & 1 deletion examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
} elseif (str_starts_with($serverRequest->getUri()->getPath(), '/register/callback')) {
send($appLifecycle->registerConfirm($serverRequest));
} elseif (str_starts_with($serverRequest->getUri()->getPath(), '/webhook/app.deleted')) {
send($appLifecycle->uninstall($serverRequest));
send($appLifecycle->delete($serverRequest));
} elseif (str_starts_with($serverRequest->getUri()->getPath(), '/webhook/app.activated')) {
send($appLifecycle->activate($serverRequest));
} elseif (str_starts_with($serverRequest->getUri()->getPath(), '/webhook/app.deactivated')) {
Expand Down
4 changes: 2 additions & 2 deletions src/AppLifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function deactivate(RequestInterface $request): ResponseInterface
}

/**
* Handles the app.uninstalled Hook to remove the shop from the repository
* Handles the app.deleted Hook to remove the shop from the repository
*/
public function uninstall(RequestInterface $request): ResponseInterface
public function delete(RequestInterface $request): ResponseInterface
{
$psrFactory = new Psr17Factory();
$response = $psrFactory->createResponse(204);
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient/SimpleHttpClient/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getContent(): string
/**
* @return array<mixed>
*/
public function toArray(): array
public function json(): array
{
$data = \json_decode($this->getContent(), true, flags: JSON_THROW_ON_ERROR);

Expand Down
8 changes: 4 additions & 4 deletions tests/AppLifecycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testUninstall(): void
{
$this->shopRepository->createShop(new MockShop('123', 'https://foo.com', '1234567890'));

$response = $this->appLifecycle->uninstall(new Request("POST", '/?shop-id=123', [], '{}'));
$response = $this->appLifecycle->delete(new Request("POST", '/?shop-id=123', [], '{}'));
static::assertSame(204, $response->getStatusCode());

static::assertNull($this->shopRepository->getShopFromId('123'));
Expand All @@ -122,7 +122,7 @@ public function testUninstall(): void

public function testUninstallNotExisting(): void
{
$response = $this->appLifecycle->uninstall(new Request("POST", '/?shop-id=123', [], '{}'));
$response = $this->appLifecycle->delete(new Request("POST", '/?shop-id=123', [], '{}'));
static::assertSame(204, $response->getStatusCode());

static::assertNull($this->shopRepository->getShopFromId('123'));
Expand All @@ -141,7 +141,7 @@ public function testUninstallWithoutEventDispatcher(): void
);

$this->shopRepository->createShop(new MockShop('123', 'https://foo.com', '1234567890'));
$response = $appLifeCycle->uninstall(new Request("POST", '/?shop-id=123', [], '{}'));
$response = $appLifeCycle->delete(new Request("POST", '/?shop-id=123', [], '{}'));
static::assertSame(204, $response->getStatusCode());

static::assertNull($this->shopRepository->getShopFromId('123'));
Expand All @@ -165,7 +165,7 @@ public function testUninstallLogs(): void
null
);

$response = $appLifeCycle->uninstall(new Request("POST", '/?shop-id=123', [], '{}'));
$response = $appLifeCycle->delete(new Request("POST", '/?shop-id=123', [], '{}'));
static::assertSame(204, $response->getStatusCode());
}

Expand Down
4 changes: 2 additions & 2 deletions tests/HttpClient/SimpleHttpClient/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testResponse(): void
static::assertSame(200, $response->getStatusCode());
static::assertSame('application/json', $response->getHeader('Content-Type'));
static::assertSame('{"foo": "bar", "baz": 1}', $response->getContent());
static::assertSame(['foo' => 'bar', 'baz' => 1], $response->toArray());
static::assertSame(['foo' => 'bar', 'baz' => 1], $response->json());
static::assertTrue($response->ok());
static::assertSame($raw, $response->getRawResponse());
}
Expand All @@ -33,7 +33,7 @@ public function testNonArrayResponse(): void
static::assertSame('application/json', $response->getHeader('Content-Type'));
static::expectException(\RuntimeException::class);
static::expectExceptionMessage('Response is not a valid JSON array');
$response->toArray();
$response->json();
}
/**
* @dataProvider okDataProvider
Expand Down
2 changes: 1 addition & 1 deletion tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testGetJSON(): void
$resp = $simpleHttpClient->get('https://example.com');

static::assertTrue($resp->ok());
static::assertSame(['foo' => 'bar'], $resp->toArray());
static::assertSame(['foo' => 'bar'], $resp->json());
}

#[DataProvider('provideMethods')]
Expand Down

0 comments on commit d2ff1cf

Please sign in to comment.