Skip to content

Commit

Permalink
[Core] add for{ObjectType}Id Support (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: onairmarc <onairmarc@users.noreply.github.com>
  • Loading branch information
onairmarc and onairmarc authored Dec 1, 2024
1 parent cd539e6 commit c29a639
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 46 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ de929a45798d68147fec3ab9527979a5e5f0fce1
c90d7383de6e86260b6cc997b3d0af8e64826a11
baa146cd798ade580c6a6f4f2cbf98d5b72cc19f
cbe36f817a31ddbeb1ef59120b2406f6e9d4a689
8bc4a90640e6ca09b522425509a0a147837a83e2
40 changes: 38 additions & 2 deletions src/Objects/Calendar/EventInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use EncoreDigitalGroup\PlanningCenter\Support\PlanningCenterApiVersion;
use EncoreDigitalGroup\PlanningCenter\Support\RelationshipMapper;
use EncoreDigitalGroup\PlanningCenter\Traits\HasPlanningCenterClient;
use EncoreDigitalGroup\StdLib\Exceptions\NullExceptions\NullException;

/** @api */
class EventInstance
Expand All @@ -36,10 +37,39 @@ public static function make(string $clientId, string $clientSecret): EventInstan
return $event;
}

public function forEventInstanceId(string $eventInstanceId): static
{
$this->attributes->eventInstanceId = $eventInstanceId;

return $this;
}

public function forEventId(string $eventId): static
{
$this->setupEventRelationship();

if ($this->relationships->event !== null && $this->relationships->event->data !== null) {
$this->relationships->event->data->id = $eventId;
}

return $this;
}

public function all(array $query = []): ClientResponse
{
$this->relationships->event = $this->relationships->event ?? new BasicRelationship;
$this->relationships->event->data = $this->relationships->event->data ?? new BasicRelationshipData;
$this->setupEventRelationship();

if ($this->relationships->event === null) {
throw new NullException("relationships->event");
}

if ($this->relationships->event->data === null) {
throw new NullException("relationships->event->data");
}

if ($this->relationships->event->data->id === null) {
throw new NullException("relationships->event->data->id");
}

$http = $this->client()
->get($this->hostname() . Event::EVENT_ENDPOINT . "/{$this->relationships->event->data->id}/event_instances", $query);
Expand Down Expand Up @@ -88,4 +118,10 @@ private function mapFromPco(mixed $pco): void

RelationshipMapper::from($pco, $this->relationships, $relationshipMap);
}

private function setupEventRelationship(): void
{
$this->relationships->event = $this->relationships->event ?? new BasicRelationship;
$this->relationships->event->data = $this->relationships->event->data ?? new BasicRelationshipData;
}
}
14 changes: 7 additions & 7 deletions src/Objects/Calendar/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ public static function make(string $clientId, string $clientSecret): Tag
return $tagGroup;
}

public function all(array $query = []): ClientResponse
public function forTagGroupId(string $tagGroupId): static
{
$http = $this->client()
->get($this->hostname() . TagGroup::TAG_GROUP_ENDPOINT . "/{$this->tagGroupId}/tags", $query);
$this->tagGroupId = $tagGroupId;

return $this->processResponse($http);
return $this;
}

public function inTagGroup(string $tagGroupId): static
public function all(array $query = []): ClientResponse
{
$this->tagGroupId = $tagGroupId;
$http = $this->client()
->get($this->hostname() . TagGroup::TAG_GROUP_ENDPOINT . "/{$this->tagGroupId}/tags", $query);

return $this;
return $this->processResponse($http);
}

/** @internal */
Expand Down
9 changes: 8 additions & 1 deletion src/Objects/Calendar/TagGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public static function make(string $clientId, string $clientSecret): TagGroup
return $tagGroup;
}

public function forTagGroupId(string $tagGroupId): static
{
$this->attributes->tagGroupId = $tagGroupId;

return $this;
}

public function all(array $query = []): ClientResponse
{
$http = $this->client()
Expand All @@ -40,7 +47,7 @@ public function all(array $query = []): ClientResponse
public function tags(array $query = []): ClientResponse
{
return Tag::make($this->clientId, $this->clientSecret)
->inTagGroup($this->attributes->tagGroupId)
->forTagGroupId($this->attributes->tagGroupId)
->all($query);
}

Expand Down
13 changes: 12 additions & 1 deletion src/Objects/Groups/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public static function make(string $clientId, string $clientSecret): Event
return $event;
}

public function forEventId(string $eventId): static
{
$this->attributes->eventId = $eventId;

return $this;
}

