| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace FMUP\Db\Driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use FMUP\Db\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use FMUP\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Pdo extends PdoConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     const CHARSET_UTF8 = 'utf8'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Charset to use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected function getCharset() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $charset = (string)$this->getSettings('charset'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         return $charset ?: self::CHARSET_UTF8; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param \Pdo $instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected function defaultConfiguration(\Pdo $instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $charset = $this->getCharset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $instance->setAttribute(\PDO::ATTR_TIMEOUT, 10.0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $instance->exec('SET NAMES ' . $charset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $instance->exec('SET CHARACTER SET ' . $charset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Fetch all rows for a given statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param object $statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 42 |  | View Code Duplication |     public function fetchAll($statement) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (!$statement instanceof \PDOStatement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->log(Logger::ERROR, 'Statement not in right format', array('statement' => $statement)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             throw new Exception('Statement not in right format'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return $statement->fetchAll($this->getFetchMode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Begin a transaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function beginTransaction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if ($this->getDriver()->inTransaction()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $this->log(Logger::CRITICAL, 'Transaction already opened', $this->getSettings()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 throw new Exception('Transaction already opened'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $this->log(Logger::DEBUG, 'Transaction start'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return $this->getDriver()->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('settings' => $this->getSettings())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 81 |  | View Code Duplication |     public function rollback() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->log(Logger::DEBUG, 'Transaction rollback'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $this->getDriver()->rollBack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function errorCode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             return $this->getDriver()->errorCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function errorInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return $this->getDriver()->errorInfo(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 124 |  | View Code Duplication |     public function commit() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $this->log(Logger::DEBUG, 'Transaction commit'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             return $this->getDriver()->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * Execute a statement for given values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param object $statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param array $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     public function execute($statement, $values = array()) | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         if (!$statement instanceof \PDOStatement) { | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             $this->log(Logger::ERROR, 'Statement not in right format', array('statement' => $statement)); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |             throw new Exception('Statement not in right format'); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |             $this->log(Logger::DEBUG, 'Executing query', array('values' => $values)); | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |             return $statement->execute($values); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         } catch (\PDOException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('exception' => $e, 'values' => $values)); | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * Prepare a SQL string to a statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @param string $sql | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @return \PDOStatement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public function prepare($sql, array $options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             $this->log(Logger::DEBUG, 'Preparing query', array('sql' => $sql, 'options' => $options)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             return $this->getDriver()->prepare($sql, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('exception' => $e, 'sql' => $sql)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * Retrieve id inserted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param string $name optional | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 182 |  | View Code Duplication |     public function lastInsertId($name = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             return $this->getDriver()->lastInsertId($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * Retrieve number of affected rows of a statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @param mixed $statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 198 |  | View Code Duplication |     public function count($statement) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         if (!$statement instanceof \PDOStatement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             $this->log(Logger::ERROR, 'Statement not in right format', array('statement' => $statement)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             throw new Exception('Statement not in right format'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |             return $statement->rowCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |             $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             throw new Exception($e->getMessage(), (int)$e->getCode(), $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 212 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 213 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.