Skip to content

Commit 1992e3e

Browse files
authored
add *async methods that return lazily-fetched resources (#30)
1 parent 63b5e11 commit 1992e3e

22 files changed

+1328
-85
lines changed

src/Contentful.php

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
use Markup\Contentful\Log\LogInterface;
1919
use Markup\Contentful\Log\NullLogger;
2020
use Markup\Contentful\Log\StandardLogger;
21+
use Markup\Contentful\Promise\AssetPromise;
22+
use Markup\Contentful\Promise\ContentTypePromise;
23+
use Markup\Contentful\Promise\EntryPromise;
24+
use Markup\Contentful\Promise\ResourceArrayPromise;
25+
use Markup\Contentful\Promise\SpacePromise;
2126
use Psr\Cache\CacheItemInterface;
2227
use Psr\Cache\CacheItemPoolInterface;
2328
use Psr\Http\Message\ResponseInterface;
@@ -125,6 +130,18 @@ public function getSpace($space = null, array $options = [])
125130
);
126131
}
127132

133+
/**
134+
* @param string|SpaceInterface $space The space name, or space object.
135+
* @param array $options
136+
* @return SpaceInterface|PromiseInterface
137+
*/
138+
public function getSpaceAsync($space = null, array $options = [])
139+
{
140+
return new SpacePromise(
141+
$this->getSpace($space, array_merge($options, ['async' => true]))
142+
);
143+
}
144+
128145
/**
129146
* @param string $id
130147
* @param string|SpaceInterface $space
@@ -156,11 +173,24 @@ public function getEntry($id, $space = null, array $options = [])
156173
);
157174
}
158175

176+
/**
177+
* @param string $id
178+
* @param string|SpaceInterface $space
179+
* @param array $options
180+
* @return EntryInterface|PromiseInterface
181+
*/
182+
public function getEntryAsync($id, $space = null, array $options = [])
183+
{
184+
return new EntryPromise(
185+
$this->getEntry($id, $space, array_merge($options, ['async' => true]))
186+
);
187+
}
188+
159189
/**
160190
* @param ParameterInterface[] $parameters
161191
* @param string $space
162192
* @param array $options
163-
* @return ResourceArray|EntryInterface[]|PromiseInterface
193+
* @return ResourceArrayInterface|EntryInterface[]|PromiseInterface
164194
* @throws Exception\ResourceUnavailableException
165195
*/
166196
public function getEntries(array $parameters = [], $space = null, array $options = [])
@@ -182,6 +212,19 @@ public function getEntries(array $parameters = [], $space = null, array $options
182212
);
183213
}
184214

215+
/**
216+
* @param ParameterInterface[] $parameters
217+
* @param string $space
218+
* @param array $options
219+
* @return ResourceArrayInterface|PromiseInterface
220+
*/
221+
public function getEntriesAsync(array $parameters = [], $space = null, array $options = [])
222+
{
223+
return new ResourceArrayPromise(
224+
$this->getEntries($parameters, $space, array_merge($options, ['async' => true]))
225+
);
226+
}
227+
185228
/**
186229
* @param string $id
187230
* @param string|SpaceInterface $space
@@ -212,6 +255,19 @@ public function getAsset($id, $space = null, array $options = [])
212255
);
213256
}
214257

258+
/**
259+
* @param string $id
260+
* @param string|SpaceInterface $space
261+
* @param array $options
262+
* @return AssetInterface|PromiseInterface
263+
*/
264+
public function getAssetAsync($id, $space = null, array $options = [])
265+
{
266+
return new AssetPromise(
267+
$this->getAsset($id, $space, array_merge($options, ['async' => true]))
268+
);
269+
}
270+
215271
/**
216272
* @param string $id
217273
* @param string|SpaceInterface $space
@@ -241,6 +297,19 @@ function ($contentTypes) use ($id) {
241297
return (isset($options['async']) && $options['async']) ? $contentTypesPromise : $contentTypesPromise->wait();
242298
}
243299

300+
/**
301+
* @param string $id
302+
* @param string|SpaceInterface $space
303+
* @param array $options
304+
* @return ContentTypeInterface|PromiseInterface
305+
*/
306+
public function getContentTypeAsync($id, $space = null, array $options = [])
307+
{
308+
return new ContentTypePromise(
309+
$this->getContentType($id, $space, array_merge($options, ['async' => true]))
310+
);
311+
}
312+
244313
/**
245314
* @param array $parameters
246315
* @param string|SpaceInterface $space
@@ -274,6 +343,19 @@ public function getContentTypes(array $parameters = [], $space = null, array $op
274343
);
275344
}
276345

346+
/**
347+
* @param array $parameters
348+
* @param string|SpaceInterface $space
349+
* @param array $options
350+
* @return ResourceArray|ContentTypeInterface[]|PromiseInterface
351+
*/
352+
public function getContentTypesAsync(array $parameters = [], $space = null, array $options = [])
353+
{
354+
return new ResourceArrayPromise(
355+
$this->getContentTypes($parameters, $space, array_merge($options, ['async' => true]))
356+
);
357+
}
358+
277359
/**
278360
* Gets a content type using its name. Assumes content types have unique names. Returns null if no content type with the given name can be found.
279361
*
@@ -307,6 +389,21 @@ function () use ($name, $space, $options) {
307389
return (isset($options['async']) && $options['async']) ? $promise : $promise->wait();
308390
}
309391

392+
/**
393+
* Gets a (lazy-fetching) content type using its name. Assumes content types have unique names. Returns null if no content type with the given name can be found.
394+
*
395+
* @param string $name
396+
* @param string|SpaceInterface $space
397+
* @param array $options
398+
* @return ContentTypeInterface|PromiseInterface|null
399+
*/
400+
public function getContentTypeByNameAsync($name, $space = null, array $options = [])
401+
{
402+
return new ContentTypePromise(
403+
$this->getContentTypeByName($name, $space, array_merge($options, ['async' => true]))
404+
);
405+
}
406+
310407
/**
311408
* @param Link $link
312409
* @param array $options
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Markup\Contentful;
4+
5+
/**
6+
* Trait to ensure mutating array access usage is prevented.
7+
*/
8+
trait DisallowArrayAccessMutationTrait
9+
{
10+
/**
11+
* @throws \BadMethodCallException
12+
*/
13+
public function offsetSet($offset, $value)
14+
{
15+
throw new \BadMethodCallException('Cannot set a value using array access');
16+
}
17+
18+
/**
19+
* @throws \BadMethodCallException
20+
*/
21+
public function offsetUnset($offset)
22+
{
23+
throw new \BadMethodCallException('Cannot unset a value');
24+
}
25+
}

