| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright  Copyright (c) Flipbox Digital Limited & Benoît Rouleau | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license    https://github.com/benface/craft-postmark/blob/master/LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link       https://github.com/benface/craft-postmark | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace flipbox\postmark; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Craft; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use craft\mail\transportadapters\BaseTransportAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Postmark\Transport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author Flipbox Factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Adapter extends BaseTransportAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public static function displayName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return 'Postmark'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function attributeLabels() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'token' => Craft::t('postmark', 'Token') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function rules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $rules = parent::rules(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if (!$this->tokenOverride()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $rules = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $rules, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                             'token' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                         'required' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     public function attributes() | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         return array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             parent::attributes(), | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 'token' | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string|null $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function setToken(string $token = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->token = $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function getToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $this->token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * Identify whether the token can be set (or if it has been set via config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     protected function tokenOverride(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return !empty(Postmark::getInstance()->getSettings()->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @throws \Twig_Error_Loader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @throws \yii\base\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function getSettingsHtml() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return Craft::$app->getView()->renderTemplate('postmark/settings', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             'adapter' => $this, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             'settings' => Postmark::getInstance()->getSettings() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function defineTransport() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'class' => Transport::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             'constructArgs' => [Postmark::getInstance()->getSettings()->token ?: Craft::parseEnv($this->token)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  |