This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andrew Waite <andrewwaite@insites.com>
- Loading branch information
Showing
5 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |