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 ( df54b5...15ba7e )
by Romain
07:00
created

DoctrineOrmObjectManager::findAllSorted()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 11
nop 4
1
<?php
2
3
namespace Halapi\ObjectManager;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
7
/**
8
 * Interface ObjectManagerInterface
9
 * @author Romain Richard
10
 */
11
class DoctrineOrmObjectManager implements ObjectManagerInterface
12
{
13
    /**
14
     * @var ObjectManager
15
     */
16
    private $objectManager;
17
18
    /**
19
     * DoctrineOrmObjectManager constructor.
20
     * @param ObjectManager $objectManager
21
     */
22
    public function __construct(ObjectManager $objectManager)
23
    {
24
        $this->objectManager = $objectManager;
25
    }
26
27
    /**
28
     * @param object $resource
29
     * @return mixed
30
     */
31
    public function getIdentifierName($resource)
32
    {
33
        $classMetadata = $this->objectManager->getClassMetadata(get_class($resource));
34
35
        return $classMetadata->getIdentifier()[0];
36
    }
37
38
    /**
39
     * @param object $resource
40
     * @return mixed
41
     */
42
    public function getIdentifier($resource)
43
    {
44
        $identifier = new \ReflectionProperty($resource, $this->getIdentifier($resource));
45
        if ($identifier->isPublic()) {
46
            return $identifier;
47
        }
48
49
        $getter = 'get'.ucfirst($identifier);
50
        $getterReflection = new \ReflectionMethod($resource, $getter);
51
        if (method_exists($resource, $getter) && $getterReflection->isPublic()) {
52
            return $resource->$getter();
53
        }
54
55
        return;
56
    }
57
58
    /**
59
     * @param string $className
60
     * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata
61
     */
62
    public function getClassMetadata($className)
63
    {
64
        return $this->objectManager->getClassMetadata($className);
65
    }
66
67
    /**
68
     * @param string $className
69
     * @param array  $sorting
70
     * @param array  $filterValues
71
     * @param array  $filerOperators
72
     *
73
     * @return array
74
     */
75
    public function findAllSorted($className, array $sorting, array $filterValues, array $filerOperators)
76
    {
77
        $fields = array_keys($this->getClassMetadata($className)->fieldMappings);
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
78
        $repository = $this->objectManager->getRepository($className);
79
80
        // If user's own implementation is defined, use it
81
        if (is_callable(array($repository, 'findAllSorted'))) {
82
            return $repository->findAllSorted($className, $sorting, $filterValues, $filerOperators);
0 ignored issues
show
Bug introduced by
The method findAllSorted() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findAll()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
83
        }
84
85
        $queryBuilder = $repository->createQueryBuilder('e');
86
87
        foreach ($fields as $field) {
88
            if (isset($sorting[$field])) {
89
                $direction = ($sorting[$field] === 'asc') ? 'asc' : 'desc';
90
                $queryBuilder->addOrderBy('e.'.$field, $direction);
91
            }
92
93
            if (isset($filterValues[$field])) {
94
                $operator = '=';
95
96
                if (isset($filerOperators[$field])
97
                    && in_array($filerOperators[$field], ['>', '<', '>=', '<=', '=', '!='])
98
                ) {
99
                    $operator = $filerOperators[$field];
100
                }
101
102
                $queryBuilder->andWhere('e.'.$field.$operator."'".$filterValues[$field]."'");
103
            }
104
        }
105
106
        return [$queryBuilder];
107
    }
108
}
109