UploadState.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Aws\Multipart;
  3. /**
  4. * Representation of the multipart upload.
  5. *
  6. * This object keeps track of the state of the upload, including the status and
  7. * which parts have been uploaded.
  8. */
  9. class UploadState
  10. {
  11. const CREATED = 0;
  12. const INITIATED = 1;
  13. const COMPLETED = 2;
  14. /** @var array Params used to identity the upload. */
  15. private $id;
  16. /** @var int Part size being used by the upload. */
  17. private $partSize;
  18. /** @var array Parts that have been uploaded. */
  19. private $uploadedParts = [];
  20. /** @var int Identifies the status the upload. */
  21. private $status = self::CREATED;
  22. /**
  23. * @param array $id Params used to identity the upload.
  24. */
  25. public function __construct(array $id)
  26. {
  27. $this->id = $id;
  28. }
  29. /**
  30. * Get the upload's ID, which is a tuple of parameters that can uniquely
  31. * identify the upload.
  32. *
  33. * @return array
  34. */
  35. public function getId()
  36. {
  37. return $this->id;
  38. }
  39. /**
  40. * Set's the "upload_id", or 3rd part of the upload's ID. This typically
  41. * only needs to be done after initiating an upload.
  42. *
  43. * @param string $key The param key of the upload_id.
  44. * @param string $value The param value of the upload_id.
  45. */
  46. public function setUploadId($key, $value)
  47. {
  48. $this->id[$key] = $value;
  49. }
  50. /**
  51. * Get the part size.
  52. *
  53. * @return int
  54. */
  55. public function getPartSize()
  56. {
  57. return $this->partSize;
  58. }
  59. /**
  60. * Set the part size.
  61. *
  62. * @param $partSize int Size of upload parts.
  63. */
  64. public function setPartSize($partSize)
  65. {
  66. $this->partSize = $partSize;
  67. }
  68. /**
  69. * Marks a part as being uploaded.
  70. *
  71. * @param int $partNumber The part number.
  72. * @param array $partData Data from the upload operation that needs to be
  73. * recalled during the complete operation.
  74. */
  75. public function markPartAsUploaded($partNumber, array $partData = [])
  76. {
  77. $this->uploadedParts[$partNumber] = $partData;
  78. }
  79. /**
  80. * Returns whether a part has been uploaded.
  81. *
  82. * @param int $partNumber The part number.
  83. *
  84. * @return bool
  85. */
  86. public function hasPartBeenUploaded($partNumber)
  87. {
  88. return isset($this->uploadedParts[$partNumber]);
  89. }
  90. /**
  91. * Returns a sorted list of all the uploaded parts.
  92. *
  93. * @return array
  94. */
  95. public function getUploadedParts()
  96. {
  97. ksort($this->uploadedParts);
  98. return $this->uploadedParts;
  99. }
  100. /**
  101. * Set the status of the upload.
  102. *
  103. * @param int $status Status is an integer code defined by the constants
  104. * CREATED, INITIATED, and COMPLETED on this class.
  105. */
  106. public function setStatus($status)
  107. {
  108. $this->status = $status;
  109. }
  110. /**
  111. * Determines whether the upload state is in the INITIATED status.
  112. *
  113. * @return bool
  114. */
  115. public function isInitiated()
  116. {
  117. return $this->status === self::INITIATED;
  118. }
  119. /**
  120. * Determines whether the upload state is in the COMPLETED status.
  121. *
  122. * @return bool
  123. */
  124. public function isCompleted()
  125. {
  126. return $this->status === self::COMPLETED;
  127. }
  128. }