Completed
Push — ezp26352-skip_csrf_check_on_re... ( 19f37a )
by
unknown
36:29
created

needsExpansion()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Publish\Core\REST\Server\Output\PathExpansion;
7
8
use eZ\Publish\Core\MVC\Symfony\RequestStackAware;
9
10
/**
11
 * Checks if a resource link needs to be expanded based on the contents of the x-ez-embed-value
12
 * header from the master request.
13
 */
14
class RequestHeaderPathExpansionChecker implements PathExpansionChecker
15
{
16
    use RequestStackAware;
17
18
    /**
19
     * Tests if the link at $documentPath must be expanded.
20
     *
21
     * @param string $documentPath Path in a rest generator (example: Content.Owner).
22
     *
23
     * @return bool
24
     */
25
    public function needsExpansion($documentPath)
26
    {
27
        $request = $this->getRequestStack()->getMasterRequest();
28
29
        if (is_null($request)) {
30
            return false;
31
        }
32
        if (!$request->headers->has('x-ez-embed-value')) {
33
            return false;
34
        }
35
36
        $documentPath = strtolower($documentPath);
37
        foreach (explode(',', $request->headers->get('x-ez-embed-value')) as $requestedPath) {
38
            $requestedPath = strtolower($requestedPath);
39
            if ($requestedPath === $documentPath) {
40
                return true;
41
            }
42
            if (substr($requestedPath, 0, strlen($documentPath)) === $documentPath) {
43
                return true;
44
            }
45
        }
46
47
        return false;
48
    }
49
}
50