Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Report Settings API (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Waite <andrewwaite@insites.com>
  • Loading branch information
andywaite and Andrew Waite authored Aug 25, 2022
1 parent e5478ad commit 6a1c872
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/example-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
require __DIR__ . "/../vendor/autoload.php";

use Symfony\Component\Dotenv\Dotenv;
use Silktide\ProspectClient\ProspectClient;

$envFile = __DIR__ . "/../.env";
if (file_exists($envFile)) {
(new Dotenv())->load($envFile);
}
$apiKey = $_ENV["PROSPECT_API_KEY"] ?? null;

if (!is_string($apiKey)) {
throw new \Exception("An API key should be specified in the ./env file to run the examples");
}

$prospectClient = ProspectClient::createFromApiKey($apiKey);
$reportApi = $prospectClient->getReportApi();

$reportId = "e69ef2c48be24356a27ff77f5d6bf5ce1678e239";

$settingsResponse = $reportApi->settings($reportId)
->persistSetting('name', 'Business Co')
->persistSetting('phone', '012 345 6789')
->execute();

$settings = $settingsResponse->getSettings();
echo "Found: " . count($settings) . " settings \n";

foreach ($settings as $setting) {
echo "ID: " . $setting->getId() . "; Value: " . $setting->getValue() . "\n";
}
6 changes: 6 additions & 0 deletions src/Api/ReportApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Silktide\ProspectClient\Request\CreateReportRequest;
use Silktide\ProspectClient\Request\FetchReportRequest;
use Silktide\ProspectClient\Request\ReanalyzeReportRequest;
use Silktide\ProspectClient\Request\ReportSettingsRequest;
use Silktide\ProspectClient\Request\SearchReportRequest;
use Silktide\ProspectClient\Http\HttpWrapper;

Expand Down Expand Up @@ -36,4 +37,9 @@ public function search(): SearchReportRequest
{
return new SearchReportRequest($this->httpWrapper);
}

public function settings(string $reportId)
{
return new ReportSettingsRequest($this->httpWrapper, $reportId);
}
}
51 changes: 51 additions & 0 deletions src/Entity/ReportSetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Silktide\ProspectClient\Entity;

class ReportSetting
{
protected $value;
protected string $type;
protected string $id;

public static function create(string $id, string $type, $value): self
{
$instance = new self();

$instance->id = $id;
$instance->type = $type;
$instance->value = $value;

return $instance;
}

public function getValue()
{
return $this->value;
}

public function setValue($value): void
{
$this->value = $value;
}

public function getType(): string
{
return $this->type;
}

public function setType(string $type): void
{
$this->type = $type;
}

public function getId(): string
{
return $this->id;
}

public function setId(string $id): void
{
$this->id = $id;
}
}
44 changes: 44 additions & 0 deletions src/Request/ReportSettingsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Silktide\ProspectClient\Request;

use Silktide\ProspectClient\Exception\Api\ReportNotFoundException;
use Silktide\ProspectClient\Http\HttpWrapper;
use Silktide\ProspectClient\Response\AbstractResponse;
use Silktide\ProspectClient\Response\ReportSettingsResponse;

class ReportSettingsRequest extends AbstractRequest
{
protected string $path = 'report-settings';
protected string $reportId;

public function __construct(HttpWrapper $httpWrapper, string $reportId)
{
parent::__construct($httpWrapper);
$this->reportId = $reportId;
}

public function persistSetting(string $name, $value): self
{
$this->method = 'PUT';
$this->body[$name] = $value;

return $this;
}

public function getPath(): string
{
return "$this->path/$this->reportId";
}

public function execute(): ReportSettingsResponse
{
$httpResponse = $this->httpWrapper->execute($this);

if ($httpResponse->getStatusCode() === 404) {
throw new ReportNotFoundException($response['error_message'] ?? 'Report not found');
}

return new ReportSettingsResponse($httpResponse->getResponse());
}
}
31 changes: 31 additions & 0 deletions src/Response/ReportSettingsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Silktide\ProspectClient\Response;

use Silktide\ProspectClient\Entity\ReportSetting;

class ReportSettingsResponse extends AbstractResponse
{
/**
* @return ReportSetting[]
*/
public function getSettings(): array
{
$settings = [];
foreach ($this->response['settings'] ?? [] as $id => $setting) {
$settings[] = ReportSetting::create($id, $setting['type'], $setting['value']);
}
return $settings;
}

public function getSetting(string $id): ReportSetting
{
if (empty($this->response['settings'][$id])) {
throw new \OutOfBoundsException('No such setting '.$id);
}

$setting = $this->response['settings'][$id];

return ReportSetting::create($id, $setting['type'], $setting['value']);
}
}

0 comments on commit 6a1c872

Please sign in to comment.