Completed
Push — master ( f73309...4d4b19 )
by Antoine
03:25
created

DashOperationPathResolver::resolveOperationPath()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 24
Code Lines 14

Duplication

Lines 5
Ratio 20.83 %

Importance

Changes 0
Metric Value
dl 5
loc 24
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 14
nc 12
nop 3
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 Doctrine\Common\Inflector\Inflector;
19
20
/**
21
 * Generates a path with words separated by dashes.
22
 *
23
 * @author Paul Le Corre <[email protected]>
24
 */
25
final class DashOperationPathResolver implements OperationPathResolverInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function resolveOperationPath(string $resourceShortName, array $operation, $operationType): string
31
    {
32
        $operationType = OperationTypeDeprecationHelper::getOperationType($operationType);
33
34 View Code Duplication
        if ($operationType === OperationType::SUBRESOURCE && 1 < count($operation['identifiers'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
            $path = str_replace('.{_format}', '', $resourceShortName);
36
        } else {
37
            $path = '/'.Inflector::pluralize($this->dashize($resourceShortName));
38
        }
39
40
        if ($operationType === OperationType::ITEM) {
41
            $path .= '/{id}';
42
        }
43
44
        if ($operationType === OperationType::SUBRESOURCE) {
45
            list($key) = end($operation['identifiers']);
46
            $property = true === $operation['collection'] ? Inflector::pluralize($this->dashize($operation['property'])) : $this->dashize($operation['property']);
47
            $path .= sprintf('/{%s}/%s', $key, $property);
48
        }
49
50
        $path .= '.{_format}';
51
52
        return $path;
53
    }
54
55
    private function dashize(string $string): string
56
    {
57
        return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '-$1', $string));
58
    }
59
}
60