| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace BEdita\ElasticSearch\Model\Index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Cake\Datasource\EntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cake\ElasticSearch\Datasource\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cake\ORM\Exception\PersistenceFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Elastica\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Elastica\Exception\NotFoundException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | trait IndexTrait | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * Returns the ElasticSearch connection instance for this index. | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @return \Cake\ElasticSearch\Datasource\Connection | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     abstract public function getConnection(): Connection; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * Retrieve a document from the index. | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @param string $primaryKey Document ID. | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @param array $options Array of options. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @return \Cake\Datasource\EntityInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     abstract public function get(string $primaryKey, array $options = []): EntityInterface; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * Persist a document to the index. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param \Cake\Datasource\EntityInterface $entity Document. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param array $options Array of options. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return \Cake\Datasource\EntityInterface|false | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     abstract public function save(EntityInterface $entity, array $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * Delete a document from the index. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param \Cake\Datasource\EntityInterface $entity Document. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param array $options Array of options. | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     abstract public function delete(EntityInterface $entity, array $options): bool; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * Retrieve a document from the index if exists, or `null` on failure. | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @see \Cake\ElasticSearch\Index::get() | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $primaryKey Document ID. | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @param array $options Array of options. | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @return \Cake\Datasource\EntityInterface|null | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     public function getIfExists(string $primaryKey, array $options = []): EntityInterface|null | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             return $this->get($primaryKey, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         } catch (NotFoundException) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * Persist a document to the index, or throw an exception upon failure. | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param \Cake\Datasource\EntityInterface $entity Document. | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @param array $options | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @return \Cake\Datasource\EntityInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function saveOrFail(EntityInterface $entity, array $options = []): EntityInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         if ($this->save($entity, $options) === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             throw new PersistenceFailedException($entity, ['save']); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         return $entity; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * Delete a document from the index, or throw an exception upon failure. | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @param \Cake\Datasource\EntityInterface $entity Document. | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @param array $options Array of options. | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function deleteOrFail(EntityInterface $entity, array $options): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if ($this->delete($entity, $options) === false) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |             throw new PersistenceFailedException($entity, ['delete']); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * Update a single field on an existing document. | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @param string $primaryKey Document ID. | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @param string $field Name of field to update. | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @param mixed $value New value. | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @return bool Success. | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function set(string $primaryKey, string $field, mixed $value): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $esIndex = $this->getConnection()->getIndex($this->getName()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $document = new Document($primaryKey, [$field => $value]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $response = $esIndex->updateDocument($document); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |         return $response->isOk(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |