EntityMapBuilderController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 143
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDoctrineObjectManager() 0 4 1
A __construct() 0 5 1
B buildAction() 0 35 5
A clearAction() 0 17 2
A setDoctrineObjectManager() 0 6 1
A getEntityMapCache() 0 4 1
A setEntityMapCache() 0 6 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Doctrine\Controller;
7
8
use Zend\Mvc\Controller\AbstractConsoleController;
9
use Zend\Console\Request;
10
use Interop\Container\ContainerInterface;
11
use Zend\View\Model\ConsoleModel;
12
use Nnx\Doctrine\Utils\EntityMapCacheInterface;
13
14
/**
15
 * Class EntityMapBuilderController
16
 *
17
 * @package Nnx\Doctrine\Controller
18
 */
19
class EntityMapBuilderController extends AbstractConsoleController
20
{
21
    /**
22
     * Менеджер для получения ObjectManager Doctrine2
23
     *
24
     * @var ContainerInterface
25
     */
26
    protected $doctrineObjectManager;
27
28
    /**
29
     * Сервис для кеширования EntityMap
30
     *
31
     * @var EntityMapCacheInterface
32
     */
33
    protected $entityMapCache;
34
35
    /**
36
     * EntityMapBuilderController constructor.
37
     *
38
     * @param ContainerInterface      $doctrineObjectManager
39
     * @param EntityMapCacheInterface $entityMapCache
40
     */
41
    public function __construct(ContainerInterface $doctrineObjectManager, EntityMapCacheInterface $entityMapCache)
42
    {
43
        $this->setDoctrineObjectManager($doctrineObjectManager);
44
        $this->setEntityMapCache($entityMapCache);
45
    }
46
47
48
    /**
49
     * Генерация карты сущностей и сохранение ее в кеше
50
     *
51
     * @return array
52
     */
53
    public function buildAction()
54
    {
55
        /** @var Request $request */
56
        $request = $this->getRequest();
57
        $managerName = $request->getParam('objectManager', 'doctrine.entitymanager.orm_default');
58
59
        if (!$this->getDoctrineObjectManager()->has($managerName)) {
60
            return [
61
                ConsoleModel::RESULT => sprintf('Doctrine ObjectManager %s not found', $managerName)
62
            ];
63
        }
64
65
        $entityMapCache = $this->getEntityMapCache();
66
        if ($entityMapCache->hasEntityMap($managerName)) {
67
            $entityMapCache->deleteEntityMap($managerName);
68
        }
69
        $entityMapCache->saveEntityMap($managerName);
70
71
72
        $entityMap = $entityMapCache->loadEntityMap($managerName);
73
74
        if (is_array($entityMap)) {
75
            $result = "Entity map:\n";
76
            foreach ($entityMap as $interfaceName => $className) {
77
                $result .= sprintf("Interface  name: %s. Class name: %s \n", $interfaceName, $className);
78
            }
79
        } else {
80
            $result = 'Empty entity map';
81
        }
82
83
84
        return [
85
            ConsoleModel::RESULT => $result
86
        ];
87
    }
88
89
90
    /**
91
     * Очистка карты сущностей из кеша
92
     *
93
     * @return array
94
     */
95
    public function clearAction()
96
    {
97
        /** @var Request $request */
98
        $request = $this->getRequest();
99
        $managerName = $request->getParam('objectManager', 'doctrine.entitymanager.orm_default');
100
101
        $entityMapCache = $this->getEntityMapCache();
102
        $result = "Entity map not found\n";
103
        if ($entityMapCache->hasEntityMap($managerName)) {
104
            $entityMapCache->deleteEntityMap($managerName);
105
            $result = "Entity map clear\n";
106
        }
107
108
        return [
109
            ConsoleModel::RESULT => $result
110
        ];
111
    }
112
113
114
    /**
115
     * Возвращает менеджер для получения ObjectManager Doctrine2
116
     *
117
     * @return ContainerInterface
118
     */
119
    public function getDoctrineObjectManager()
120
    {
121
        return $this->doctrineObjectManager;
122
    }
123
124
    /**
125
     * Устанавливает менеджер для получения ObjectManager Doctrine2
126
     *
127
     * @param ContainerInterface $doctrineObjectManager
128
     *
129
     * @return $this
130
     */
131
    public function setDoctrineObjectManager(ContainerInterface $doctrineObjectManager)
132
    {
133
        $this->doctrineObjectManager = $doctrineObjectManager;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Возвращает сервис для кеширования EntityMap
140
     *
141
     * @return EntityMapCacheInterface
142
     */
143
    public function getEntityMapCache()
144
    {
145
        return $this->entityMapCache;
146
    }
147
148
    /**
149
     * Устанавливает сервис для кеширования EntityMap
150
     *
151
     * @param EntityMapCacheInterface $entityMapCache
152
     *
153
     * @return $this
154
     */
155
    public function setEntityMapCache(EntityMapCacheInterface $entityMapCache)
156
    {
157
        $this->entityMapCache = $entityMapCache;
158
159
        return $this;
160
    }
161
}
162