Passed
Push — master ( 1316c5...63993c )
by Matthew
05:18
created

GridSourceManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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