     1: <?php
     2: /**
     3:  * Copyright 2012-2014 Rackspace US, Inc.
     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:  * You may obtain a copy of the License at
     8:  *
     9:  * http://www.apache.org/licenses/LICENSE-2.0
    10:  *
    11:  * Unless required by applicable law or agreed to in writing, software
    12:  * distributed under the License is distributed on an "AS IS" BASIS,
    13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14:  * See the License for the specific language governing permissions and
    15:  * limitations under the License.
    16:  */
    17:
    18: namespace OpenCloud\ObjectStore\Upload;
    19:
    20: use Guzzle\Http\EntityBody;
    21: use Guzzle\Http\ReadLimitEntityBody;
    22: use OpenCloud\Common\Constants\Size;
    23:
    24: /**
    25:  * A transfer type which executes consecutively - i.e. it will upload an entire EntityBody and then move on to the next
    26:  * in a linear fashion. There is no concurrency here.
    27:  *
    28:  * @codeCoverageIgnore
    29:  */
    30: class ConsecutiveTransfer extends AbstractTransfer
    31: {
    32:
    33:     public function transfer()
    34:     {
    35:         while (!$this->entityBody->isConsumed()) {
    36:
    37:             if ($this->entityBody->getContentLength() && $this->entityBody->isSeekable()) {
    38:                 // Stream directly from the data
    39:                 $body = new ReadLimitEntityBody($this->entityBody, $this->partSize, $this->entityBody->ftell());
    40:             } else {
    41:                 // If not-seekable, read the data into a new, seekable "buffer"
    42:                 $body = EntityBody::factory();
    43:                 $output = true;
    44:                 while ($body->getContentLength() < $this->partSize && $output !== false) {
    45:                     // Write maximum of 10KB at a time
    46:                     $length = min(10 * Size::KB, $this->partSize - $body->getContentLength());
    47:                     $output = $body->write($this->entityBody->read($length));
    48:                 }
    49:             }
    50:
    51:             if ($body->getContentLength() == 0) {
    52:                 break;
    53:             }
    54:
    55:             $request = TransferPart::createRequest(
    56:                 $body,
    57:                 $this->transferState->count() + 1,
    58:                 $this->client,
    59:                 $this->options
    60:             );
    61:
    62:             $response = $request->send();
    63:
    64:             $this->transferState->addPart(TransferPart::fromResponse($response));
    65:         }
    66:     }
    67: }
    68: