CryptoParamsTrait.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Aws\S3\Crypto;
  3. use Aws\Crypto\MaterialsProvider;
  4. use Aws\Crypto\MetadataEnvelope;
  5. use Aws\Crypto\MetadataStrategyInterface;
  6. trait CryptoParamsTrait
  7. {
  8. protected function getMaterialsProvider(array $args)
  9. {
  10. if ($args['@MaterialsProvider'] instanceof MaterialsProvider) {
  11. return $args['@MaterialsProvider'];
  12. }
  13. throw new \InvalidArgumentException('An instance of MaterialsProvider'
  14. . ' must be passed in the "MaterialsProvider" field.');
  15. }
  16. protected function getInstructionFileSuffix(array $args)
  17. {
  18. return !empty($args['@InstructionFileSuffix'])
  19. ? $args['@InstructionFileSuffix']
  20. : $this->instructionFileSuffix;
  21. }
  22. protected function determineGetObjectStrategy(
  23. $result,
  24. $instructionFileSuffix
  25. ) {
  26. if (isset($result['Metadata'][MetadataEnvelope::CONTENT_KEY_V2_HEADER])) {
  27. return new HeadersMetadataStrategy();
  28. }
  29. return new InstructionFileMetadataStrategy(
  30. $this->client,
  31. $instructionFileSuffix
  32. );
  33. }
  34. protected function getMetadataStrategy(array $args, $instructionFileSuffix)
  35. {
  36. if (!empty($args['@MetadataStrategy'])) {
  37. if ($args['@MetadataStrategy'] instanceof MetadataStrategyInterface) {
  38. return $args['@MetadataStrategy'];
  39. }
  40. if (is_string($args['@MetadataStrategy'])) {
  41. switch ($args['@MetadataStrategy']) {
  42. case HeadersMetadataStrategy::class:
  43. return new HeadersMetadataStrategy();
  44. case InstructionFileMetadataStrategy::class:
  45. return new InstructionFileMetadataStrategy(
  46. $this->client,
  47. $instructionFileSuffix
  48. );
  49. default:
  50. throw new \InvalidArgumentException('Could not match the'
  51. . ' specified string in "MetadataStrategy" to a'
  52. . ' predefined strategy.');
  53. }
  54. } else {
  55. throw new \InvalidArgumentException('The metadata strategy that'
  56. . ' was passed to "MetadataStrategy" was unrecognized.');
  57. }
  58. } elseif ($instructionFileSuffix) {
  59. return new InstructionFileMetadataStrategy(
  60. $this->client,
  61. $instructionFileSuffix
  62. );
  63. }
  64. return null;
  65. }
  66. }