Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cl 8956 add new partner api methods #19

Merged
merged 11 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-phpunit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
- name: Install dependencies
uses: php-actions/composer@v6
- name: Run tests
uses: php-actions/phpunit@v3
uses: php-actions/phpunit@v9
6 changes: 6 additions & 0 deletions USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ You can create personalised messages using template variables. To do this you ca
The Close app can be used to provide digital tickets to event-visitors. Using the Close PHP SDK you can both import and cancel tickets.


### carousel
| Operations | Use-case |
|-------------------------------------------------------------------------------|----------------------------------------------------------|
| [getCarousel(eventId, text)](/examples/carousel/getCarousel.md) | Use when you need to lookup a carousel by carousel name. |
| [createCarousel(eventId, chatId, text)](/examples/carousel/createCarousel.md) | Use when you need to create a carousel for an event. |

| Operation | Use-case |
| -------- | ----------- |
|[import(eventId,ticketgroup)](/examples/ticket/import.md)| Use when you want to import a ticket.|
Expand Down
30 changes: 30 additions & 0 deletions examples/carousel/createCarousel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Sending a message to all users in all chats for one event.
Do you want to send a message to all users going to a specific event? Then you can use the sendToAllChatsForEvent() operation.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this text should change? It is about creating the carousel, not sending a message. The one below (the lookup one) should also change.



##### example:
```php
<?php
use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Exception\CloseSdkException;

try {
// Define DTO structure
$eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
$name = 'Awesome Carousel';

$sdk
->event()
->createCarousel($eventId, $name)
} catch (CloseSdkException $e) {
// error
}
```
##### DTOs explained:
| DTO | info |
|---------|----------------------------------------------------------|
| EventId | Identifies one specific event. Always starts with "CLEV" |
| Name | Name of the carousel. Must be unique per event. |

