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