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.

TransformerManager::getTransformer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace Nimble\ElasticBundle\Transformer;
4
5
use Nimble\ElasticBundle\ClassUtils;
6
use Nimble\ElasticBundle\Document;
7
use Nimble\ElasticBundle\Transformer\Exception\TransformerAlreadyRegisteredException;
8
use Nimble\ElasticBundle\Transformer\Exception\TransformerNotFoundException;
9
10
class TransformerManager
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $transfomers = [];
16
17
    /**
18
     * @param TransformerInterface $transformer
19
     * @param string $indexId
20
     * @param string $typeName
21
     */
22
    public function registerTransformer(TransformerInterface $transformer, $indexId, $typeName)
23
    {
24
        $className = $transformer->getClass();
25
26
        if (isset($this->transfomers[$className][$indexId][$typeName])) {
27
            throw new TransformerAlreadyRegisteredException($className, $indexId, $typeName);
28
        }
29
30
        $this->transfomers[$className][$indexId][$typeName] = $transformer;
31
    }
32
33
    /**
34
     * @param object $entity
35
     * @param string $indexId
36
     * @param string $typeName
37
     * @return TransformerInterface
38
     */
39
    protected function getTransformer($entity, $indexId, $typeName)
40
    {
41
        $className = get_class($entity);
42
43
        $classKey = ClassUtils::findClassKey($className, $this->transfomers);
44
45
        if (!$classKey) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classKey of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
46
            throw new TransformerNotFoundException($className, $indexId, $typeName);
47
        }
48
49
        return $this->transfomers[$classKey][$indexId][$typeName];
50
    }
51
52
    /**
53
     * @param object $entity
54
     * @param string $indexId
55
     * @param string $typeName
56
     * @return Document[]
57
     */
58
    public function transformToDocuments($entity, $indexId, $typeName)
59
    {
60
        return $this->getTransformer($entity, $indexId, $typeName)->transformToDocuments($entity);
61
    }
62
63
    /**
64
     * @param object $entity
65
     * @param string $indexId
66
     * @param string $typeName
67
     * @return string[]|int[]
68
     */
69
    public function transformToIds($entity, $indexId, $typeName)
70
    {
71
        return $this->getTransformer($entity, $indexId, $typeName)->transformToIds($entity);
72
    }
73
}
74