Passed
Pull Request — main (#152)
by Daniel
05:42 queued 01:54
created

MercureIriConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
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