public function all(array $query = []): ClientResponse
{
$http = $this->client()
Expand All @@ -56,7 +63,11 @@ public function get(array $query = []): ClientResponse

private function mapFromPco(mixed $pco): void
{
$pco = objectify($pco);
$pco = pco_objectify($pco);

if (is_null($pco)) {
return;
}

$attributeMap = [
"eventId" => "id",
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/Groups/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function people(array $query = []): ClientResponse
public function tags(array $query = []): ClientResponse
{
return Tag::make($this->clientId, $this->clientSecret)
->forGroup($this->attributes->groupId)
->forGroupId($this->attributes->groupId)
->groups($query);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Objects/Groups/GroupEnrollment.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public static function make(string $clientId, string $clientSecret): GroupEnroll
return $group;
}

public function forGroupId(string $groupId): static
{
$this->attributes->groupId = $groupId;

return $this;
}

public function get(array $query = []): ClientResponse
{
$http = $this->client()
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/Groups/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function make(string $clientId, string $clientSecret): Tag
return $tag;
}

public function forGroup(string $groupId): static
public function forGroupId(string $groupId): static
{
$this->groupId = $groupId;

Expand Down
16 changes: 15 additions & 1 deletion src/Objects/People/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ public static function make(?string $clientId = null, ?string $clientSecret = nu
return $email;
}

public function forEmailAddressId(string $emailAddressId): static
{
$this->attributes->emailAddressId = $emailAddressId;

return $this;
}

public function forPersonId(string $personId): static
{
$this->attributes->personId = $personId;

return $this;
}

public function get(): ClientResponse
{
$http = $this->client()
Expand All @@ -39,7 +53,7 @@ public function get(): ClientResponse
return $this->processResponse($http);
}

public function forPerson(): ClientResponse
public function person(): ClientResponse
{
$http = $this->client()
->get($this->hostname() . Person::PEOPLE_ENDPOINT . "/{$this->attributes->personId}/emails");
Expand Down
8 changes: 7 additions & 1 deletion src/Objects/People/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ private function mapToPco(): array
],
];

unset($person["data"]["attributes"]["id"], $person["data"]["attributes"]["created_at"], $person["data"]["attributes"]["updated_at"], $person["data"]["attributes"]["name"], $person["data"]["attributes"]["demographic_avatar_url"]);
unset(
$person["data"]["attributes"]["id"],
$person["data"]["attributes"]["created_at"],
$person["data"]["attributes"]["updated_at"],
$person["data"]["attributes"]["name"],
$person["data"]["attributes"]["demographic_avatar_url"]
);

return Arr::whereNotNull($person["data"]["attributes"]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/People/Traits/HasEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function email(): ClientResponse
$email = Email::make($this->clientId, $this->clientSecret);
$email->attributes->personId = $this->attributes->personId;

return $email->forPerson();
return $email->person();
}
}
11 changes: 7 additions & 4 deletions tests/Unit/Calendar/EventInstanceTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
describe("Calendar EventInstance Tests", function (): void {
test("EventInstance: Can Get EventInstance By ID", function (): void {
$eventInstance = EventInstance::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);
$eventInstance->attributes->eventInstanceId = "1";

$response = $eventInstance->get();
$response = $eventInstance
->forEventInstanceId("1")
->get();

/** @var EventInstance $calendarEventInstance */
$calendarEventInstance = $response->data->first();

Expand All @@ -23,9 +25,10 @@

test("EventInstance: Can List All EventInstances", function (): void {
$eventInstance = EventInstance::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);
$eventInstance->relationships->event->data->id = "1";

$response = $eventInstance->all();
$response = $eventInstance
->forEventId("1")
->all();

expect($response)->toBeInstanceOf(ClientResponse::class)
->and($response->data)->toBeInstanceOf(Collection::class)
Expand Down
13 changes: 10 additions & 3 deletions tests/Unit/Calendar/EventTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
test("Event: Can Get Event By ID", function (): void {
$event = Event::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);

$response = $event->forEventId("1")->get();
$response = $event
->forEventId("1")
->get();

/** @var Event $calendarEvent */
$calendarEvent = $response->data->first();

Expand All @@ -35,7 +38,9 @@
test("Event: Can List All Event Instances For A Specific Event", function (): void {
$event = Event::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);

$response = $event->forEventId("1")->instances();
$response = $event
->forEventId("1")
->instances();

/** @var EventInstance $eventInstance */
$eventInstance = $response->data->first();
Expand All @@ -50,7 +55,9 @@
test("Event: Can List All Tags Associated with an Event", function (): void {
$event = Event::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);

$response = $event->forEventId("1")->tags();
$response = $event
->forEventId("1")
->tags();

/** @var Event $calendarEvent */
$calendarEvent = $response->data->first();
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Calendar/TagGroupTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@

test("TagGroup: Can List All Tags For A Specific TagGroup", function (): void {
$tagGroup = TagGroup::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);
$tagGroup->attributes->tagGroupId = "1";

$response = $tagGroup->tags();
$response = $tagGroup
->forTagGroupId("1")
->tags();

/** @var TagAttributes $tagAttributes */
$tagAttributes = $response->data->first()->attributes;
Expand Down
26 changes: 16 additions & 10 deletions tests/Unit/Groups/GroupTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@

test("Group: Can Get Group By ID", function (): void {
$group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);
$group->attributes->groupId = "1";

$response = $group->get();
$response = $group
->forGroupId("1")
->get();

/** @var GroupAttributes $groupAttributes */
$groupAttributes = $response->data->first()->attributes;

Expand All @@ -54,9 +56,10 @@

test("Group: Can List Group Memberships", function (): void {
$group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);
$group->attributes->groupId = "1";

$response = $group->membership();
$response = $group
->forGroupId("1")
->membership();

/** @var GroupMembership $membership */
$membership = $response->data->first();
Expand All @@ -71,9 +74,10 @@

test("Group: Can List Group People", function (): void {
$group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);
$group->attributes->groupId = "1";

$response = $group->people();
$response = $group
->forGroupId("1")
->people();

/** @var GroupMemberPersonAttributes $groupMember */
$groupMember = $response->data->first()->attributes;
Expand All @@ -88,9 +92,10 @@

test("Group: Can List Tags Assigned to Group", function (): void {
$group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);
$group->attributes->groupId = "1";

$response = $group->tags();
$response = $group
->forGroupId("1")
->tags();

/** @var Tag $tag */
$tag = $response->data->first();
Expand All @@ -105,9 +110,10 @@

test("Group: Can Get Group Enrollment Details", function (): void {
$group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET);
$group->attributes->groupId = "1";

$response = $group->enrollment();
$response = $group
->forGroupId("1")
->enrollment();

/** @var GroupEnrollmentAttributes $enrollmentAttributes */
$enrollmentAttributes = $response->data->first()->attributes;
Expand Down
Loading

0 comments on commit c29a639

Please sign in to comment.