GridSourceManager   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 31
eloc 61
c 3
b 1
f 0
dl 0
loc 169
rs 9.92

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setMongodbRegistry() 0 3 1
A add() 0 3 1
A getGridSource() 0 18 5
B get() 0 28 11
A setReader() 0 3 1
A all() 0 3 2
A setRegistry() 0 3 1
A setEntityManager() 0 3 1
A getGridSourceFromColumnSourceInfo() 0 18 3
A setDocumentManager() 0 3 1
A setReflectionAllowedEntities() 0 3 3
A __construct() 0 5 1
1
<?php
2
3
namespace Dtc\GridBundle\Manager;
4
5
use Doctrine\Common\Annotations\Reader;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Annotations\Reader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Doctrine\ODM\MongoDB\DocumentManager;
0 ignored issues
show
Bug introduced by
The type Doctrine\ODM\MongoDB\DocumentManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Doctrine\ORM\EntityManager;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Dtc\GridBundle\Grid\Source\ColumnSource;
10
use Dtc\GridBundle\Grid\Source\ColumnSourceInfo;
11
use Dtc\GridBundle\Grid\Source\DocumentGridSource;
12
use Dtc\GridBundle\Grid\Source\EntityGridSource;
13
use Dtc\GridBundle\Grid\Source\GridSourceInterface;
14
15
class GridSourceManager
16
{
17
    protected $sourcesByClass;
18
    protected $sourcesByName;
19
20
    protected $reader;
21
22
    protected $registry;
23
24
    protected $mongodbRegistry;
25
26
    protected $customManagerMappings;
27
28
    protected $extraGridSources;
29
30
    protected $columnSource;
31
    /**
32
     * @var array|null Null means all entities allowed, empty array means no entities allowed
33
     */
34
    protected $reflectionAllowedEntities;
35
36
    /**
37
     * GridSourceManager constructor.
38
     */
39
    public function __construct(ColumnSource $columnSource)
40
    {
41
        $this->columnSource = $columnSource;
42
        $this->reflectionAllowedEntities = [];
43
        $this->sources = [];
0 ignored issues
show
Bug Best Practice introduced by
The property sources does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
    }
45
46
    public function setReader(Reader $reader)
47
    {
48
        $this->reader = $reader;
49
    }
50
51
    /**
52
     * @param array|string $allowedEntities Array of allowed entities or string '*' to allow all entities. Use empty array to specify no entities allowed for reflection.
53
     */
54
    public function setReflectionAllowedEntities($allowedEntities)
55
    {
56
        $this->reflectionAllowedEntities = is_array($allowedEntities) ? array_flip($allowedEntities) : ('*' === $allowedEntities ? null : []);
57
    }
58
59
    public function setRegistry($registry)
60
    {
61
        $this->registry = $registry;
62
    }
63
64
    public function setMongodbRegistry($registry)
65
    {
66
        $this->mongodbRegistry = $registry;
67
    }
68
69
    public function add($id, GridSourceInterface $gridSource)
70
    {
71
        $this->extraGridSources[$id] = $gridSource;
72
    }
73
74
    /**
75
     * @param $entityOrDocument
76
     *z
77
     *
78
     * @return DocumentGridSource|EntityGridSource|null
79
     *
80
     * @throws \Exception
81
     */
82
    protected function getGridSource($manager, $entityOrDocument)
83
    {
84
        $repository = $manager->getRepository($entityOrDocument);
85
        if ($repository) {
86
            $className = $repository->getClassName();
87
            $classMetadata = $manager->getClassMetadata($className);
88
            $params = [$manager, $entityOrDocument, null === $this->reflectionAllowedEntities || isset($this->reflectionAllowedEntities[$entityOrDocument])];
89
            if ($this->reader) {
90
                $params[] = $this->reader;
91
            }
92
            $columnSourceInfo = call_user_func_array([$this->columnSource, 'getColumnSourceInfo'], $params);
93
            $name = $classMetadata->getName();
94
            if ($columnSourceInfo) {
95
                return $this->getGridSourceFromColumnSourceInfo($manager, $className, $name, $columnSourceInfo);
96
            }
97
        }
98
99
        return null;
100
    }
101
102
    /**
103
     * @param $manager
104
     * @param $className
105
     * @param $name
106
     *
107
     * @return DocumentGridSource|EntityGridSource
108
     *
109
     * @throws \Exception
110
     */
111
    private function getGridSourceFromColumnSourceInfo($manager, $className, $name, ColumnSourceInfo $columnSourceInfo)
112
    {
113
        if ($manager instanceof EntityManagerInterface) {
114
            $gridSource = new EntityGridSource($manager, $name);
115
        } else {
116
            if (!$manager instanceof DocumentManager) {
117
                throw new \Exception('Unknown ObjectManager type: '.get_class($manager));
118
            }
119
            $gridSource = new DocumentGridSource($manager, $name);
120
        }
121
        $gridSource->setIdColumn($columnSourceInfo->idColumn);
122
        $gridSource->setColumns($columnSourceInfo->columns);
123
        $this->sourcesByName[$name] = $gridSource;
124
        $this->sourcesByClass[$className] = $gridSource;
125
        $gridSource->setId($className);
126
        $gridSource->setDefaultSort($columnSourceInfo->sort);
127
128
        return $gridSource;
129
    }
130
131
    /**
132
     * Get a gridsource.
133
     *
134
     * @param string                             $id      Entity or Document
135
     * @param EntityManager|DocumentManager|null $manager (optional) Entity or Document manager to use (overrides default)
136
     *
137
     * @return GridSourceInterface|null
138
     *
139
     * @throws \Exception
140
     */
141
    public function get($entityOrDocumentNameOrId)
142
    {
143
        // @Support legacy method of adding/removing grid sources
144
        if (isset($this->extraGridSources[$entityOrDocumentNameOrId])) {
145
            return $this->extraGridSources[$entityOrDocumentNameOrId];
146
        }
147
148
        if (isset($this->sourcesByClass[$entityOrDocumentNameOrId])) {
149
            return $this->sourcesByClass[$entityOrDocumentNameOrId];
150
        }
151
152
        if (isset($this->sourcesByName[$entityOrDocumentNameOrId])) {
153
            return $this->sourcesByName[$entityOrDocumentNameOrId];
154
        }
155
156
        try {
157
            if ($this->registry && ($manager = $this->registry->getManagerForClass($entityOrDocumentNameOrId)) &&
158
                $gridSource = $this->getGridSource($manager, $entityOrDocumentNameOrId)) {
159
                return $gridSource;
160
            }
161
        } catch (\ReflectionException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
162
        }
163
164
        if ($this->mongodbRegistry && ($manager = $this->mongodbRegistry->getManagerForClass($entityOrDocumentNameOrId)) &&
165
            $gridSource = $this->getGridSource($manager, $entityOrDocumentNameOrId)) {
166
            return $gridSource;
167
        }
168
        throw new \Exception("Can't find grid source for $entityOrDocumentNameOrId");
169
    }
170
171
    public function all()
172
    {
173
        return isset($this->sourcesByName) ? array_values($this->sourcesByName) : [];
174
    }
175
176
    public function setEntityManager(EntityManager $entityManager)
177
    {
178
        $this->entityManager = $entityManager;
0 ignored issues
show
Bug Best Practice introduced by
The property entityManager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
179
    }
180
181
    public function setDocumentManager(DocumentManager $documentManager)
182
    {
183
        $this->documentManager = $documentManager;
0 ignored issues
show
Bug Best Practice introduced by
The property documentManager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
184
    }
185
}
186