Completed
Push — 7.5 ( b0871e...08bd2d )
by Łukasz
18:14
created

PermissionAwareLocationResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveLocation() 0 23 5
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\LocationResolver;
10
11
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
13
use eZ\Publish\API\Repository\LocationService;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\Content\Location;
16
use eZ\Publish\Core\Base\Exceptions\NotFoundException as CoreNotFoundException;
17
18
/**
19
 * @internal For internal use by eZ Platform core packages
20
 */
21
final class PermissionAwareLocationResolver implements LocationResolver
22
{
23
    /** @var \eZ\Publish\API\Repository\LocationService */
24
    private $locationService;
25
26
    public function __construct(LocationService $locationService)
27
    {
28
        $this->locationService = $locationService;
29
    }
30
31
    /**
32
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
33
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
34
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
35
     */
36
    public function resolveLocation(ContentInfo $contentInfo): Location
37
    {
38
        try {
39
            if (null === $contentInfo->mainLocationId) {
40
                throw new CoreNotFoundException('location', $contentInfo->mainLocationId);
41
            }
42
43
            $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
44
        } catch (NotFoundException | UnauthorizedException $e) {
45
            // try different locations if main location is not accessible for the user
46
            $locations = $this->locationService->loadLocations($contentInfo);
47
            if (empty($locations)) {
48
                throw $e;
49
            }
50
51
            // foreach to keep forward compatibility with a type of returned loadLocations() result
52
            foreach ($locations as $location) {
53
                return $location;
54
            }
55
        }
56
57
        return $location;
0 ignored issues
show
Bug introduced by
The variable $location does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
58
    }
59
}
60