Passed
Push — main ( c28b1c...6868cf )
by Daniel
04:09
created

MercureIriConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResourceFromIri() 0 3 1
A __construct() 0 2 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 MercureIriConverter 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