Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions src/Collections/Company/BanCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace TruckersMP\APIClient\Collections\Company;

use TruckersMP\APIClient\Collections\Collection;
use TruckersMP\APIClient\Models\CompanyBan;

class BanCollection extends Collection
{
/**
* Create a new BanCollection instance.
*
* @param array $response
* @return void
*/
public function __construct(array $response)
{
parent::__construct();

foreach ($response['members'] as $key => $ban) {
$this->items[$key] = new CompanyBan($ban);
}
}
}
144 changes: 144 additions & 0 deletions src/Models/CompanyBan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace TruckersMP\APIClient\Models;

use Carbon\Carbon;

class CompanyBan
{
/**
* The player's member ID within the company.
*
* @var int
*/
protected $id;

/**
* The ID of the user.
*
* @var int
*/
protected $userId;

/**
* The username of the user.
*
* @var string
*/
protected $username;

/**
* The Steam ID of the user.
*
* @var int
*/
protected $steamId;

/**
* The player's role ID within the company.
*
* @var int
*/
protected $roleId;

/**
* The player's role name within the company.
*
* @var string
*/
protected $roleName;

/**
* The date at which the user joined the company.
*
* @var Carbon
*/
protected $joinDate;

/**
* Create a new CompanyBan instance.
*
* @param array $ban
* @return void
*/
public function __construct(array $ban)
{
$this->id = $ban['id'];
$this->userId = $ban['user_id'];
$this->username = $ban['username'];
$this->steamId = $ban['steam_id'];
$this->roleId = $ban['role_id'];
$this->roleName = $ban['role'];
$this->joinDate = new Carbon($ban['joinDate'], 'UTC');
}

/**
* Get the player's member ID within the company.
*
* @return int
*/
public function getId(): int
{
return $this->id;
}

/**
* Get the ID of the user.
*
* @return int
*/
public function getUserId(): int
{
return $this->userId;
}

/**
* Get the username of the user.
*
* @return string
*/
public function getUsername(): string
{
return $this->username;
}

/**
* Get the Steam ID of the user.
*
* @return int
*/
public function getSteamId(): int
{
return $this->steamId;
}

/**
* Get the ID of the member's role within the company.
*
* @return int
*/
public function getRoleId(): int
{
return $this->roleId;
}

/**
* Get the name of the member's role within the company.
*
* @return string
*/
public function getRoleName(): string
{
return $this->roleName;
}

/**
* Get the date the member joined the company.
*
* @return Carbon
*/
public function getJoinDate(): Carbon
{
return $this->joinDate;
}
}
57 changes: 57 additions & 0 deletions src/Requests/Company/BanIndexRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace TruckersMP\APIClient\Requests\Company;

use Psr\Http\Client\ClientExceptionInterface;
use TruckersMP\APIClient\Collections\Company\BanCollection;
use TruckersMP\APIClient\Exceptions\ApiErrorException;
use TruckersMP\APIClient\Models\CompanyBan;
use TruckersMP\APIClient\Requests\Request;

class BanIndexRequest extends Request
{
/**
* The ID or slug of the requested company.
*
* @var string|int
*/
protected $companyKey;

/**
* Create a new BanIndexRequest instance.
*
* @param string|int $companyKey
* @return void
*/
public function __construct(string $companyKey)
{
parent::__construct();

$this->companyKey = $companyKey;
}

/**
* Get the endpoint of the request.
*
* @return string
*/
public function getEndpoint(): string
{
return 'vtc/' . $this->companyKey . '/members/banned';
}

/**
* Get the data for the request.
*
* @return BanCollection|CompanyBan[]
*
* @throws ApiErrorException
* @throws ClientExceptionInterface
*/
public function get(): BanCollection
{
return new BanCollection(
$this->send()['response']
);
}
}
12 changes: 12 additions & 0 deletions src/Requests/Company/MemberIndexRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,16 @@ public function get(): CompanyMemberIndex
$this->send()['response']
);
}

/**
* Get the members within the company that are currently banned.
*
* @return BanIndexRequest
*/
public function bans(): BanIndexRequest
{
return new BanIndexRequest(
$this->companyKey
);
}
}
25 changes: 25 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
use ReflectionException;
use TruckersMP\APIClient\Client;
use TruckersMP\APIClient\Collections\BanCollection;
use TruckersMP\APIClient\Collections\Company\BanCollection as CompanyBanCollection;
use TruckersMP\APIClient\Collections\Company\PostCollection;
use TruckersMP\APIClient\Collections\Company\RoleCollection;
use TruckersMP\APIClient\Collections\ServerCollection;
use TruckersMP\APIClient\Exceptions\ApiErrorException;
use TruckersMP\APIClient\Models\Ban;
use TruckersMP\APIClient\Models\Company;
use TruckersMP\APIClient\Models\CompanyBan;
use TruckersMP\APIClient\Models\CompanyEventIndex;
use TruckersMP\APIClient\Models\CompanyIndex;
use TruckersMP\APIClient\Models\CompanyMember;
Expand Down Expand Up @@ -397,6 +399,29 @@ public function companyMember(int $companyId, int $memberId): CompanyMember
return $cachedMember->get();
}

/**
* Get the members within the specified company that are currently banned.
*
* @param int $id
* @return CompanyBanCollection|CompanyBan[]
*
* @throws PhpfastcacheInvalidArgumentException
* @throws ApiErrorException
* @throws ClientExceptionInterface
* @throws InvalidArgumentException
*/
public function companyBans(int $id): CompanyBanCollection
{
$cachedCompanyBans = self::$cache->getItem('company_bans_' . $id);

if (!$cachedCompanyBans->isHit()) {
$cachedCompanyBans->set($this->client->company($id)->members()->bans()->get())->expiresAfter(self::CACHE_SECONDS);
self::$cache->save($cachedCompanyBans);
}

return $cachedCompanyBans->get();
}

/**
* Get or cache the TruckersMP version.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/CompanyBanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use TruckersMP\APIClient\Collections\Company\BanCollection;
use TruckersMP\APIClient\Models\CompanyBan;

class CompanyBanTest extends TestCase
{
/**
* The ID of the company to use in the tests.
*/
private const TEST_COMPANY = 2;

/** @test */
public function it_can_get_all_the_bans()
{
$bans = $this->companyBans(self::TEST_COMPANY);

$this->assertInstanceOf(BanCollection::class, $bans);

if ($bans->count() > 0) {
$ban = $bans[0];

$this->assertInstanceOf(CompanyBan::class, $ban);
}
}
}