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

Added missing segments API calls (v11) #178

Merged
merged 51 commits into from
Dec 8, 2023
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
739bbe5
added segments api calls
rcerljenko Nov 29, 2023
f1b309e
php cs fixer fix
rcerljenko Nov 29, 2023
d37c6bc
moved to phpdoc payload array
rcerljenko Nov 30, 2023
32cde31
fix erros
rcerljenko Nov 30, 2023
21b93b9
fix errors
rcerljenko Nov 30, 2023
7bf3d11
introduce dto
rcerljenko Nov 30, 2023
0ce5531
introduce dto
rcerljenko Nov 30, 2023
87924c6
introduce dto
rcerljenko Nov 30, 2023
bd3440a
fixes
rcerljenko Nov 30, 2023
15ce5cb
removed general purpose pagination dto
rcerljenko Nov 30, 2023
2cbd763
requested changes
rcerljenko Nov 30, 2023
88a9a0d
requested changes
rcerljenko Nov 30, 2023
45ce096
requested changes
rcerljenko Nov 30, 2023
30f411a
requested changes
rcerljenko Nov 30, 2023
310f776
requested changes
rcerljenko Nov 30, 2023
2b73275
requested changes
rcerljenko Nov 30, 2023
59b8b9d
Merge branch 'master' into feature/segments
rcerljenko Nov 30, 2023
ec753de
removed []
rcerljenko Nov 30, 2023
c40dfe9
Merge branch 'feature/segments' of https://github.com/rcerljenko/ones…
rcerljenko Nov 30, 2023
ae0120e
added filters dto
rcerljenko Nov 30, 2023
a9ef92b
added filters dto
rcerljenko Nov 30, 2023
4b00629
added filters dto
rcerljenko Nov 30, 2023
9b52286
fixes
rcerljenko Nov 30, 2023
6285d84
added missing relation key
rcerljenko Dec 1, 2023
177f1a8
moved value to extra params
rcerljenko Dec 1, 2023
95d5307
added relation constants
rcerljenko Dec 1, 2023
19a01fc
introduce separate dtos for every filter type
rcerljenko Dec 1, 2023
079542e
renamed filters
rcerljenko Dec 1, 2023
d039ac9
change request
rcerljenko Dec 4, 2023
171a390
change request
rcerljenko Dec 4, 2023
22b53da
namespace change to singular
rcerljenko Dec 4, 2023
e3c5be8
namespace change to singular
rcerljenko Dec 4, 2023
4e8157f
move operands to abstract class
rcerljenko Dec 4, 2023
7dec6e5
null check filters
rcerljenko Dec 4, 2023
4b26aaf
fix
rcerljenko Dec 4, 2023
72c928e
added response objects
rcerljenko Dec 5, 2023
71ba641
added response objects
rcerljenko Dec 5, 2023
9abc5e3
added exception handling
rcerljenko Dec 5, 2023
3f75b40
added exception handling
rcerljenko Dec 5, 2023
ec9ad0d
added exception handling
rcerljenko Dec 5, 2023
e0587b5
added request and response to exception
rcerljenko Dec 5, 2023
5fb5974
added request and response to exception - fixes
rcerljenko Dec 5, 2023
2a91971
added separate makeRequest method
rcerljenko Dec 5, 2023
3566e53
added separate makeRequest method
rcerljenko Dec 5, 2023
e67cd5d
added separate makeRequest method
rcerljenko Dec 5, 2023
c721474
added separate makeRequest method
rcerljenko Dec 5, 2023
2337167
added list and delete segments tests
rcerljenko Dec 8, 2023
1d81fca
added requested changes
rcerljenko Dec 8, 2023
4d5caf6
removed unused method
rcerljenko Dec 8, 2023
ccc897d
changes
rcerljenko Dec 8, 2023
0886d6e
added test for create segment
rcerljenko Dec 8, 2023
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
Prev Previous commit
Next Next commit
added response objects
rcerljenko committed Dec 5, 2023
commit 72c928e661c7bac1268d76e1d58ad1765b6e4587
10 changes: 10 additions & 0 deletions src/Response/AbstractResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace OneSignal\Response;

interface AbstractResponse
{
public static function makeFromRequest(array $request): self;
}
44 changes: 44 additions & 0 deletions src/Response/Segment/CreateSegmentResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace OneSignal\Response\Segment;

use OneSignal\Response\AbstractResponse;

class CreateSegmentResponse implements AbstractResponse
{
protected bool $success;

/**
* @var non-empty-string
*/
protected string $id;

/**
* @param non-empty-string $id
*/
public function __construct(bool $success, string $id)
rcerljenko marked this conversation as resolved.
Show resolved Hide resolved
{
$this->success = $success;
$this->id = $id;
}

public static function makeFromRequest(array $request): self
{
return new static(

Check failure on line 29 in src/Response/Segment/CreateSegmentResponse.php

GitHub Actions / PHPStan

Unsafe usage of new static().
$request['success'],
$request['id']
);
}

public function getSuccess(): bool
{
return $this->success;
}

public function getId(): string
{
return $this->id;
}
}
29 changes: 29 additions & 0 deletions src/Response/Segment/DeleteSegmentResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace OneSignal\Response\Segment;

use OneSignal\Response\AbstractResponse;

class DeleteSegmentResponse implements AbstractResponse
{
protected bool $success;

public function __construct(bool $success)
{
$this->success = $success;
}

public static function makeFromRequest(array $request): self
{
return new static(

Check failure on line 20 in src/Response/Segment/DeleteSegmentResponse.php

GitHub Actions / PHPStan

Unsafe usage of new static().
$request['success']
);
}

public function getSuccess(): bool
{
return $this->success;
}
}
92 changes: 92 additions & 0 deletions src/Response/Segment/ListSegmentsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace OneSignal\Response\Segment;

