src/App/Entity/System/Sendgrid/SendgridConfig.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity\System\Sendgrid;
  3. use App\Entity\System\AbstractMessagingConfig;
  4. use Cms\CoreBundle\Model\Interfaces\Identifiable\IdentifiableInterface;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JsonSerializable;
  8. use Ramsey\Uuid\UuidInterface;
  9. /**
  10.  * Class SendgridConfig
  11.  * @package App\Entity\System\Sendgrid
  12.  *
  13.  * @ORM\Entity(
  14.  *     repositoryClass = "App\Doctrine\Repository\System\Sendgrid\SendgridConfigRepository",
  15.  * )
  16.  * @ORM\Table(
  17.  *     name = "sys__sendgrid_config",
  18.  * )
  19.  */
  20. class SendgridConfig extends AbstractMessagingConfig implements JsonSerializable
  21. {
  22.     use SendgridConfigs\SendgridConfigSubaccountTrait;
  23.     use SendgridConfigs\SendgridConfigApiTrait;
  24.     use SendgridConfigs\SendgridConfigWebhooksTrait;
  25.     use SendgridConfigs\SendgridConfigSendersTrait;
  26.     /**
  27.      *
  28.      */
  29.     public function __construct()
  30.     {
  31.         $this->domains = new ArrayCollection();
  32.         $this->__constructSubaccount();
  33.         $this->__constructApi();
  34.         $this->__constructWebhooks();
  35.         $this->__constructSenders();
  36.     }
  37.     /**
  38.      * {@inheritDoc}
  39.      */
  40.     public function isUsable(): bool
  41.     {
  42.         // we must be provisioned and have at least one domain setup
  43.         return (
  44.                 $this->isProvisioned()
  45.             &&
  46.                 $this->hasDomains()
  47.         );
  48.     }
  49.     /**
  50.      * @return bool
  51.      */
  52.     public function isCredentialed(): bool
  53.     {
  54.         return (
  55.                 $this->getSubaccountId()
  56.             &&
  57.                 $this->getApiKeyId()
  58.         );
  59.     }
  60.     /**
  61.      * {@inheritDoc}
  62.      */
  63.     public function isProvisioned(): bool
  64.     {
  65.         // all we need is a subaccount setup along with api creds
  66.         // we can assume if we have an api key id, that we have the secret and stuff too...
  67.         // the webhook url is taken care of during provisioning
  68.         return (
  69.                 $this->isCredentialed()
  70.             &&
  71.                 $this->getWebhookUrl()
  72.         );
  73.     }
  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     public function isOrganizationRegistered(): bool
  78.     {
  79.         // sendgrid does not have any kind of special registration processes...
  80.         return $this->isProvisioned();
  81.     }
  82.     /**
  83.      * {@inheritDoc}
  84.      */
  85.     public function isOrganizationEvaluated(): bool
  86.     {
  87.         // sendgrid does not have any kind of special registration processes...
  88.         return $this->isProvisioned();
  89.     }
  90.     /**
  91.      * {@inheritDoc}
  92.      */
  93.     public function isOrganizationSubmitted(): bool
  94.     {
  95.         // sendgrid does not have any kind of special registration processes...
  96.         return $this->isProvisioned();
  97.     }
  98.     /**
  99.      * {@inheritDoc}
  100.      */
  101.     public function isOrganizationVerified(): bool
  102.     {
  103.         // sendgrid does not have any kind of special registration processes...
  104.         return $this->isProvisioned();
  105.     }
  106.     /**
  107.      * {@inheritDoc}
  108.      */
  109.     public function isOrganizationValid(): bool
  110.     {
  111.         // sendgrid does not have any kind of special registration processes...
  112.         return $this->isProvisioned();
  113.     }
  114.     /**
  115.      * {@inheritDoc}
  116.      */
  117.     public function isOrganizationInvalid(): bool
  118.     {
  119.         // sendgrid does not have any kind of special registration processes...
  120.         return false;
  121.     }
  122.     /**
  123.      * {@inheritDoc}
  124.      */
  125.     public function canPrepare(): bool
  126.     {
  127.         return $this->isProvisioned();
  128.     }
  129.     /**
  130.      * {@inheritDoc}
  131.      */
  132.     public function isPrepared(): bool
  133.     {
  134.         return $this->hasDomains();
  135.     }
  136.     /**
  137.      * {@inheritDoc}
  138.      */
  139.     public function isServicesRegistered(): bool
  140.     {
  141.         // sendgrid does not have any kind of special registration processes...
  142.         return $this->isProvisioned();
  143.     }
  144.     /**
  145.      * {@inheritDoc}
  146.      */
  147.     public function isServicesEvaluated(): bool
  148.     {
  149.         // sendgrid does not have any kind of special registration processes...
  150.         return $this->isProvisioned();
  151.     }
  152.     /**
  153.      * {@inheritDoc}
  154.      */
  155.     public function isServicesSubmitted(): bool
  156.     {
  157.         // sendgrid does not have any kind of special registration processes...
  158.         return $this->isProvisioned();
  159.     }
  160.     /**
  161.      * {@inheritDoc}
  162.      */
  163.     public function isServicesVerified(): bool
  164.     {
  165.         // sendgrid does not have any kind of special registration processes...
  166.         return $this->isProvisioned();
  167.     }
  168.     /**
  169.      * {@inheritDoc}
  170.      */
  171.     public function isServicesValid(): bool
  172.     {
  173.         // sendgrid does not have any kind of special registration processes...
  174.         return $this->isProvisioned();
  175.     }
  176.     /**
  177.      * {@inheritDoc}
  178.      */
  179.     public function isServicesInvalid(): bool
  180.     {
  181.         // sendgrid does not have any kind of special registration processes...
  182.         return false;
  183.     }
  184.     /**
  185.      * {@inheritDoc}
  186.      */
  187.     public function isBrandSubmitted(): bool
  188.     {
  189.         // sendgrid does not have any kind of special registration processes...
  190.         return $this->isProvisioned();
  191.     }
  192.     /**
  193.      * {@inheritDoc}
  194.      */
  195.     public function isBrandVerified(): bool
  196.     {
  197.         // sendgrid does not have any kind of special registration processes...
  198.         return $this->isProvisioned();
  199.     }
  200.     /**
  201.      * {@inheritDoc}
  202.      */
  203.     public function isBrandValid(): bool
  204.     {
  205.         // sendgrid does not have any kind of special registration processes...
  206.         return $this->isProvisioned();
  207.     }
  208.     /**
  209.      * {@inheritDoc}
  210.      */
  211.     public function isBrandInvalid(): bool
  212.     {
  213.         // sendgrid does not have any kind of special registration processes...
  214.         return false;
  215.     }
  216.     /**
  217.      * {@inheritDoc}
  218.      */
  219.     public function isCampaignsSubmitted(): bool
  220.     {
  221.         // sendgrid does not have any kind of special registration processes...
  222.         return $this->isProvisioned();
  223.     }
  224.     /**
  225.      * {@inheritDoc}
  226.      */
  227.     public function isCampaignsVerified(): bool
  228.     {
  229.         // sendgrid does not have any kind of special registration processes...
  230.         return $this->isProvisioned();
  231.     }
  232.     /**
  233.      * {@inheritDoc}
  234.      */
  235.     public function isCampaignsValid(): bool
  236.     {
  237.         // sendgrid does not have any kind of special registration processes...
  238.         return $this->isProvisioned();
  239.     }
  240.     /**
  241.      * {@inheritDoc}
  242.      */
  243.     public function isCampaignsInvalid(): bool
  244.     {
  245.         // sendgrid does not have any kind of special registration processes...
  246.         return false;
  247.     }
  248.     /**
  249.      * {@inheritDoc}
  250.      */
  251.     public function jsonSerialize(): array
  252.     {
  253.         static $refl null;
  254.         $formatter = static function ($value, callable $formatter) {
  255.             switch (true) {
  256.                 case is_scalar($value):
  257.                     return $value;
  258.                 case is_array($value):
  259.                     $data = [];
  260.                     foreach ($value as $k => $v) {
  261.                         $data[$k] = $formatter($v$formatter);
  262.                     }
  263.                     ksort($data);
  264.                     return $data;
  265.                 case $value instanceof UuidInterface:
  266.                     return $value->__toString();
  267.                 case $value instanceof IdentifiableInterface:
  268.                     return $value->getId();
  269.                 case $value instanceof \DateTimeInterface:
  270.                     return $value->format(\DateTimeInterface::RFC3339);
  271.                 case $value instanceof JsonSerializable:
  272.                     return $value->jsonSerialize();
  273.                 default:
  274.                     return null;
  275.             }
  276.         };
  277.         if ( ! $refl) {
  278.             $refl = new \ReflectionObject($this);
  279.         }
  280.         $data = [];
  281.         foreach ($refl->getProperties(\ReflectionProperty::IS_PROTECTED) as $property) {
  282.             $property->setAccessible(true);
  283.             $data[$property->getName()] = $formatter(
  284.                 $property->getValue($this),
  285.                 $formatter
  286.             );
  287.             $property->setAccessible(false);
  288.         }
  289.         ksort($data);
  290.         return $data;
  291.     }
  292. }