| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace CCT\Component\ODMElasticsearch\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Elastica\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Elastica\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Elastica\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Elastica\Index as BaseIndex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Elastica\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Use Type methods in index class as there is only one type allowed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @see     \Elastica\Type          to have the access to the methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @method  \Elastica\Response               addDocument(Document $doc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @method  \Elastica\Response               addObject($object, Document $doc = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @method  \Elastica\Response               updateDocument($data, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @method  \Elastica\Response               updateDocuments(array $docs, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @method  \Elastica\Bulk\ResponseSet       addObjects(array $objects, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @method  string                           getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @method  Document                         getDocument($id, $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @method  Document                         createDocument($id = '', $data = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @method  \Elastica\Response               setMapping($mapping, array $query = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @method  \Elastica\Response               getMapping() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @method  \Elastica\Response               deleteDocument(Document $document) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @method  \Elastica\Bulk\ResponseSet       deleteDocuments(array $docs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @method  \Elastica\Response               deleteById($id, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @method  \Elastica\Response               deleteIds(array $ids, $routing = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @method  \Elastica\Response               deleteByQuery($query, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class Index extends BaseIndex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var Type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Index constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param Client $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $typeName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |     public function __construct(Client $client, string $name, $typeName = 'record') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |         parent::__construct($client, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |         $this->type = new Type($this, $typeName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Elasticsearch type. To support removal of type in future versions of elastic search, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * we use one one type per index with default name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @see https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return Type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |     public function getType($type = null): Type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |         return $this->type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Check if document exists in the index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @TODO use a HEAD request as we do not need the content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function hasDocument($id, array $options = []): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->type->getDocument($id, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } catch (NotFoundException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Uses _bulk to send documents to the server. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param array|\Elastica\Document[] $docs    Array of Elastica\Document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param array $options Array of query params to use for query. For possible options check es api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return \Elastica\Bulk\ResponseSet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function addDocuments(array $docs, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         foreach ($docs as $doc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $doc->setType($this->type->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return parent::addDocuments($docs, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     public function __call($name, $arguments) | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         if (method_exists($this, $name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             return \call_user_func_array(array($this, $name), $arguments); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         if (method_exists($this->type, $name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             return \call_user_func_array(array($this->type, $name), $arguments); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         throw new \RuntimeException('The called method does not exist.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  |