use OneSignal\Response\AbstractResponse;

class ListSegmentsResponse implements AbstractResponse
{
/**
* @var int<0, 2147483648>
*/
protected int $totalCount;

/**
* @var int<0, 2147483648>
*/
protected int $offset;

/**
* @var int<0, 2147483648>
*/
protected int $limit;

/**
* @var list<Segment>
*/
protected array $segments;

/**
* @param int<0, 2147483648> $totalCount
* @param int<0, 2147483648> $limit
* @param int<0, 2147483648> $offset
* @param list<Segment> $segments
*/
public function __construct(int $totalCount, int $offset, int $limit, array $segments)
{
$this->totalCount = $totalCount;
$this->offset = $offset;
$this->limit = $limit;
$this->segments = $segments;
}

public static function makeFromRequest(array $request): self
{
$segments = array_map(
static function (array $segment): Segment {
return new Segment(
$segment['id'],
$segment['name'],
$segment['created_at'],
$segment['updated_at'],
$segment['app_id'],
$segment['read_only'],
$segment['is_active'],
);
},
$request['segments']
);

return new static(

Check failure on line 62 in src/Response/Segment/ListSegmentsResponse.php

GitHub Actions / PHPStan

Unsafe usage of new static().
$request['total_count'],
$request['offset'],
$request['limit'],
$segments
);
}

public function getTotalCount(): int
{
return $this->totalCount;
}

public function getOffset(): int
{
return $this->offset;
}

public function getLimit(): int
{
return $this->limit;
}

/**
* @return list<Segment>
*/
public function getSegments(): array
{
return $this->segments;
}
}
93 changes: 93 additions & 0 deletions src/Response/Segment/Segment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace OneSignal\Response\Segment;

use DateTimeImmutable;

class Segment
{
/**
* @var non-empty-string
*/
protected string $id;

/**
* @var non-empty-string
*/
protected string $name;

protected DateTimeImmutable $createdAt;

protected DateTimeImmutable $updatedAt;

/**
* @var non-empty-string
*/
protected string $appId;

protected bool $readOnly;

protected bool $isActive;

/**
* @param non-empty-string $id
* @param non-empty-string $name
* @param non-empty-string $createdAt
* @param non-empty-string $updatedAt
* @param non-empty-string $appId
*/
public function __construct(
string $id,
string $name,
string $createdAt,
string $updatedAt,
string $appId,
bool $readOnly,
bool $isActive
) {
$this->id = $id;
$this->name = $name;
$this->createdAt = new DateTimeImmutable($createdAt);
$this->updatedAt = new DateTimeImmutable($updatedAt);
rcerljenko marked this conversation as resolved.
Show resolved Hide resolved
$this->appId = $appId;
$this->readOnly = $readOnly;
$this->isActive = $isActive;
}

public function getId(): string
{
return $this->id;
}

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

public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}

public function getUpdatedAt(): DateTimeImmutable
{
return $this->updatedAt;
}

public function getAppId(): string
{
return $this->appId;
}

public function getReadOnly(): bool
{
return $this->readOnly;
}

public function getIsActive(): bool
{
return $this->isActive;
}
}
15 changes: 9 additions & 6 deletions src/Segments.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@

use OneSignal\Dto\Segment\CreateSegment;
use OneSignal\Dto\Segment\ListSegments;
use OneSignal\Response\Segment\CreateSegmentResponse;
use OneSignal\Response\Segment\DeleteSegmentResponse;
use OneSignal\Response\Segment\ListSegmentsResponse;

class Segments extends AbstractApi
{
@@ -19,22 +22,22 @@ public function __construct(OneSignal $client)
*
* Application authentication key and ID must be set.
*/
public function list(ListSegments $listSegmentsDto): array
public function list(ListSegments $listSegmentsDto): ListSegmentsResponse
{
$appId = $this->client->getConfig()->getApplicationId();

$request = $this->createRequest('GET', '/apps/'.$appId.'/segments?'.http_build_query($listSegmentsDto->toArray()));
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");

return $this->client->sendRequest($request);
return ListSegmentsResponse::makeFromRequest($this->client->sendRequest($request));
}

/**
* Create new segment with provided data.
*
* Application authentication key and ID must be set.
*/
public function create(CreateSegment $createSegmentDto): array
public function create(CreateSegment $createSegmentDto): CreateSegmentResponse
{
$appId = $this->client->getConfig()->getApplicationId();

@@ -43,7 +46,7 @@ public function create(CreateSegment $createSegmentDto): array
$request = $request->withHeader('Content-Type', 'application/json');
$request = $request->withBody($this->createStream($createSegmentDto->toArray()));

return $this->client->sendRequest($request);
return CreateSegmentResponse::makeFromRequest($this->client->sendRequest($request));
}

/**
@@ -53,13 +56,13 @@ public function create(CreateSegment $createSegmentDto): array
*
* @param non-empty-string $id Segment ID
*/
public function delete(string $id): array
public function delete(string $id): DeleteSegmentResponse
{
$appId = $this->client->getConfig()->getApplicationId();

$request = $this->createRequest('DELETE', '/apps/'.$appId.'/segments/'.$id);
$request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");

return $this->client->sendRequest($request);
return DeleteSegmentResponse::makeFromRequest($this->client->sendRequest($request));
}
}