[Back to User Guide](/USERGUIDE.md#carousel)
30 changes: 30 additions & 0 deletions examples/carousel/lookupCarousel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Sending a message to all users in all chats for one event.
Do you want to send a message to all users going to a specific event? Then you can use the sendToAllChatsForEvent() operation.


##### example:
```php
<?php
use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Exception\CloseSdkException;

try {
// Define DTO structure
$eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
$name = 'Awesome Carousel';

$sdk
->event()
->lookupCarousel($eventId, $name)
} catch (CloseSdkException $e) {
// error
}
```
##### DTOs explained:
| DTO | info |
|---------|----------------------------------------------------------|
| EventId | Identifies one specific event. Always starts with "CLEV" |
| Name | Name of the carousel. |

[Back to User Guide](/USERGUIDE.md#carousel)
32 changes: 32 additions & 0 deletions src/Dto/Carousel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ClosePartnerSdk\Dto;

class Carousel
{
protected string $name;
protected string $public_id;

/**
* @param string $name
* @param string $public_id
*/
public function __construct(string $name, string $public_id)
{
$this->name = $name;
$this->public_id = $public_id;
}

public function getName(): string
{
return $this->name;
}

public static function buildFromResponseObject(\StdClass $obj): self
{
return new self(
$obj->name,
$obj->id,
);
}
}
53 changes: 48 additions & 5 deletions src/Operation/EventOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

namespace ClosePartnerSdk\Operation;

use ClosePartnerSdk\Dto\Carousel;
use ClosePartnerSdk\Dto\Event;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\EventTime;
use ClosePartnerSdk\HttpClient\Message\RequestBodyMediator;
use DateTimeInterface;
use JsonException;

final class EventOperation extends CloseOperation
{
/**
* @return Event[]
* @throws \Http\Client\Exception
* @throws \JsonException
* @throws JsonException
*/
public function getEvents(): array
{
Expand All @@ -39,7 +42,7 @@ public function getEvents(): array
* @param EventId $eventId
* @return Event
* @throws \Http\Client\Exception
* @throws \JsonException
* @throws JsonException
*/
public function getEvent(EventId $eventId): Event
{
Expand All @@ -59,7 +62,7 @@ public function getEvent(EventId $eventId): Event
* @param array $updates
* @return Event
* @throws \Http\Client\Exception
* @throws \JsonException
* @throws JsonException
*/
public function updateEvent(EventId $eventId, array $updates): Event
{
Expand All @@ -82,7 +85,7 @@ public function updateEvent(EventId $eventId, array $updates): Event
* @param EventId $eventId
* @return Event
* @throws \Http\Client\Exception
* @throws \JsonException
* @throws JsonException
*/
public function copyEvent(EventId $eventId): Event
{
Expand All @@ -102,14 +105,15 @@ public function copyEvent(EventId $eventId): Event
* @param EventTime $eventTime;
* @return Event
* @throws \Http\Client\Exception
* @throws \JsonException
* @throws JsonException
*/
public function cloneEvent(EventId $eventId, EventTime $eventTime): Event
{
$response = $this->sdk
->getHttpClient()
->post(
$this->buildUriWithLatestVersion('/events/' . $eventId . '/clone'),
[],
[
'start_date_time' => $eventTime->getStartDateTime()->format(DateTimeInterface::W3C)
]
Expand All @@ -118,4 +122,43 @@ public function cloneEvent(EventId $eventId, EventTime $eventTime): Event
$obj = json_decode($response->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);
return Event::buildFromRepsonseObject($obj);
}

/**
* @param EventId $eventId
* @param string $name
* @return Carousel
* @throws JsonException
*/
public function createCarousel(EventId $eventId, string $name): Carousel
{
$response = $this->sdk
->getHttpClient()
->post(
$this->buildUriWithLatestVersion('/events/' . $eventId . '/carousels'),
[],
json_encode(['name' => $name])
);
$obj = json_decode($response->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);
return Carousel::buildFromResponseObject($obj);
}

/**
* @param EventId $eventId
* @param string $name
* @return Carousel
* @throws JsonException
*/
public function lookupCarousel(EventId $eventId, string $name): Carousel
{
$response = $this->sdk
->getHttpClient()
->get(
$this->buildUriWithLatestVersion('/events/' . $eventId . '/carousels?name=' . $name),
[],
);

$obj = json_decode($response->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);

return Carousel::buildFromResponseObject($obj);
}
}
92 changes: 92 additions & 0 deletions tests/Endpoint/CreateCarouselForEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);

namespace ClosePartnerSdk\Tests\Endpoint;

use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\Mapper\CancelTicketMapper;
use ClosePartnerSdk\Dto\Mapper\SendMessageMapper;
use ClosePartnerSdk\Exception\Auth\InvalidCredentialsException;
use ClosePartnerSdk\Exception\InvalidRequestJsonFormat;
use ClosePartnerSdk\Tests\Factory\Dto\TicketCancelFactory;
use Http\Client\Common\Exception\ClientErrorException;
use Http\Message\RequestMatcher\RequestMatcher;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class CreateCarouselForEventTest extends EndpointTestCase
{
/** @test */
public function it_does_not_have_the_required_field()
{
$token = 'ey8393930dkdkdk';
$this->mockClient
->on(
new RequestMatcher('oauth/token'),
fn() => $this->mockResponse([
"token_type" => "Bearer",
"expires_in" => 1113,
"access_token" => $token,
])
);
$eventId = new EventId('1234');
$this->mockClient
->on(
new RequestMatcher('events/'.$eventId.'/carousels'),
function (RequestInterface $request) {
$response = $this->mockResponse([]);
$response
->method('getStatusCode')
->willReturn(422);

self::assertEquals(
422,
$response->getStatusCode()
);
});

$this->expectException(\Throwable::class);

$this->givenSdk()->event()->createCarousel(
$eventId,
'',
);
}

/** @test */
public function it_creates_a_new_carousel_for_the_event()
{
$token = 'ey8393930dkdkdk';
$this->mockClient
->on(
new RequestMatcher('oauth/token'),
fn() => $this->mockResponse([
"token_type" => "Bearer",
"expires_in" => 1113,
"access_token" => $token,
])
);

$eventId = new EventId('1234');

$this->mockClient
->on(
new RequestMatcher('events/'.$eventId.'/carousels'),
function (RequestInterface $request) {
$response = json_decode($request->getBody()->getContents(), true);
$response['id'] = 'CLOC1234567890';
self::assertEquals(
'Carousel 1',
$response['name']
);
return $this->mockResponse($response);
});


$this->givenSdk()->event()->createCarousel(
$eventId,
'Carousel 1',
);
}
}