Passed
Pull Request — main (#152)
by Daniel
06:28
created

PublishableIriConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 8
c 1
b 1
f 0
dl 0
loc 24
ccs 0
cts 10
cp 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getResourceFromIri() 0 3 1
A getIriFromResource() 0 13 4
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\ApiPlatform\Api;
15
16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Api\UrlGeneratorInterface;
18
use ApiPlatform\Metadata\Operation;
19
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class PublishableIriConverter implements IriConverterInterface
25
{
26
    public function __construct(private IriConverterInterface $decorated, private PublishableStatusChecker $publishableStatusChecker)
27
    {
28
    }
29
30
    public function getResourceFromIri(string $iri, array $context = [], ?Operation $operation = null): object
31
    {
32
        return $this->decorated->getResourceFromIri($iri, $context, $operation);
33
    }
34
35
    public function getIriFromResource($resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null, array $context = []): ?string
36
    {
37
        $iri = $this->decorated->getIriFromResource($resource, $referenceType, $operation, $context);
38
39
        if (\is_string($resource)) {
40
            return $iri;
41
        }
42
43
        if ($this->publishableStatusChecker->getAnnotationReader()->isConfigured($resource) && !$this->publishableStatusChecker->isActivePublishedAt($resource)) {
44
            $iri .= '?draft=1';
45
        }
46
47
        return $iri;
48
    }
49
}
50