| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\S3\Crypto; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Crypto\MetadataStrategyInterface; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\Crypto\MetadataEnvelope; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\S3\S3Client; |
| 8 |
/** |
| 9 |
* Stores and reads encryption MetadataEnvelope information in a file on Amazon |
| 10 |
* S3. |
| 11 |
* |
| 12 |
* A file with the contents of a MetadataEnvelope will be created or read from |
| 13 |
* alongside the base file on Amazon S3. The provided client will be used for |
| 14 |
* reading or writing this object. A specified suffix (default of '.instruction' |
| 15 |
* will be applied to each of the operations involved with the instruction file. |
| 16 |
* |
| 17 |
* If there is a failure after an instruction file has been uploaded, it will |
| 18 |
* not be automatically deleted. |
| 19 |
*/ |
| 20 |
class InstructionFileMetadataStrategy implements MetadataStrategyInterface |
| 21 |
{ |
| 22 |
const DEFAULT_FILE_SUFFIX = '.instruction'; |
| 23 |
private $client; |
| 24 |
private $suffix; |
| 25 |
/** |
| 26 |
* @param S3Client $client Client for use in uploading the instruction file. |
| 27 |
* @param string|null $suffix Optional override suffix for instruction file |
| 28 |
* object keys. |
| 29 |
*/ |
| 30 |
public function __construct(S3Client $client, $suffix = null) |
| 31 |
{ |
| 32 |
$this->suffix = empty($suffix) ? self::DEFAULT_FILE_SUFFIX : $suffix; |
| 33 |
$this->client = $client; |
| 34 |
} |
| 35 |
/** |
| 36 |
* Places the information in the MetadataEnvelope to a location on S3. |
| 37 |
* |
| 38 |
* @param MetadataEnvelope $envelope Encryption data to save according to |
| 39 |
* the strategy. |
| 40 |
* @param array $args Starting arguments for PutObject, used for saving |
| 41 |
* extra the instruction file. |
| 42 |
* |
| 43 |
* @return array Updated arguments for PutObject. |
| 44 |
*/ |
| 45 |
public function save(MetadataEnvelope $envelope, array $args) |
| 46 |
{ |
| 47 |
$this->client->putObject(['Bucket' => $args['Bucket'], 'Key' => $args['Key'] . $this->suffix, 'Body' => \json_encode($envelope)]); |
| 48 |
return $args; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Uses the strategy's client to retrieve the instruction file from S3 and generates |
| 52 |
* a MetadataEnvelope from its contents. |
| 53 |
* |
| 54 |
* @param array $args Arguments from Command and Result that contains |
| 55 |
* S3 Object information, relevant headers, and command |
| 56 |
* configuration. |
| 57 |
* |
| 58 |
* @return MetadataEnvelope |
| 59 |
*/ |
| 60 |
public function load(array $args) |
| 61 |
{ |
| 62 |
$result = $this->client->getObject(['Bucket' => $args['Bucket'], 'Key' => $args['Key'] . $this->suffix]); |
| 63 |
$metadataHeaders = \json_decode($result['Body'], \true); |
| 64 |
$envelope = new MetadataEnvelope(); |
| 65 |
$constantValues = MetadataEnvelope::getConstantValues(); |
| 66 |
foreach ($constantValues as $constant) { |
| 67 |
if (!empty($metadataHeaders[$constant])) { |
| 68 |
$envelope[$constant] = $metadataHeaders[$constant]; |
| 69 |
} |
| 70 |
} |
| 71 |
return $envelope; |
| 72 |
} |
| 73 |
} |
| 74 |
|