GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 642c1a...b5d278 )
by Sergey
12s
created

LazyMetadataFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.14%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 92
c 0
b 0
f 0
ccs 34
cts 35
cp 0.9714
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasMetadataFor() 0 14 5
B getMetadataFor() 0 39 11
1
<?php
2
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) Sergey Revenko <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Reva2\JsonApi\Decoders\Mapping\Factory;
13
14
use Reva2\JsonApi\Contracts\Decoders\Mapping\Cache\CacheInterface;
15
use Reva2\JsonApi\Contracts\Decoders\Mapping\ClassMetadataInterface;
16
use Reva2\JsonApi\Contracts\Decoders\Mapping\Factory\MetadataFactoryInterface;
17
use Reva2\JsonApi\Contracts\Decoders\Mapping\GenericMetadataInterface;
18
use Reva2\JsonApi\Contracts\Decoders\Mapping\Loader\LoaderInterface;
19
20
/**
21
 * JSON API metadata factory
22
 *
23
 * @package Reva2\JsonApi\Decoders\Mapping\Factory
24
 * @author Sergey Revenko <[email protected]>
25
 */
26
class LazyMetadataFactory implements MetadataFactoryInterface
27
{
28
    /**
29
     * @var LoaderInterface
30
     */
31
    protected $loader;
32
33
    /**
34
     * @var CacheInterface
35
     */
36
    protected $cache;
37
38
    /**
39
     * Loaded metadata indexed by class name
40
     *
41
     * @var GenericMetadataInterface[]
42
     */
43
    protected $loadedClasses = [];
44
45
    /**
46
     * Constructor
47
     *
48
     * @param LoaderInterface $loader
49
     * @param CacheInterface|null $cache
50
     */
51 22
    public function __construct(LoaderInterface $loader, CacheInterface $cache = null)
52
    {
53 22
        $this->loader = $loader;
54 22
        $this->cache = $cache;
55 22
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 11
    public function getMetadataFor($value)
61
    {
62 11
        if (!is_object($value) && !is_string($value)) {
63 1
            throw new \InvalidArgumentException(sprintf(
64 1
                "Cannot create metadata for non-objects. Got: %s",
65 1
                gettype($value)
66 1
            ));
67
        }
68
        
69 11
        $class = ltrim(((is_object($value)) ? get_class($value) : $value), '\\');
70
        
71 11
        if (array_key_exists($value, $this->loadedClasses)) {
72 6
            return $this->loadedClasses[$class];
73
        }
74
        
75 11
        if ((null !== $this->cache) && (false !== ($this->loadedClasses[$class] = $this->cache->read($class)))) {
76
            return $this->loadedClasses[$class];
77
        }
78
        
79 11
        if (!class_exists($class)) {
80 1
            throw new \RuntimeException(sprintf(
81 1
                "The class '%s' doesn't exist",
82
                $class
83 1
            ));
84
        }
85
        
86 10
        $reflection = new \ReflectionClass($class);
87
        
88 10
        $metadata = $this->loader->loadClassMetadata($reflection);
89 10
        if (($metadata instanceof ClassMetadataInterface) && (false !== ($parent = $reflection->getParentClass()))) {
90 8
            $metadata->mergeMetadata($this->getMetadataFor($parent->getName()));
91 8
        }
92
93 10
        if (null !== $this->cache) {
94 1
            $this->cache->write($metadata);
95 1
        }
96
97 10
        return $this->loadedClasses[$class] = $metadata;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 2
    public function hasMetadataFor($value)
104
    {
105 2
        if (!is_object($value) && !is_string($value)) {
106 1
            return false;
107
        }
108
109 1
        $class = ltrim(((is_object($value)) ? get_class($value) : $value), '\\');
110
111 1
        if (class_exists($class)) {
112 1
            return true;
113
        }
114
115 1
        return false;
116
    }
117
}
118