Completed
Push — ezp26297-rest_embedding_http_c... ( 123323 )
by
unknown
80:28 queued 54:58
created

CachedValue::visit()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 39
Code Lines 23

Duplication

Lines 18
Ratio 46.15 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 16
nop 3
dl 18
loc 39
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentList ValueObjectVisitor class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
12
13
use eZ\Publish\Core\MVC\ConfigResolverInterface;
14
use eZ\Publish\Core\MVC\Symfony\RequestStackAware;
15
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor;
16
use eZ\Publish\Core\REST\Common\Output\Generator;
17
use eZ\Publish\Core\REST\Common\Output\Visitor;
18
19
/**
20
 * CachedValue value object visitor.
21
 */
22
class CachedValue extends ValueObjectVisitor
23
{
24
    use RequestStackAware;
25
26
    /** @var ConfigResolverInterface */
27
    protected $configResolver;
28
29
    public function __construct(ConfigResolverInterface $configResolver)
30
    {
31
        $this->configResolver = $configResolver;
32
    }
33
34
    /**
35
     * @param Visitor   $visitor
36
     * @param Generator $generator
37
     * @param \eZ\Publish\Core\REST\Server\Values\CachedValue $data
38
     */
39
    public function visit(Visitor $visitor, Generator $generator, $data)
40
    {
41
        $visitor->visitValueObject($data->value, $generator, $visitor);
42
43
        if ($this->getParameter('content.view_cache') !== true) {
44
            return;
45
        }
46
47
        $response = $visitor->getResponse();
48
        $response->setPublic();
49
        $response->setVary('Accept', false);
50
51
        if ($this->getParameter('content.ttl_cache') === true) {
52
            $response->setSharedMaxAge($this->getParameter('content.default_ttl'));
0 ignored issues
show
Documentation introduced by
$this->getParameter('content.default_ttl') is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
            $request = $this->getCurrentRequest();
54
            if (isset($request) && $request->headers->has('X-User-Hash')) {
55
                $response->setVary('X-User-Hash', false);
56
            }
57
        }
58
59 View Code Duplication
        if (!empty($data->cacheTags)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            // See doc/specifications/cache/multi_tagging.md
61
            $tags = [];
62
            foreach ($data->cacheTags as $tag => $values) {
63
                foreach ((array)$values as $value) {
64
                    $tagValue = $tag . '-' . $value;
65
                    if (!$response->headers->contains('xkey', $tagValue)) {
66
                        $tags[] = $tag . '-' . $value;
67
                    }
68
                }
69
            }
70
71
            $response->headers->set(
72
                'xkey',
73
                $tags,
74
                false
75
            );
76
        }
77
    }
78
79
    public function getParameter($parameterName, $defaultValue = null)
80
    {
81
        if ($this->configResolver->hasParameter($parameterName)) {
82
            return $this->configResolver->getParameter($parameterName);
83
        }
84
85
        return $defaultValue;
86
    }
87
}
88