1
+ <?php
2
+
3
+ namespace Mdiyakov \DoctrineSolrBundle \Command ;
4
+
5
+ use Doctrine \Bundle \DoctrineBundle \Registry ;
6
+ use Doctrine \ORM \EntityNotFoundException ;
7
+ use Mdiyakov \DoctrineSolrBundle \Config \Config ;
8
+ use Mdiyakov \DoctrineSolrBundle \Exception \EntityNotIndexedException ;
9
+ use Mdiyakov \DoctrineSolrBundle \Query \UpdateQueryBuilder ;
10
+ use Symfony \Component \Console \Input \InputArgument ;
11
+ use Symfony \Component \Console \Input \InputInterface ;
12
+ use Symfony \Component \Console \Output \OutputInterface ;
13
+ use Symfony \Component \Console \Command \Command ;
14
+
15
+ class ClearIndexCommand extends Command
16
+ {
17
+
18
+ /**
19
+ * @var OutputInterface
20
+ */
21
+ private $ output ;
22
+
23
+ /**
24
+ * @var Config
25
+ */
26
+ private $ config ;
27
+
28
+ /**
29
+ * @var UpdateQueryBuilder
30
+ */
31
+ private $ updateQueryBuilder ;
32
+
33
+ /**
34
+ * @var Registry
35
+ */
36
+ private $ registry ;
37
+
38
+ /**
39
+ * @param Config $config
40
+ * @param UpdateQueryBuilder $updateQueryBuilder
41
+ * @param Registry $registry
42
+ */
43
+ public function __construct (
44
+ Config $ config ,
45
+ UpdateQueryBuilder $ updateQueryBuilder ,
46
+ Registry $ registry
47
+ )
48
+ {
49
+ $ this ->config = $ config ;
50
+ $ this ->possibleEntityTypes = array_keys ($ this ->getAssocEntitiesClasses ());
51
+ $ this ->updateQueryBuilder = $ updateQueryBuilder ;
52
+ $ this ->registry = $ registry ;
53
+
54
+ parent ::__construct ();
55
+ }
56
+
57
+ /**
58
+ * {@inheritdoc}
59
+ */
60
+ protected function configure ()
61
+ {
62
+ $ this
63
+ ->setName ('doctrine-solr:clear-index ' )
64
+ ->addArgument (
65
+ 'entity-type ' ,
66
+ InputArgument::OPTIONAL ,
67
+ 'Specify type of entity to be removed. Possible values are "all", " ' . join ('"," ' , $ this ->possibleEntityTypes ),
68
+ 'all '
69
+ )
70
+ ->addArgument (
71
+ 'id ' ,
72
+ InputArgument::OPTIONAL ,
73
+ 'Specify id of entity to be removed. Value must be integer. Also entity-type must be specify '
74
+ )
75
+ ;
76
+ }
77
+
78
+ /**
79
+ * {@inheritdoc}
80
+ */
81
+ protected function execute (InputInterface $ input , OutputInterface $ output )
82
+ {
83
+ $ this ->output = $ output ;
84
+ $ entityType = $ input ->getArgument ('entity-type ' );
85
+ $ entityId = (int ) $ input ->getArgument ('id ' );
86
+
87
+ if ($ entityType == 'all ' ) {
88
+ foreach ($ this ->config ->getIndexedEntities () as $ entityConfig ) {
89
+ $ this ->deleteByEntityConfig (
90
+ $ entityConfig
91
+ );
92
+ }
93
+ } else {
94
+ $ entitiesClasses = $ this ->getAssocEntitiesClasses ();
95
+ if (!array_key_exists ($ entityType , $ entitiesClasses )) {
96
+ throw new \Exception ('There is no such possible entity-type. Check help section for possible values ' );
97
+ }
98
+ $ entityClass = $ entitiesClasses [$ entityType ];
99
+ $ entityConfig = $ this ->config ->getEntityConfig ($ entityClass );
100
+ if (!$ entityConfig ) {
101
+ throw new EntityNotIndexedException (
102
+ sprintf ('Entity class %s is not found in config ' , $ entityClass )
103
+ );
104
+ }
105
+
106
+ $ this ->deleteByEntityConfig (
107
+ $ entityConfig ,
108
+ $ entityId
109
+ );
110
+ }
111
+ }
112
+
113
+ /**
114
+ * @return string[]
115
+ */
116
+ private function getAssocEntitiesClasses ()
117
+ {
118
+ $ entitiesConfigs = $ this ->config ->getIndexedEntities ();
119
+
120
+ $ result = [];
121
+ foreach ($ entitiesConfigs as $ entityKey => $ entityConfig ) {
122
+ $ result [$ entityKey ] = $ entityConfig ['class ' ];
123
+ }
124
+
125
+ return $ result ;
126
+ }
127
+
128
+ /**
129
+ * @param string[][] $entityConfig
130
+ * @param null|int $id
131
+ * @throws EntityNotFoundException
132
+ */
133
+ private function deleteByEntityConfig ($ entityConfig , $ id = null )
134
+ {
135
+ $ schemaName = $ entityConfig ['schema ' ];
136
+ $ updateQuery = $ this ->updateQueryBuilder ->buildUpdateQueryBySchemaName ($ schemaName );
137
+ $ schema = $ this ->config ->getSchemaByName ($ schemaName );
138
+ $ em = $ this ->registry ->getManagerForClass ($ entityConfig ['class ' ]);
139
+
140
+ if (empty ($ id )) {
141
+ $ discriminatorField = $ schema ->getDiscriminatorConfigField ();
142
+ $ discriminatorValue = $ discriminatorField ->getValue ($ entityConfig );
143
+ $ updateQuery ->addDeleteCriteriaByConfigField (
144
+ $ discriminatorField ->getDocumentFieldName (),
145
+ $ discriminatorValue
146
+ );
147
+ $ message = sprintf ('Removing of %s is completed successfully ' , $ entityConfig ['class ' ]);
148
+ } else {
149
+ $ entityClass = $ entityConfig ['class ' ];
150
+ $ repository = $ em ->getRepository ($ entityClass );
151
+ $ entity = $ repository ->find ($ id );
152
+
153
+ if (!$ entity ) {
154
+ throw new EntityNotFoundException (
155
+ sprintf ('% with %s id is not found ' , $ entityClass , $ id )
156
+ );
157
+ }
158
+
159
+ $ updateQuery ->addDeleteCriteriaByUniqueFieldValue (
160
+ $ schema ->getDocumentUniqueField ()->getValue ($ entity , $ entityConfig )
161
+ );
162
+
163
+ $ message = sprintf ('Removing of %s with id %s is completed successfully ' ,
164
+ $ entityClass ,
165
+ $ id
166
+ );
167
+
168
+ }
169
+
170
+ $ updateQuery ->update ();
171
+ $ this ->output ->writeln ($ message );
172
+ }
173
+ }
0 commit comments