Completed
Push — master ( 209945...41a4bc )
by Matthew
07:41 queued 03:34
created

GridSourceManager::setMongodbRegistry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\GridBundle\Manager;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Doctrine\Common\Persistence\AbstractManagerRegistry;
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\EntityManagerInterface;
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
    protected $cacheDir;
22
    protected $debug;
23
24
    /** @var AbstractManagerRegistry */
25
    protected $registry;
26
27
    /** @var AbstractManagerRegistry */
28
    protected $mongodbRegistry;
29
30
    protected $customManagerMappings;
31
32
    protected $extraGridSources;
33
34
    /**
35
     * GridSourceManager constructor.
36
     *
37
     * @param string $cacheDir
38
     * @param bool   $debug
39
     */
40
    public function __construct(Reader $reader, $cacheDir, $debug = false)
41
    {
42
        $this->cacheDir = $cacheDir;
43
        $this->reader = $reader;
44
        $this->debug = $debug;
45
        $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...
46
    }
47
48
    public function setRegistry(AbstractManagerRegistry $registry)
49
    {
50
        $this->registry = $registry;
51
    }
52
53
    public function setMongodbRegistry(AbstractManagerRegistry $registry)
54
    {
55
        $this->mongodbRegistry = $registry;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    protected function getCacheDir()
62
    {
63
        return $this->cacheDir;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    protected function getDebug()
70
    {
71
        return $this->debug;
72
    }
73
74
    public function add($id, GridSourceInterface $gridSource)
75
    {
76
        $this->extraGridSources[$id] = $gridSource;
77
    }
78
79
    /**
80
     * @param ObjectManager $manager
81
     * @param $entityOrDocument
82
     *
83
     * @return DocumentGridSource|EntityGridSource|null
84
     */
85
    protected function getGridSource($manager, $entityOrDocument)
86
    {
87
        $repository = $manager->getRepository($entityOrDocument);
88
        if ($repository) {
89
            $className = $repository->getClassName();
90
            $classMetadata = $manager->getClassMetadata($className);
91
            $name = $classMetadata->getName();
92
            $reflectionClass = $classMetadata->getReflectionClass();
93
            $annotation = $this->reader->getClassAnnotation($reflectionClass, 'Dtc\GridBundle\Annotation\Grid');
94
            if (!$annotation) {
95
                throw new \Exception("GridSource requested for '$entityOrDocument' but no Grid annotation found");
96
            }
97
            if ($manager instanceof EntityManagerInterface) {
98
                $gridSource = new EntityGridSource($manager, $name);
99
            } else {
100
                $gridSource = new DocumentGridSource($manager, $name);
101
            }
102
            $gridSource->setAnnotationReader($this->reader);
103
            $gridSource->setCacheDir($this->cacheDir);
104
105
            $gridSource->setDebug($this->debug);
106
            $gridSource->autoDiscoverColumns();
107
            $this->sourcesByName[$name] = $gridSource;
108
            $this->sourcesByClass[$className] = $gridSource;
109
            $gridSource->setId($className);
110
111
            return $gridSource;
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * Get a gridsource.
119
     *
120
     * @param string                             $id      Entity or Document
121
     * @param EntityManager|DocumentManager|null $manager (optional) Entity or Document manager to use (overrides default)
122
     *
123
     * @return GridSourceInterface|null
124
     *
125
     * @throws \Exception
126
     */
127
    public function get($entityOrDocumentNameOrId)
128
    {
129
        // @Support legacy method of adding/removing grid sources
130
        if (isset($this->extraGridSources[$entityOrDocumentNameOrId])) {
131
            return $this->extraGridSources[$entityOrDocumentNameOrId];
132
        }
133
134
        if (isset($this->sourcesByClass[$entityOrDocumentNameOrId])) {
135
            return $this->sourcesByClass[$entityOrDocumentNameOrId];
136
        }
137
138
        if (isset($this->sourcesByName[$entityOrDocumentNameOrId])) {
139
            return $this->sourcesByName[$entityOrDocumentNameOrId];
140
        }
141
142
        try {
143 View Code Duplication
            if ($this->registry && ($manager = $this->registry->getManagerForClass($entityOrDocumentNameOrId)) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
                $gridSource = $this->getGridSource($manager, $entityOrDocumentNameOrId)) {
145
                return $gridSource;
146
            }
147
        } catch (\ReflectionException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
148
        }
149
150 View Code Duplication
        if ($this->mongodbRegistry && ($manager = $this->mongodbRegistry->getManagerForClass($entityOrDocumentNameOrId)) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
            $gridSource = $this->getGridSource($manager, $entityOrDocumentNameOrId)) {
152
            return $gridSource;
153
        }
154
        throw new \Exception("Can't find grid source for $entityOrDocumentNameOrId");
155
    }
156
157
    public function all()
158
    {
159
        return isset($this->sourcesByName) ? array_values($this->sourcesByName) : [];
160
    }
161
162
    public function setEntityManager(EntityManager $entityManager)
163
    {
164
        $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...
165
    }
166
167
    public function setDocumentManager(DocumentManager $documentManager)
168
    {
169
        $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...
170
    }
171
}
172