| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Pageon\SlackWebhookMonolog\Monolog; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Monolog\Handler\AbstractProcessingHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Monolog\Handler\MissingExtensionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ConfigInterface as MonologConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Pageon\SlackWebhookMonolog\Slack\Interfaces\ConfigInterface as SlackConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Monolog\Handler\Curl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Pageon\SlackWebhookMonolog\Slack\Payload; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author Jelmer Prins <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class SlackWebhookHandler extends AbstractProcessingHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var SlackConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $slackConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var MonologConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $monologConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var Curl\Util | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $curlUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * SlackWebhookHandler constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param SlackConfig $slackConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param MonologConfig $monologConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @throws MissingExtensionException When the curl extension is not activated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |     public function __construct(SlackConfig $slackConfig, MonologConfig $monologConfig, Curl\Util $curlUtil) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |         parent::__construct($monologConfig->getLevel(), $monologConfig->doesBubble()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         $this->slackConfig = $slackConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $this->monologConfig = $monologConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         $this->curlUtil = $curlUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |     public function write(array $record) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         $curlUtil = $this->curlUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         $curlSession = curl_init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         curl_setopt($curlSession, CURLOPT_URL, $this->slackConfig->getWebhook()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         curl_setopt($curlSession, CURLOPT_POST, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         curl_setopt($curlSession, CURLOPT_POSTFIELDS, $this->getPayload($record)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         // we return this because our mock will return the curl session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         return $curlUtil::execute($curlSession); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Prepares content data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param array $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 75 | 1 |  |     private function getPayload($record) | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         return [ | 
            
                                                        
            
                                    
            
            
                | 78 | 1 |  |             'payload' => new Payload($record, $this->slackConfig), | 
            
                                                        
            
                                    
            
            
                | 79 | 1 |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |