|
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; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: