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

RequestHeaderPathExpansionChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B needsExpansion() 0 24 6
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