Skip to content

Commit

Permalink
Implement Organizations view/list endpoints (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
rapkis authored Sep 19, 2023
1 parent dc94b11 commit b7fc461
Show file tree
Hide file tree
Showing 20 changed files with 768 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/ControlD.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rapkis\Controld\Resources\Account;
use Rapkis\Controld\Resources\Analytics;
use Rapkis\Controld\Resources\Devices;
use Rapkis\Controld\Resources\Organizations;
use Rapkis\Controld\Resources\Profiles;
use Rapkis\Controld\Resources\Services;

Expand Down Expand Up @@ -47,4 +48,9 @@ public function account(): Account
{
return app(Account::class, ['client' => $this->request]);
}

public function organizations(): Organizations
{
return app(Organizations::class, ['client' => $this->request]);
}
}
18 changes: 18 additions & 0 deletions src/Entities/Member.php
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,
) {
}
}
15 changes: 15 additions & 0 deletions src/Entities/OrganizationLimit.php
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,
) {
}
}
14 changes: 14 additions & 0 deletions src/Entities/Permission.php
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,
) {
}
}
27 changes: 27 additions & 0 deletions src/Factories/MemberFactory.php
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'],
)
);
}
}
57 changes: 57 additions & 0 deletions src/Factories/OrganizationFactory.php
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,
);
}
}
2 changes: 1 addition & 1 deletion src/Factories/UserDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function make(array $data): UserData
proxyAccess: (bool) $data['proxy_access'],
lastActive: (int) $data['last_active'],
twoFactorAuthentication: (bool) $data['twofa'],
analyticsRegion: $data['stats_endpoint'],
statsEndpoint: $data['stats_endpoint'],
debug: $data['debug'] ?? [],
);
}
Expand Down
56 changes: 56 additions & 0 deletions src/Resources/Organizations.php
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;
}
}
11 changes: 11 additions & 0 deletions src/Responses/Members.php
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
{
}
40 changes: 40 additions & 0 deletions src/Responses/Organization.php
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,
) {
}
}
11 changes: 11 additions & 0 deletions src/Responses/Organizations.php
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
{
}
2 changes: 1 addition & 1 deletion src/Responses/UserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
public readonly bool $proxyAccess,
public readonly int $lastActive,
public readonly bool $twoFactorAuthentication,
public readonly string $analyticsRegion,
public readonly string $statsEndpoint,
public readonly array $debug,
) {
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ControlDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rapkis\Controld\Resources\Account;
use Rapkis\Controld\Resources\Analytics;
use Rapkis\Controld\Resources\Devices;
use Rapkis\Controld\Resources\Organizations;
use Rapkis\Controld\Resources\Profiles;
use Rapkis\Controld\Resources\Services;

Expand Down Expand Up @@ -40,3 +41,8 @@
$client = new ControlD($this->createStub(PendingRequest::class));
expect($client->account())->toBeInstanceOf(Account::class);
});

it('accesses organizations resource', function () {
$client = new ControlD($this->createStub(PendingRequest::class));
expect($client->organizations())->toBeInstanceOf(Organizations::class);
});
36 changes: 36 additions & 0 deletions tests/Factories/MemberFactoryTest.php
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',
)
),
],
]);
Loading

0 comments on commit b7fc461

Please sign in to comment.