ModelRegistry::getShortName()   A
last analyzed

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the "elao/api-resources-metadata" package.
5
 *
6
 * Copyright (C) 2016 Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\ApiResourcesMetadata\Bridge\Nelmio\ApiDoc\Swagger;
12
13
use Elao\ApiResourcesMetadata\Resource\Factory\ResourceMetadataFactory;
14
use Elao\ApiResourcesMetadata\Resource\ResourceIndex;
15
use Nelmio\ApiDocBundle\Swagger\ModelRegistry as NelmioModelRegistry;
16
17
/**
18
 * A special ModelRegistry allowing to hook on model name generation
19
 * and automatically use api resource shortName if available
20
 */
21
class ModelRegistry extends NelmioModelRegistry
22
{
23
    /** @var ResourceMetadataFactory */
24
    private $metadataFactory;
25
26
    /** @var ResourceIndex */
27
    private $resourceIndex;
28
29
    public function __construct($namingStrategy, ResourceIndex $resourceIndex, ResourceMetadataFactory $metadataFactory)
30
    {
31
        parent::__construct($namingStrategy);
32
33
        $this->metadataFactory = $metadataFactory;
34
        $this->resourceIndex = $resourceIndex;
35
    }
36
37
    public function register($className, array $parameters = null, $description = '')
38
    {
39
        if ('' === $className) {
40
            // Fix an issue with a ghost model registered by Nelmio
41
            return;
42
        }
43
44
        if (!class_exists($className) && $this->resourceIndex->has($className)) {
45
            $className = $this->resourceIndex->getResourceClass($className);
46
        }
47
48
        $id = parent::register($className, $parameters, $description);
49
50
        $matches = [];
51
        if (preg_match('/^(.*)\[(.*)\]$/', $className, $matches)) {
52
            list(, $class, $itemsName) = $matches;
53
            if (null !== $shortName = $this->getShortName($class)) {
54
                $collectionShortName = sprintf('%s[%s]', $shortName, $itemsName);
55
                $this->models[$collectionShortName] = $this->models[$id];
56
                $this->models[$collectionShortName]['id'] = $collectionShortName;
57
                $this->models[$collectionShortName]['description'] = $this->metadataFactory->getMetadataFor($class)->getDescription() ?: "array of objects ($shortName)";
58
                unset($this->models[$id]);
59
60
                return $collectionShortName;
61
            }
62
        }
63
64
        if (null !== $shortName = $this->getShortName($className)) {
65
            $this->models[$shortName] = $this->models[$id];
66
            $this->models[$shortName]['id'] = $shortName;
67
            $this->models[$shortName]['description'] = $this->metadataFactory->getMetadataFor($className)->getDescription() ?: "object ($shortName)";
68
            unset($this->models[$id]);
69
70
            return $shortName;
71
        }
72
73
        return $id;
74
    }
75
76
    public function getShortName(string $className)
77
    {
78
        return $this->resourceIndex->has($className) ? $this->resourceIndex->getShortName($className) : null;
79
    }
80
}
81