<?php
namespace App\Entity\System\Sendgrid;
use App\Entity\System\AbstractMessagingConfig;
use Cms\CoreBundle\Model\Interfaces\Identifiable\IdentifiableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use Ramsey\Uuid\UuidInterface;
/**
* Class SendgridConfig
* @package App\Entity\System\Sendgrid
*
* @ORM\Entity(
* repositoryClass = "App\Doctrine\Repository\System\Sendgrid\SendgridConfigRepository",
* )
* @ORM\Table(
* name = "sys__sendgrid_config",
* )
*/
class SendgridConfig extends AbstractMessagingConfig implements JsonSerializable
{
use SendgridConfigs\SendgridConfigSubaccountTrait;
use SendgridConfigs\SendgridConfigApiTrait;
use SendgridConfigs\SendgridConfigWebhooksTrait;
use SendgridConfigs\SendgridConfigSendersTrait;
/**
*
*/
public function __construct()
{
$this->domains = new ArrayCollection();
$this->__constructSubaccount();
$this->__constructApi();
$this->__constructWebhooks();
$this->__constructSenders();
}
/**
* {@inheritDoc}
*/
public function isUsable(): bool
{
// we must be provisioned and have at least one domain setup
return (
$this->isProvisioned()
&&
$this->hasDomains()
);
}
/**
* @return bool
*/
public function isCredentialed(): bool
{
return (
$this->getSubaccountId()
&&
$this->getApiKeyId()
);
}
/**
* {@inheritDoc}
*/
public function isProvisioned(): bool
{
// all we need is a subaccount setup along with api creds
// we can assume if we have an api key id, that we have the secret and stuff too...
// the webhook url is taken care of during provisioning
return (
$this->isCredentialed()
&&
$this->getWebhookUrl()
);
}
/**
* {@inheritDoc}
*/
public function isOrganizationRegistered(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isOrganizationEvaluated(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isOrganizationSubmitted(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isOrganizationVerified(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isOrganizationValid(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isOrganizationInvalid(): bool
{
// sendgrid does not have any kind of special registration processes...
return false;
}
/**
* {@inheritDoc}
*/
public function canPrepare(): bool
{
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isPrepared(): bool
{
return $this->hasDomains();
}
/**
* {@inheritDoc}
*/
public function isServicesRegistered(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isServicesEvaluated(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isServicesSubmitted(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isServicesVerified(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isServicesValid(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isServicesInvalid(): bool
{
// sendgrid does not have any kind of special registration processes...
return false;
}
/**
* {@inheritDoc}
*/
public function isBrandSubmitted(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isBrandVerified(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isBrandValid(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isBrandInvalid(): bool
{
// sendgrid does not have any kind of special registration processes...
return false;
}
/**
* {@inheritDoc}
*/
public function isCampaignsSubmitted(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isCampaignsVerified(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isCampaignsValid(): bool
{
// sendgrid does not have any kind of special registration processes...
return $this->isProvisioned();
}
/**
* {@inheritDoc}
*/
public function isCampaignsInvalid(): bool
{
// sendgrid does not have any kind of special registration processes...
return false;
}
/**
* {@inheritDoc}
*/
public function jsonSerialize(): array
{
static $refl = null;
$formatter = static function ($value, callable $formatter) {
switch (true) {
case is_scalar($value):
return $value;
case is_array($value):
$data = [];
foreach ($value as $k => $v) {
$data[$k] = $formatter($v, $formatter);
}
ksort($data);
return $data;
case $value instanceof UuidInterface:
return $value->__toString();
case $value instanceof IdentifiableInterface:
return $value->getId();
case $value instanceof \DateTimeInterface:
return $value->format(\DateTimeInterface::RFC3339);
case $value instanceof JsonSerializable:
return $value->jsonSerialize();
default:
return null;
}
};
if ( ! $refl) {
$refl = new \ReflectionObject($this);
}
$data = [];
foreach ($refl->getProperties(\ReflectionProperty::IS_PROTECTED) as $property) {
$property->setAccessible(true);
$data[$property->getName()] = $formatter(
$property->getValue($this),
$formatter
);
$property->setAccessible(false);
}
ksort($data);
return $data;
}
}