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 OpenCloud\Common\Exceptions\InvalidArgumentError;
22: use OpenCloud\ObjectStore\Resource\Container;
23:
24: /**
25: * Factory which creates Transfer objects, either ConcurrentTransfer or ConsecutiveTransfer.
26: */
27: class TransferBuilder
28: {
29: /**
30: * @var Container The container being uploaded to
31: */
32: protected $container;
33:
34: /**
35: * @var EntityBody The data payload.
36: */
37: protected $entityBody;
38:
39: /**
40: * @var array A key/value pair of options.
41: */
42: protected $options = array();
43:
44: /**
45: * @return TransferBuilder
46: */
47: public static function newInstance()
48: {
49: return new self();
50: }
51:
52: /**
53: * @param type $options Available configuration options:
54: *
55: * * `concurrency' <bool> The number of concurrent workers.
56: * * `partSize' <int> The size, in bytes, for the chunk
57: * * `doPartChecksum' <bool> Enable or disable MD5 checksum in request (ETag)
58: *
59: * If you are uploading FooBar, its chunks will have the following naming structure:
60: *
61: * FooBar/1
62: * FooBar/2
63: * FooBar/3
64: *
65: * @return \OpenCloud\ObjectStore\Upload\UploadBuilder
66: */
67: public function setOptions($options)
68: {
69: $this->options = $options;
70:
71: return $this;
72: }
73:
74: /**
75: * @param $key The option name
76: * @param $value The option value
77: * @return $this
78: */
79: public function setOption($key, $value)
80: {
81: $this->options[$key] = $value;
82:
83: return $this;
84: }
85:
86: /**
87: * @param Container $container
88: * @return $this
89: */
90: public function setContainer(Container $container)
91: {
92: $this->container = $container;
93:
94: return $this;
95: }
96:
97: /**
98: * @param EntityBody $entityBody
99: * @return $this
100: */
101: public function setEntityBody(EntityBody $entityBody)
102: {
103: $this->entityBody = $entityBody;
104:
105: return $this;
106: }
107:
108: /**
109: * Build the transfer.
110: *
111: * @return mixed
112: * @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
113: */
114: public function build()
115: {
116: // Validate properties
117: if (!$this->container || !$this->entityBody || !$this->options['objectName']) {
118: throw new InvalidArgumentError('A container, entity body and object name must be set');
119: }
120:
121: // Create TransferState object for later use
122: $transferState = TransferState::factory();
123:
124: // Instantiate Concurrent-/ConsecutiveTransfer
125: $transferClass = isset($this->options['concurrency']) && $this->options['concurrency'] > 1
126: ? __NAMESPACE__ . '\\ConcurrentTransfer'
127: : __NAMESPACE__ . '\\ConsecutiveTransfer';
128:
129: return $transferClass::newInstance()
130: ->setClient($this->container->getClient())
131: ->setEntityBody($this->entityBody)
132: ->setTransferState($transferState)
133: ->setOptions($this->options)
134: ->setOption('containerName', $this->container->getName())
135: ->setOption('containerUrl', $this->container->getUrl())
136: ->setup();
137: }
138: }
139: