Completed
Push — master ( 131c91...75b368 )
by Kévin
03:30
created

OperationPathResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\PathResolver;
15
16
use ApiPlatform\Core\Api\OperationType;
17
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper;
18
use ApiPlatform\Core\Exception\InvalidArgumentException;
19
use ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface;
20
21
/**
22
 * Generates an operation path.
23
 *
24
 * @author Antoine Bluchet <[email protected]>
25
 */
26
final class OperationPathResolver implements OperationPathResolverInterface
27
{
28
    private $pathSegmentNameGenerator;
29
30
    public function __construct(PathSegmentNameGeneratorInterface $pathSegmentNameGenerator)
31
    {
32
        $this->pathSegmentNameGenerator = $pathSegmentNameGenerator;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function resolveOperationPath(string $resourceShortName, array $operation, $operationType/*, string $operationName = null*/): string
39
    {
40
        if (func_num_args() < 4) {
41
            @trigger_error(sprintf('Method %s() will have a 4th `string $operationName` argument in version 3.0. Not defining it is deprecated since 2.1.', __METHOD__), E_USER_DEPRECATED);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
42
        }
43
44
        $operationType = OperationTypeDeprecationHelper::getOperationType($operationType);
45
46
        if (OperationType::SUBRESOURCE === $operationType) {
47
            throw new InvalidArgumentException('Subresource operations are not supported by the OperationPathResolver.');
48
        }
49
50
        $path = '/'.$this->pathSegmentNameGenerator->getSegmentName($resourceShortName, true);
51
52
        if ($operationType === OperationType::ITEM) {
53
            $path .= '/{id}';
54
        }
55
56
        $path .= '.{_format}';
57
58
        return $path;
59
    }
60
}
61