Add API call for existing campaigns of the user

This commit is contained in:
Matthias Böhm 2020-11-20 15:02:13 +01:00
parent 9b3ab12ec5
commit 66f9f2bd5e
4 changed files with 326 additions and 0 deletions

81
src/Api/Campaigns.php Normal file
View file

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2020 Matthias Böhm <matthias.boehm@reelworx.at>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
namespace SkillDisplay\PHPToolKit\Api;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request;
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\Campaign;
class Campaigns
{
/**
* @var Settings
*/
protected $settings;
/**
* @var Client
*/
protected $client;
public function __construct(
Settings $settings,
Client $client
) {
$this->settings = $settings;
$this->client = $client;
}
public function getForUser(): array
{
$url = $this->settings->getAPIUrl() . '/api/v1/campaigns';
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() === 400) {
throw new \InvalidArgumentException($e->getMessage(), 1605877581);
}
throw $e;
}
if ($result->getStatusCode() !== 200) {
throw new \Exception('API key or user invalid.', 1605878128);
}
$body = json_decode($result->getBody(), true);
$campaigns = [];
foreach ($body['Campaigns'] as $campaign) {
$campaigns[] = new Campaign($campaign, $this->settings);
}
return $campaigns;
}
}

65
src/Entity/Campaign.php Normal file
View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2020 Matthias Böhm <matthias.boehm@reelworx.at>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
namespace SkillDisplay\PHPToolKit\Entity;
use SkillDisplay\PHPToolKit\Configuration\Settings;
class Campaign
{
/**
* @var array
*/
private $data;
/**
* @var Settings
*/
private $settings;
public function __construct(array $data, Settings $settings)
{
$this->data = $data;
$this->settings = $settings;
}
public function getId(): int
{
return $this->data['uid'];
}
public function getTitle(): string
{
return $this->data['title'] ?? '';
}
// In order to support frameworks / APIs that expect "getter".
public function getAsArray(): array
{
return $this->toArray();
}
public function toArray(): array
{
return $this->data;
}
}

View file

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Tests\Unit\Api;
/*
* Copyright (C) 2020 Matthias Böhm <matthias.boehm@reelworx.at>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use SkillDisplay\PHPToolKit\Api\Campaigns;
use PHPUnit\Framework\TestCase;
use SkillDisplay\PHPToolKit\Configuration\Settings;
/**
* @covers SkillDisplay\PHPToolKit\Api\Campaigns
*/
class CampaignsTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
public function instanceCanBeCreated()
{
$settings = $this->prophesize(Settings::class);
$client = $this->prophesize(Client::class);
$subject = new Campaigns(
$settings->reveal(),
$client->reveal()
);
static::assertInstanceOf(Campaigns::class, $subject);
}
/**
* @test
*/
public function fetchedResultFromApi()
{
$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/campaigns'
&& $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('{"Version": "1.0","ErrorMessage": "","Campaigns": [{"uid": 1},{"uid": 2}]}');
$subject = new Campaigns(
$settings->reveal(),
$client->reveal()
);
$result = $subject->getForUser();
static::assertSame(2, count($result));
}
}

View file

@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Tests\Unit\Entity;
/*
* Copyright (C) 2020 Matthias Böhm <matthias.boehm@reelworx.at>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\Campaign;
/**
* @covers SkillDisplay\PHPToolKit\Entity\Campaign
*/
class CampaignTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
public function instanceCanBeCreatedFromJson()
{
$settings = $this->prophesize(Settings::class);
$subject = new Campaign([], $settings->reveal());
static::assertInstanceOf(Campaign::class, $subject);
}
/**
* @test
*/
public function instanceReturnsId()
{
$settings = $this->prophesize(Settings::class);
$subject = new Campaign(['uid' => 1], $settings->reveal());
static::assertSame(1, $subject->getId());
}
/**
* @test
*/
public function instanceReturnsTitle()
{
$settings = $this->prophesize(Settings::class);
$subject = new Campaign(['title' => 'Example Campaign'], $settings->reveal());
static::assertSame('Example Campaign', $subject->getTitle());
}
/**
* @test
*/
public function canReturnAsArray()
{
$settings = $this->prophesize(Settings::class);
$subject = new Campaign(['uid' => 1, 'title' => 'Example Campaign'], $settings->reveal());
static::assertSame([
'uid' => 1,
'title' => 'Example Campaign',
], $subject->getAsArray());
}
/**
* @test
*/
public function canBeConvertedToArray()
{
$settings = $this->prophesize(Settings::class);
$subject = new Campaign(['uid' => 1, 'title' => 'Example Campaign'], $settings->reveal());
static::assertSame([
'uid' => 1,
'title' => 'Example Campaign',
], $subject->toArray());
}
}