AbstractTransferState.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Aws\Common\Model\MultipartUpload;
  17. use Aws\Common\Exception\RuntimeException;
  18. /**
  19. * State of a multipart upload
  20. */
  21. abstract class AbstractTransferState implements TransferStateInterface
  22. {
  23. /**
  24. * @var UploadIdInterface Object holding params used to identity the upload part
  25. */
  26. protected $uploadId;
  27. /**
  28. * @var array Array of parts where the part number is the index
  29. */
  30. protected $parts = array();
  31. /**
  32. * @var bool Whether or not the transfer was aborted
  33. */
  34. protected $aborted = false;
  35. /**
  36. * Construct a new transfer state object
  37. *
  38. * @param UploadIdInterface $uploadId Upload identifier object
  39. */
  40. public function __construct(UploadIdInterface $uploadId)
  41. {
  42. $this->uploadId = $uploadId;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getUploadId()
  48. {
  49. return $this->uploadId;
  50. }
  51. /**
  52. * Get a data value from the transfer state's uploadId
  53. *
  54. * @param string $key Key to retrieve (e.g. Bucket, Key, UploadId, etc)
  55. *
  56. * @return string|null
  57. */
  58. public function getFromId($key)
  59. {
  60. $params = $this->uploadId->toParams();
  61. return isset($params[$key]) ? $params[$key] : null;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getPart($partNumber)
  67. {
  68. return isset($this->parts[$partNumber]) ? $this->parts[$partNumber] : null;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function addPart(UploadPartInterface $part)
  74. {
  75. $partNumber = $part->getPartNumber();
  76. $this->parts[$partNumber] = $part;
  77. return $this;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function hasPart($partNumber)
  83. {
  84. return isset($this->parts[$partNumber]);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getPartNumbers()
  90. {
  91. return array_keys($this->parts);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function setAborted($aborted)
  97. {
  98. $this->aborted = (bool) $aborted;
  99. return $this;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function isAborted()
  105. {
  106. return $this->aborted;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function count()
  112. {
  113. return count($this->parts);
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getIterator()
  119. {
  120. return new \ArrayIterator($this->parts);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function serialize()
  126. {
  127. return serialize(get_object_vars($this));
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function unserialize($serialized)
  133. {
  134. $data = unserialize($serialized);
  135. foreach (get_object_vars($this) as $property => $oldValue) {
  136. if (array_key_exists($property, $data)) {
  137. $this->{$property} = $data[$property];
  138. } else {
  139. throw new RuntimeException("The {$property} property could be restored during unserialization.");
  140. }
  141. }
  142. }
  143. }