CustomPageUrlPlaceholderReplacementRenderer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFallbackPlaceholderReplacement() 0 3 1
A __construct() 0 3 1
A isApplicable() 0 4 2
A getPlaceholderReplacement() 0 14 3
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\Coremedia\ApiResponse\Renderer;
9
10
use Generated\Shared\Transfer\CoremediaPlaceholderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ediaPlaceholderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\Routing\Exception\RouteNotFoundException;
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
14
class CustomPageUrlPlaceholderReplacementRenderer implements PlaceholderReplacementRendererInterface
15
{
16
    protected const PLACEHOLDER_OBJECT_TYPE = 'page';
17
    protected const PLACEHOLDER_RENDER_TYPE = 'url';
18
19
    /**
20
     * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface
21
     */
22
    protected $urlGenerator;
23
24
    /**
25
     * @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $urlGenerator
26
     */
27
    public function __construct(UrlGeneratorInterface $urlGenerator)
28
    {
29
        $this->urlGenerator = $urlGenerator;
30
    }
31
32
    /**
33
     * @param \Generated\Shared\Transfer\CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer
34
     *
35
     * @return bool
36
     */
37
    public function isApplicable(CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer): bool
38
    {
39
        return $coreMediaPlaceholderTransfer->getObjectType() === static::PLACEHOLDER_OBJECT_TYPE &&
40
            $coreMediaPlaceholderTransfer->getRenderType() === static::PLACEHOLDER_RENDER_TYPE;
41
    }
42
43
    /**
44
     * @param \Generated\Shared\Transfer\CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer
45
     * @param string $locale
46
     *
47
     * @return string|null
48
     */
49
    public function getPlaceholderReplacement(
50
        CoremediaPlaceholderTransfer $coreMediaPlaceholderTransfer,
51
        string $locale
52
    ): ?string {
53
        if (!$coreMediaPlaceholderTransfer->getExternalSeoSegment()) {
54
            return null;
55
        }
56
57
        try {
58
            $pageUrl = $this->urlGenerator->generate($coreMediaPlaceholderTransfer->getExternalSeoSegment());
59
60
            return $pageUrl;
61
        } catch (RouteNotFoundException $e) {
62
            return null;
63
        }
64
    }
65
66
    /**
67
     * @return string|null
68
     */
69
    public function getFallbackPlaceholderReplacement(): ?string
70
    {
71
        return '';
72
    }
73
}
74