generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Organizations view/list endpoints (#25)
- Loading branch information
Showing
20 changed files
with
768 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Entities; | ||
|
||
class Member | ||
{ | ||
public function __construct( | ||
public readonly string $pk, | ||
public readonly string $email, | ||
public readonly int $last_active, | ||
public readonly bool $twoFactorAuthentication, | ||
public readonly bool $status, | ||
public readonly Permission $permission, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Entities; | ||
|
||
class OrganizationLimit | ||
{ | ||
public function __construct( | ||
public readonly int $count, | ||
public readonly ?int $max, | ||
public readonly ?float $price, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Entities; | ||
|
||
class Permission | ||
{ | ||
public function __construct( | ||
public readonly int $level, | ||
public readonly string $printable, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Factories; | ||
|
||
use Rapkis\Controld\Contracts\Factories\Factory; | ||
use Rapkis\Controld\Entities\Member; | ||
use Rapkis\Controld\Entities\Permission; | ||
|
||
class MemberFactory implements Factory | ||
{ | ||
public function make(array $data): Member | ||
{ | ||
return new Member( | ||
pk: $data['PK'], | ||
email: $data['email'], | ||
last_active: (int) $data['last_active'], | ||
twoFactorAuthentication: (bool) $data['twofa'], | ||
status: (bool) $data['status'], | ||
permission: new Permission( | ||
level: (int) $data['permission']['level'], | ||
printable: $data['permission']['printable'], | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Factories; | ||
|
||
use Carbon\CarbonImmutable; | ||
use Rapkis\Controld\Contracts\Factories\Factory; | ||
use Rapkis\Controld\Entities\OrganizationLimit; | ||
use Rapkis\Controld\Responses\Organization; | ||
|
||
class OrganizationFactory implements Factory | ||
{ | ||
public function make(array $data): Organization | ||
{ | ||
if (! empty($data['parent_profile'])) { | ||
$parentProfile = (new ProfileFactory())->make($data['parent_profile']); | ||
} | ||
|
||
return new Organization( | ||
pk: $data['PK'], | ||
parentOrganizationPk: $data['parent_org']['PK'] ?? null, | ||
parentOrganizationName: $data['parent_org']['name'] ?? null, | ||
parentProfile: $parentProfile ?? null, | ||
status: (bool) $data['status'], | ||
name: $data['name'], | ||
contactEmail: $data['contact_email'], | ||
contactName: $data['contact_name'] ?? '', | ||
contactPhone: $data['contact_phone'] ?? '', | ||
website: $data['website'] ?? '', | ||
address: $data['address'] ?? '', | ||
statsEndpoint: $data['stats_endpoint'], | ||
twoFactorAuthenticationRequired: (bool) ($data['twofa_req'] ?? null), | ||
date: CarbonImmutable::make($data['date']), | ||
priceUsers: (float) ($data['price_users'] ?? null), | ||
maxLegacyResolvers: (int) $data['max_legacy_resolvers'], | ||
maxProfiles: (int) $data['max_profiles'], | ||
maxUsers: (int) $data['max_users'], | ||
maxRouters: (int) $data['max_routers'], | ||
maxSubOrganizations: (int) ($data['max_sub_orgs'] ?? null), | ||
members: $this->makeOrganizationLimit($data, 'members'), | ||
profiles: $this->makeOrganizationLimit($data, 'profiles'), | ||
users: $this->makeOrganizationLimit($data, 'users'), | ||
routers: $this->makeOrganizationLimit($data, 'routers'), | ||
subOrganizations: $this->makeOrganizationLimit($data, 'sub_organizations'), | ||
); | ||
} | ||
|
||
private function makeOrganizationLimit(array $data, string $type): OrganizationLimit | ||
{ | ||
return new OrganizationLimit( | ||
count: (int) ($data[$type]['count'] ?? null), | ||
max: ! empty($data[$type]['max']) ? (int) $data[$type]['max'] : null, | ||
price: ! empty($data[$type]['price']) ? (float) $data[$type]['price'] : null, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Resources; | ||
|
||
use Illuminate\Http\Client\PendingRequest; | ||
use Rapkis\Controld\Factories\MemberFactory; | ||
use Rapkis\Controld\Factories\OrganizationFactory; | ||
use Rapkis\Controld\Responses\Members; | ||
use Rapkis\Controld\Responses\Organization; | ||
|
||
class Organizations | ||
{ | ||
public function __construct( | ||
private readonly PendingRequest $client, | ||
private readonly OrganizationFactory $organization, | ||
private readonly MemberFactory $member, | ||
) { | ||
} | ||
|
||
public function organization(): Organization | ||
{ | ||
return $this->organization->make( | ||
$this->client->get('organizations/organization')->json('body.organization') | ||
); | ||
} | ||
|
||
public function members(): Members | ||
{ | ||
$response = $this->client->get('organizations/members')->json('body.members'); | ||
|
||
$result = new Members(); | ||
|
||
foreach ($response as $member) { | ||
$member = $this->member->make($member); | ||
$result->put($member->pk, $member); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function subOrganizations(): \Rapkis\Controld\Responses\Organizations | ||
{ | ||
$response = $this->client->get('organizations/sub_organizations')->json('body.sub_organizations'); | ||
|
||
$result = new \Rapkis\Controld\Responses\Organizations(); | ||
|
||
foreach ($response as $organization) { | ||
$organization = $this->organization->make($organization); | ||
$result->put($organization->pk, $organization); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Responses; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class Members extends Collection | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Responses; | ||
|
||
use DateTimeInterface; | ||
use Rapkis\Controld\Entities\OrganizationLimit; | ||
|
||
class Organization | ||
{ | ||
public function __construct( | ||
public readonly string $pk, | ||
public readonly ?string $parentOrganizationPk, | ||
public readonly ?string $parentOrganizationName, | ||
public readonly ?Profile $parentProfile, | ||
public readonly bool $status, | ||
public readonly string $name, | ||
public readonly string $contactEmail, | ||
public readonly string $contactName, | ||
public readonly string $contactPhone, | ||
public readonly string $website, | ||
public readonly string $address, | ||
public readonly string $statsEndpoint, | ||
public readonly bool $twoFactorAuthenticationRequired, | ||
public readonly DateTimeInterface $date, | ||
public readonly float $priceUsers, | ||
public readonly int $maxLegacyResolvers, | ||
public readonly int $maxProfiles, | ||
public readonly int $maxUsers, | ||
public readonly int $maxRouters, | ||
public readonly int $maxSubOrganizations, | ||
public readonly OrganizationLimit $members, | ||
public readonly OrganizationLimit $profiles, | ||
public readonly OrganizationLimit $users, | ||
public readonly OrganizationLimit $routers, | ||
public readonly OrganizationLimit $subOrganizations, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Responses; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class Organizations extends Collection | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rapkis\Controld\Entities\Member; | ||
use Rapkis\Controld\Entities\Permission; | ||
use Rapkis\Controld\Factories\MemberFactory; | ||
|
||
it('builds a organization', function (array $data, Member $expected) { | ||
expect((new MemberFactory())->make($data))->toEqual($expected); | ||
})->with([ | ||
[ | ||
[ | ||
'PK' => 'member_pk', | ||
'email' => 'test@example.com', | ||
'last_active' => 111111111, | ||
'twofa' => 0, | ||
'status' => 1, | ||
'permission' => [ | ||
'level' => 100, | ||
'printable' => 'Owner', | ||
], | ||
], | ||
new Member( | ||
pk: 'member_pk', | ||
email: 'test@example.com', | ||
last_active: 111111111, | ||
twoFactorAuthentication: false, | ||
status: true, | ||
permission: new Permission( | ||
level: 100, | ||
printable: 'Owner', | ||
) | ||
), | ||
], | ||
]); |
Oops, something went wrong.