src/DynamicEntry.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
class DynamicEntry implements EntryInterface
99
{
10+
use DisallowArrayAccessMutationTrait;
1011
use EntryUnknownMethodTrait;
1112

1213
/**
@@ -54,23 +55,6 @@ public function offsetGet($offset)
5455
return $this->getField($offset);
5556
}
5657

57-
/**
58-
* @param mixed $offset
59-
* @param mixed $value
60-
*/
61-
public function offsetSet($offset, $value)
62-
{
63-
throw new \BadMethodCallException('Cannot set a field using array access');
64-
}
65-
66-
/**
67-
* @param mixed $offset
68-
*/
69-
public function offsetUnset($offset)
70-
{
71-
throw new \BadMethodCallException('Cannot unset a field');
72-
}
73-
7458
/**
7559
* Gets the list of field values in the entry, keyed by fields. Could be scalars, DateTime objects, or links.
7660
*

src/Entry.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
class Entry implements EntryInterface
88
{
9-
use MetadataAccessTrait, EntryUnknownMethodTrait;
9+
use DisallowArrayAccessMutationTrait;
10+
use EntryUnknownMethodTrait;
11+
use MetadataAccessTrait;
1012

1113
/**
1214
* @var array
@@ -105,22 +107,6 @@ public function offsetGet($offset)
105107
return $this->getField($offset);
106108
}
107109

108-
/**
109-
* @throws \BadMethodCallException
110-
*/
111-
public function offsetSet($offset, $value)
112-
{
113-
throw new \BadMethodCallException('Cannot set a field using array access');
114-
}
115-
116-
/**
117-
* @throws \BadMethodCallException
118-
*/
119-
public function offsetUnset($offset)
120-
{
121-
throw new \BadMethodCallException('Cannot unset a field');
122-
}
123-
124110
/**
125111
* Sets a function that can return a promise for a resolved link.
126112
*

src/Promise/AssetPromise.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Markup\Contentful\Promise;
4+
5+
use GuzzleHttp\Promise\PromiseInterface;
6+
use Markup\Contentful\Asset;
7+
use Markup\Contentful\AssetInterface;
8+
use Markup\Contentful\ImageApiOptions;
9+
use Markup\Contentful\Metadata;
10+
11+
/**
12+
* An asset implementation that is also a promise allowing deferred or pooled fetching of the underlying data.
13+
*/
14+
class AssetPromise extends ResourcePromise implements AssetInterface
15+
{
16+
/**
17+
* @return string
18+
*/
19+
public function getTitle()
20+
{
21+
$resolved = $this->getResolved();
22+
if (!$resolved instanceof AssetInterface) {
23+
return '';
24+
}
25+
26+
return $resolved->getTitle();
27+
}
28+
29+
/**
30+
* @return string
31+
*/
32+
public function getDescription()
33+
{
34+
$resolved = $this->getResolved();
35+
if (!$resolved instanceof AssetInterface) {
36+
return '';
37+
}
38+
39+
return $resolved->getDescription();
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
public function getFilename()
46+
{
47+
$resolved = $this->getResolved();
48+
if (!$resolved instanceof AssetInterface) {
49+
return '';
50+
}
51+
52+
return $resolved->getFilename();
53+
}
54+
55+
/**
56+
* @param array|ImageApiOptions $options Options for rendering the image using the Image API @see http://docs.contentfulimagesapi.apiary.io/
57+
* @return string
58+
*/
59+
public function getUrl($imageApiOptions = null)
60+
{
61+
$resolved = $this->getResolved();
62+
if (!$resolved instanceof AssetInterface) {
63+
return '';
64+
}
65+
66+
return $resolved->getUrl($imageApiOptions);
67+
}
68+
69+
/**
70+
* @return array
71+
*/
72+
public function getDetails()
73+
{
74+
$resolved = $this->getResolved();
75+
if (!$resolved instanceof AssetInterface) {
76+
return [];
77+
}
78+
79+
return $resolved->getDetails();
80+
}
81+
82+
/**
83+
* @return int
84+
*/
85+
public function getFileSizeInBytes()
86+
{
87+
$resolved = $this->getResolved();
88+
if (!$resolved instanceof AssetInterface) {
89+
return 0;
90+
}
91+
92+
return $resolved->getFileSizeInBytes();
93+
}
94+
95+
/**
96+
* @param PromiseInterface $promise
97+
* @return PromiseInterface
98+
*/
99+
protected function addRejectionHandlerToPromise(PromiseInterface $promise)
100+
{
101+
return $promise
102+
->otherwise(function ($reason) {
103+
return new Asset('', '', null, new Metadata());
104+
});
105+
}
106+
}

0 commit comments

Comments
 (0)