Skip to content

Instantly share code, notes, and snippets.

@tonysm
Created March 18, 2020 13:41
Show Gist options
  • Save tonysm/a2fab21857b92d5ce5d757976dbc0bbb to your computer and use it in GitHub Desktop.
Save tonysm/a2fab21857b92d5ce5d757976dbc0bbb to your computer and use it in GitHub Desktop.
AWS IoT, pipeline actions, and builder pattern
<?php
class AwsIotProvider implements IotProvider
{
public function createDevice(DeviceId $deviceId, Certificate $rootCertificate): void
{
(new Pipeline(app()))
->send(
Aws\Tasks\CreateIotDeviceTask::forDeviceId($deviceId)
->withPolicyName(config('iot.providers.aws.devices.policy_name'))
->withDeviceType(config('iot.providers.aws.devices.device_type'))
->build()
)
->through([
Aws\Actions\CreateDeviceThing::class,
Aws\Actions\CreateSignedDeviceCertificate::class,
])
->thenReturn();
}
}
final class CreateIotDeviceTask
{
private DeviceId $deviceId;
private string $deviceType;
private string $policyName;
public static function forDeviceId(DeviceId $deviceId): CreateIotDeviceTaskBuilder
{
return (new CreateIotDeviceTaskBuilder())
->withDeviceId($deviceId);
}
// ...
}
final class CreateIotDeviceTaskBuilder
{
public DeviceId $deviceId;
public string $deviceType;
public string $policyName;
public function withDeviceId(DeviceId $deviceId): self
{
$this->deviceId = $deviceId;
return $this;
}
// ...
public function build(): CreateIotDeviceTask
{
return new CreateIotDeviceTask($this->deviceId, $this->deviceType, $this->policyName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment