Skip to content

Commit 5510c11

Browse files
Grant Ellisshieldo
authored andcommitted
feat(assets): add getAssets and isAssetUnlinked methods
1 parent 74f647c commit 5510c11

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

src/Contentful.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Markup\Contentful\Exception\ResourceUnavailableException;
1616
use Markup\Contentful\Filter\ContentTypeFilterProvider;
1717
use Markup\Contentful\Filter\DecidesCacheKeyInterface;
18+
use Markup\Contentful\Filter\LinksToAssetFilter;
1819
use Markup\Contentful\Filter\LocaleFilter;
1920
use Markup\Contentful\Log\LoggerInterface;
2021
use Markup\Contentful\Log\LogInterface;
@@ -274,6 +275,61 @@ public function getAssetAsync($id, $space = null, array $options = [], $locale =
274275
);
275276
}
276277

278+
/**
279+
* @param array $parameters
280+
* @param null $space
281+
* @param array $options
282+
* @return PromiseInterface|AssetInterface[]
283+
*/
284+
public function getAssets(array $parameters = [], $space = null, array $options = [])
285+
{
286+
$spaceName = ($space instanceof SpaceInterface) ? $space->getName() : $space;
287+
$spaceData = $this->getSpaceDataForName($spaceName);
288+
$api = ($spaceData['preview_mode']) ? self::PREVIEW_API : self::CONTENT_DELIVERY_API;
289+
290+
return $this->doRequest(
291+
$spaceData,
292+
$spaceName,
293+
$this->getEndpointUrl(sprintf('/spaces/%s/assets', $spaceData['key']), $api),
294+
sprintf('The assets from the space "%s" were unavailable.', $spaceName),
295+
$api,
296+
'assets',
297+
'',
298+
$parameters,
299+
$options
300+
);
301+
}
302+
303+
/**
304+
* @param array $parameters
305+
* @param null $space
306+
* @param array $options
307+
* @return ResourceArrayPromise
308+
*/
309+
public function getAssetsAsync(array $parameters = [], $space = null, array $options = [])
310+
{
311+
return new ResourceArrayPromise(
312+
$this->getAssets($parameters, $space, array_merge($options, ['async' => true]))
313+
);
314+
}
315+
316+
/**
317+
* @param string $assetId
318+
* @param string|SpaceInterface $space
319+
* @return bool
320+
*/
321+
public function isAssetUnlinked($assetId, $space = null)
322+
{
323+
$filters = [];
324+
$filters[] = new LinksToAssetFilter($assetId);
325+
326+
$linkedEntries = $this->getEntries($filters, $space);
327+
328+
$totalEntries = $linkedEntries->getTotal();
329+
330+
return ($totalEntries > 0) ? false : true;
331+
}
332+
277333
/**
278334
* @param string $id
279335
* @param string|SpaceInterface $space

src/Filter/LinksToAssetFilter.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Markup\Contentful\Filter;
4+
5+
use Markup\Contentful\FilterInterface;
6+
7+
class LinksToAssetFilter implements FilterInterface
8+
{
9+
use ClassBasedNameTrait;
10+
11+
const KEY = 'links_to_asset';
12+
13+
/**
14+
* @var string
15+
*/
16+
private $value;
17+
18+
/**
19+
* @param string $value
20+
*/
21+
public function __construct($value)
22+
{
23+
$this->value = $value;
24+
}
25+
26+
/**
27+
* The key in a query string on an API request.
28+
*
29+
* @return string
30+
*/
31+
public function getKey()
32+
{
33+
return self::KEY;
34+
}
35+
36+
/**
37+
* The value in a query string on an API request.
38+
*
39+
* @return string
40+
*/
41+
public function getValue()
42+
{
43+
return $this->value;
44+
}
45+
}

tests/ContentfulTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Markup\Contentful\Link;
1818
use Markup\Contentful\Log\LogInterface;
1919
use Markup\Contentful\Metadata;
20+
use Markup\Contentful\Promise\ResourceArrayPromise;
2021
use Markup\Contentful\Property\FieldProperty;
2122
use Markup\Contentful\Property\SystemProperty;
2223
use Markup\Contentful\ResourceArray;
@@ -199,6 +200,32 @@ public function testGetAssetDecorates()
199200
$this->assertSame($asset, $decoratedAsset);
200201
}
201202

203+
public function testGetAssets()
204+
{
205+
$handlerOption = $this->getSuccessHandlerOption($this->getSuccessAssetData(), '235345lj34h53j4h');
206+
$contentful = $this->getContentful(null, array_merge($this->options, $handlerOption));
207+
$assets = $contentful->getAssets();
208+
$this->assertInstanceOf(AssetInterface::class, $assets);
209+
}
210+
211+
public function testGetAssetsAsync()
212+
{
213+
$handlerOption = $this->getSuccessHandlerOption($this->getSuccessAssetData(), '235345lj34h53j4h');
214+
$contentful = $this->getContentful(null, array_merge($this->options, $handlerOption));
215+
$assets = $contentful->getAssetsAsync();
216+
$this->assertInstanceOf(ResourceArrayPromise::class, $assets);
217+
$this->assertInstanceOf(PromiseInterface::class, $assets);
218+
}
219+
220+
public function testIsAssetUnlinked()
221+
{
222+
$handlerOption = $this->getSuccessHandlerOption($this->getEntriesData(), '235345lj34h53j4h');
223+
$contentful = $this->getContentful(null, array_merge($this->options, $handlerOption));
224+
$assetUnlinked = $contentful->isAssetUnlinked('nyancat');
225+
$this->assertInternalType('bool', $assetUnlinked);
226+
$this->assertFalse($assetUnlinked);
227+
}
228+
202229
private function getSuccessAssetData()
203230
{
204231
return [

0 commit comments

Comments
 (0)