| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Pageon\SlackWebhookMonolog\Slack\Attachment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Monolog\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * An attachment containing basic info about the error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Jelmer Prins <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @since 0.4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class BasicInfoAttachment extends Attachment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array The data from the error handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Extra error data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var ErrorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * BasicInfoAttachment constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param array $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param ErrorInterface $error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 10 |  |     public function __construct(array $record, ErrorInterface $error = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 10 |  |         $this->record = $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 10 |  |         $this->error = $error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 10 |  |         $message = ($this->error !== null) ? $this->error->getMessage() : $this->record['message']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 10 |  |         parent::__construct($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 10 |  |         $this->setColour($this->getColourForLoggerLevel()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 10 |  |         $this->setText(sprintf('*Error:* %s', $this->record['level_name'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 10 |  |         $this->addField(new Field('What', $message)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 10 |  |         $this->addField(new Field('When', $this->record['datetime']->format('d/m/Y H:m:i'), true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 49 | 10 | View Code Duplication |         if (!empty($this->record['context'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             $this->addField(new Field('Context', json_encode($this->record['context']))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 | 10 | View Code Duplication |         if (!empty($this->record['extra'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |             $this->addField(new Field('Extra', json_encode($this->record['extra']))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 10 |  |         if ($this->error !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |             $this->addField(new Field('Line', $this->error->getLine(), true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |             $this->addField(new Field('File', $this->error->getFile())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Returned a Slack message attachment color associated with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * provided level. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return Colour | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 69 | 10 |  |     private function getColourForLoggerLevel() | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 71 | 10 |  |         switch (true) { | 
            
                                                        
            
                                    
            
            
                | 72 | 10 |  |             case $this->record['level'] >= Logger::ERROR: | 
            
                                                        
            
                                    
            
            
                | 73 | 10 |  |                 return new Colour('danger'); | 
            
                                                        
            
                                    
            
            
                | 74 | 1 |  |             case $this->record['level'] >= Logger::WARNING: | 
            
                                                        
            
                                    
            
            
                | 75 | 1 |  |                 return new Colour('warning'); | 
            
                                                        
            
                                    
            
            
                | 76 | 1 |  |             case $this->record['level'] >= Logger::INFO: | 
            
                                                        
            
                                    
            
            
                | 77 | 1 |  |                 return new Colour('good'); | 
            
                                                        
            
                                    
            
            
                | 78 | 1 |  |             default: | 
            
                                                        
            
                                    
            
            
                | 79 | 1 |  |                 return new Colour('#e3e4e6'); | 
            
                                                        
            
                                    
            
            
                | 80 | 1 |  |         } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  | 
            
                        
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.