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