mirror of
https://github.com/SkillDisplay/PHPToolKit.git
synced 2024-12-22 10:46:09 +01:00
Added Member and Organisation Statistic with Test
This commit is contained in:
parent
0f4263663b
commit
ae0c838722
7 changed files with 631 additions and 0 deletions
69
src/Api/MemberSkills.php
Normal file
69
src/Api/MemberSkills.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Api;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use SkillDisplay\PHPToolKit\Entity\Campaign;
|
||||
use SkillDisplay\PHPToolKit\Entity\MemberSkill;
|
||||
use SkillDisplay\PHPToolKit\Entity\MemberSkill as Entity;
|
||||
|
||||
class MemberSkills
|
||||
{
|
||||
protected Settings $settings;
|
||||
|
||||
protected Client $client;
|
||||
|
||||
public function __construct(
|
||||
Settings $settings,
|
||||
Client $client
|
||||
) {
|
||||
$this->settings = $settings;
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function getMemberSkillsById(int $id)
|
||||
{
|
||||
if ($id <= 0) {
|
||||
throw new \Exception('ID of Organisation has to be a positive integer.');
|
||||
}
|
||||
$url = $this->settings->getAPIUrl() . '/api/v1/organisation/' . $id . '/listVerifications/json';
|
||||
try {
|
||||
$result = $this->client->send(new Request(
|
||||
'GET',
|
||||
$url,
|
||||
[
|
||||
'Content-Type' => 'application/json',
|
||||
'x-api-key' => $this->settings->getApiKey()
|
||||
]
|
||||
));
|
||||
} catch (ClientException $e) {
|
||||
if ($e->getCode() === 404) {
|
||||
throw new \InvalidArgumentException('Given Organisation with id "' . $id . '" not available.', 1601881616);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if ($result->getStatusCode() !== 200) {
|
||||
throw new \Exception('Did not get proper response for Organisation.', 1600693562);
|
||||
}
|
||||
|
||||
$body = (string) $result->getBody();
|
||||
|
||||
if (strpos($body, 'Oops, an error occurred') !== false) {
|
||||
throw new \Exception('Did not get proper response for Organisation. Organisation with id "' . $id . '" does probably not exist.', 1600694312);
|
||||
}
|
||||
|
||||
$body = json_decode((string) $result->getBody(), true);
|
||||
$skills = [];
|
||||
foreach ($body as $skill) {
|
||||
$skills[] = Entity::createFromJson(json_encode($skill), $this->settings);
|
||||
}
|
||||
return $skills;
|
||||
}
|
||||
}
|
61
src/Api/Organisation.php
Normal file
61
src/Api/Organisation.php
Normal file
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Api;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use SkillDisplay\PHPToolKit\Entity\Organisation as Entity;
|
||||
|
||||
class Organisation
|
||||
{
|
||||
protected Settings $settings;
|
||||
|
||||
protected Client $client;
|
||||
|
||||
public function __construct(
|
||||
Settings $settings,
|
||||
Client $client
|
||||
) {
|
||||
$this->settings = $settings;
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function getStatisticById(int $id)
|
||||
{
|
||||
if ($id <= 0) {
|
||||
throw new \Exception('ID of Organisation has to be a positive integer.');
|
||||
}
|
||||
$url = $this->settings->getAPIUrl() . '/api/v1/organisation/' . $id . '/statistic';
|
||||
try {
|
||||
$result = $this->client->send(new Request(
|
||||
'GET',
|
||||
$url,
|
||||
[
|
||||
'Content-Type' => 'application/json',
|
||||
'x-api-key' => $this->settings->getApiKey()
|
||||
]
|
||||
));
|
||||
} catch (ClientException $e) {
|
||||
if ($e->getCode() === 404) {
|
||||
throw new \InvalidArgumentException('Given Organisation with id "' . $id . '" not available.', 1601881616);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if ($result->getStatusCode() !== 200) {
|
||||
throw new \Exception('Did not get proper response for Organisation.', 1600693562);
|
||||
}
|
||||
|
||||
$body = (string) $result->getBody();
|
||||
|
||||
if (strpos($body, 'Oops, an error occurred') !== false) {
|
||||
throw new \Exception('Did not get proper response for Organisation. Organisation with id "' . $id . '" does probably not exist.', 1600694312);
|
||||
}
|
||||
return Entity::createFromJson($body, $this->settings);
|
||||
}
|
||||
}
|
179
src/Entity/MemberSkill.php
Normal file
179
src/Entity/MemberSkill.php
Normal file
|
@ -0,0 +1,179 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Entity;
|
||||
|
||||
class MemberSkill
|
||||
{
|
||||
private int $id = 0;
|
||||
private string $created = '';
|
||||
private string $granted = '';
|
||||
private int $skillUid = 0;
|
||||
private string $skillUUid = '';
|
||||
private string $skill = '';
|
||||
private string $domainTag = '';
|
||||
private string $level = '';
|
||||
private string $user = '';
|
||||
private string $firstName = '';
|
||||
private string $lastName = '';
|
||||
private string $certifier = '';
|
||||
|
||||
private string $organisation = '';
|
||||
private string $campaign = '';
|
||||
private int $skillSetUid = 0;
|
||||
private string $skillSetName = '';
|
||||
|
||||
public static function createFromJson(string $json_encode, \SkillDisplay\PHPToolKit\Configuration\Settings $settings)
|
||||
{
|
||||
$json = json_decode($json_encode, true);
|
||||
$entity = new self();
|
||||
$entity->id = $json['uid'] ?? 0;
|
||||
$entity->created = $json['created'] ?? '';
|
||||
$entity->granted = $json['granted'] ?? '';
|
||||
$entity->skillUid = $json['skillUid'] ?? 0;
|
||||
$entity->skillUUid = $json['skillUUid'] ?? '';
|
||||
$entity->skill = $json['skill'] ?? '';
|
||||
$entity->domainTag = $json['domainTag'] ?? '';
|
||||
$entity->level = $json['level'] ?? '';
|
||||
$entity->user = $json['user'] ?? '';
|
||||
$entity->firstName = $json['firstName'] ?? '';
|
||||
$entity->lastName = $json['lastName'] ?? '';
|
||||
$entity->certifier = $json['certifier'] ?? '';
|
||||
$entity->organisation = $json['organisation'] ?? '';
|
||||
$entity->campaign = $json['campaign'] ?? '';
|
||||
$entity->skillSetUid = $json['skillSetUid'] ?? 0;
|
||||
$entity->skillSetName = $json['skillSetName'] ?? '';
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCreated(): string
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getGranted(): string
|
||||
{
|
||||
return $this->granted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSkillUid(): int
|
||||
{
|
||||
return $this->skillUid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSkillUUid(): string
|
||||
{
|
||||
return $this->skillUUid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSkill(): string
|
||||
{
|
||||
return $this->skill;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDomainTag(): string
|
||||
{
|
||||
return $this->domainTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLevel(): string
|
||||
{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUser(): string
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstName(): string
|
||||
{
|
||||
return $this->firstName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLastName(): string
|
||||
{
|
||||
return $this->lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCertifier(): string
|
||||
{
|
||||
return $this->certifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOrganisation(): string
|
||||
{
|
||||
return $this->organisation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCampaign(): string
|
||||
{
|
||||
return $this->campaign;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSkillSetUid(): int
|
||||
{
|
||||
return $this->skillSetUid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSkillSetName(): string
|
||||
{
|
||||
return $this->skillSetName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
}
|
133
src/Entity/Organisation.php
Normal file
133
src/Entity/Organisation.php
Normal file
|
@ -0,0 +1,133 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Entity;
|
||||
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
|
||||
class Organisation
|
||||
{
|
||||
private array $data = [];
|
||||
|
||||
private Settings $settings;
|
||||
|
||||
private ?Brand $brand = null;
|
||||
|
||||
private function __construct(array $data, Settings $settings)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->data['uid'];
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->data['name'] ?? '';
|
||||
}
|
||||
|
||||
public function getBrand(): Brand
|
||||
{
|
||||
if ($this->brand instanceof Brand) {
|
||||
return $this->brand;
|
||||
}
|
||||
|
||||
$this->brand = Brand::createFromJson(json_encode($this->data['brand']), $this->settings);
|
||||
|
||||
return $this->brand;
|
||||
}
|
||||
|
||||
public function getComposition(): string
|
||||
{
|
||||
return $this->data['composition'] ?? '';
|
||||
}
|
||||
|
||||
public function getCurrentMonthIssued(): int
|
||||
{
|
||||
return $this->data['currentMonthIssued'] ?? 0;
|
||||
}
|
||||
|
||||
public function getCurrentMonthUsers(): int
|
||||
{
|
||||
return $this->data['currentMonthUsers'] ?? 0;
|
||||
}
|
||||
|
||||
public function getCurrentMonthVerifications(): int
|
||||
{
|
||||
return $this->data['currentMonthVerifications'] ?? 0;
|
||||
}
|
||||
|
||||
public function getInterestSets(): array
|
||||
{
|
||||
return $this->data['interestSets'] ?? [];
|
||||
}
|
||||
|
||||
public function getLastMonthIssued(): int
|
||||
{
|
||||
return $this->data['lastMonthIssued'] ?? 0;
|
||||
}
|
||||
|
||||
public function getLastMonthUsers(): int
|
||||
{
|
||||
return $this->data['lastMonthUsers'] ?? 0;
|
||||
}
|
||||
|
||||
public function getLastMonthVerifications(): int
|
||||
{
|
||||
return $this->data['lastMonthVerifications'] ?? 0;
|
||||
}
|
||||
|
||||
public function getMonthlyScores(): array
|
||||
{
|
||||
return $this->data['monthlyScores'] ?? [];
|
||||
}
|
||||
|
||||
public function getPotential(): array
|
||||
{
|
||||
return $this->data['potential'] ?? [];
|
||||
}
|
||||
|
||||
public function getSumIssued(): int
|
||||
{
|
||||
return $this->data['sumSkills'] ?? 0;
|
||||
}
|
||||
|
||||
public function getSumSkills(): int
|
||||
{
|
||||
return $this->data['sumSkills'] ?? 0;
|
||||
}
|
||||
|
||||
public function getSumSupportedSkills(): int
|
||||
{
|
||||
return $this->data['sumSupportedSkills'] ?? 0;
|
||||
}
|
||||
|
||||
public function getSumVerifications(): int
|
||||
{
|
||||
return $this->data['sumVerifications'] ?? 0;
|
||||
}
|
||||
|
||||
public function getTotalScore(): int
|
||||
{
|
||||
return $this->data['totalScore'] ?? 0;
|
||||
}
|
||||
|
||||
public static function createFromJson(string $json, Settings $settings): self
|
||||
{
|
||||
return new Organisation(json_decode($json, true), $settings);
|
||||
}
|
||||
|
||||
public function getAsArray(): array
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
64
tests/Unit/Api/MemberSkillsTest.php
Normal file
64
tests/Unit/Api/MemberSkillsTest.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Tests\Unit\Api;
|
||||
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Prophecy\Argument;
|
||||
use SkillDisplay\PHPToolKit\Api\MemberSkills;
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use SkillDisplay\PHPToolKit\Entity\MemberSkill;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
|
||||
/**
|
||||
* @covers SkillDisplay\PHPToolKit\Api\MemberSkills
|
||||
*/
|
||||
class MemberSkillsTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceCanBeCreated()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$client = $this->prophesize(Client::class);
|
||||
$subject = new MemberSkills($settings->reveal(), $client->reveal());
|
||||
static::assertInstanceOf(MemberSkills::class, $subject);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function fetchedEntityFromApi()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$client = $this->prophesize(Client::class);
|
||||
$response = $this->prophesize(Response::class);
|
||||
|
||||
$settings->getAPIUrl()->willReturn('https://example.com');
|
||||
$settings->getApiKey()->willReturn('none');
|
||||
|
||||
$client->send(Argument::that(function (Request $request) {
|
||||
return (string) $request->getUri() === 'https://example.com/api/v1/organisation/10/listVerifications/json'
|
||||
&& $request->getHeader('x-api-key') === ['none']
|
||||
&& $request->getHeader('Content-Type') === ['application/json']
|
||||
&& $request->getMethod() === 'GET';
|
||||
}))->willReturn($response->reveal());
|
||||
|
||||
$response->getStatusCode()->willReturn(200);
|
||||
$response->getBody()->willReturn('[{"uid": 1},{"uid": 2}]');
|
||||
|
||||
$subject = new MemberSkills($settings->reveal(), $client->reveal());
|
||||
$skills = $subject->getMemberSkillsById(10);
|
||||
|
||||
static::assertInstanceOf(MemberSkill::class, $skills[0]);
|
||||
static::assertEquals(1, $skills[0]->getId());
|
||||
}
|
||||
}
|
66
tests/Unit/Api/OrganisationTest.php
Normal file
66
tests/Unit/Api/OrganisationTest.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Tests\Unit\Api;
|
||||
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Prophecy\Argument;
|
||||
use SkillDisplay\PHPToolKit\Api\Organisation;
|
||||
use SkillDisplay\PHPToolKit\Api\SkillSet;
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use GuzzleHttp\Client;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use SkillDisplay\PHPToolKit\Entity\Organisation as Entity;
|
||||
|
||||
/**
|
||||
* @covers SkillDisplay\PHPToolKit\Api\Organisation
|
||||
*/
|
||||
class OrganisationTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceCanBeCreated()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$client = $this->prophesize(Client::class);
|
||||
$subject = new Organisation($settings->reveal(), $client->reveal());
|
||||
|
||||
static::assertInstanceOf(Organisation::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function fetchOrganisationStatistic()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$client = $this->prophesize(Client::class);
|
||||
$response = $this->prophesize(Response::class);
|
||||
|
||||
$settings->getAPIUrl()->willReturn('https://example.com');
|
||||
$settings->getApiKey()->willReturn('none');
|
||||
|
||||
$client->send(Argument::that(function (Request $request) {
|
||||
return (string)$request->getUri() === 'https://example.com/api/v1/organisation/10/statistic'
|
||||
&& $request->getHeader('x-api-key') === ['none']
|
||||
&& $request->getHeader('Content-Type') === ['application/json']
|
||||
&& $request->getMethod() === 'GET';
|
||||
}))->willReturn($response->reveal());
|
||||
|
||||
$response->getStatusCode()->willReturn(200);
|
||||
$response->getBody()->willReturn('{"uid":10}');
|
||||
|
||||
$subject = new Organisation(
|
||||
$settings->reveal(),
|
||||
$client->reveal()
|
||||
);
|
||||
$result = $subject->getStatisticById(10);
|
||||
static::assertInstanceOf(Entity::class, $result);
|
||||
}
|
||||
}
|
59
tests/Unit/Entity/OrganisationTest.php
Normal file
59
tests/Unit/Entity/OrganisationTest.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Tests\Unit\Entity;
|
||||
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use SkillDisplay\PHPToolKit\Entity\Brand;
|
||||
use SkillDisplay\PHPToolKit\Entity\Organisation;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @covers SkillDisplay\PHPToolKit\Entity\Organisation
|
||||
*/
|
||||
class OrganisationTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceCanBeCreatedFromJson()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$subject = Organisation::createFromJson('{}', $settings->reveal());
|
||||
static::assertInstanceOf(Organisation::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceReturnsId()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$subject = Organisation::createFromJson('{"uid":90}', $settings->reveal());
|
||||
static::assertSame(90, $subject->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceReturnsName()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$subject = Organisation::createFromJson('{"name":"Example name"}', $settings->reveal());
|
||||
static::assertSame('Example name', $subject->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function canBeConvertedToArray()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$subject = Organisation::createFromJson('{"uid":90,"name":"Example name"}', $settings->reveal());
|
||||
static::assertSame(['uid' => 90, 'name' => 'Example name'], $subject->toArray());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue