Passed
Pull Request — master (#2819)
by Han Hui
03:28
created

ResourceClassInfoTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A getResourceClass() 0 13 4
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\Util;
15
16
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
17
18
/**
19
 * Retrieves information about a resource class.
20
 *
21
 * @internal
22
 */
23
trait ResourceClassInfoTrait
24
{
25
    use ClassInfoTrait;
26
27
    /**
28
     * @var ResourceClassResolverInterface|null
29
     */
30
    private $resourceClassResolver;
31
32
    /**
33
     * Gets the resource class of the given object.
34
     *
35
     * @param object $object
36
     * @param bool   $strict If true, object class is expected to be a resource class
37
     *
38
     * @return string|null The resource class, or null if object class is not a resource class
39
     */
40
    private function getResourceClass($object, bool $strict = false): ?string
41
    {
42
        $objectClass = $this->getObjectClass($object);
43
44
        if (null === $this->resourceClassResolver) {
45
            return $objectClass;
46
        }
47
48
        if (!$strict && !$this->resourceClassResolver->isResourceClass($objectClass)) {
49
            return null;
50
        }
51
52
        return $this->resourceClassResolver->getResourceClass($object);
53
    }
54
}
55