| Total Complexity | 56 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Typo3 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Typo3, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Typo3 implements \Aimeos\MW\Mail\Message\Iface |
||
| 22 | { |
||
| 23 | private $charset; |
||
| 24 | private $object; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Initializes the message instance. |
||
| 29 | * |
||
| 30 | * @param \TYPO3\CMS\Core\Mail\MailMessage $object TYPO3 mail object |
||
| 31 | * @param string $charset Default charset of the message |
||
| 32 | */ |
||
| 33 | public function __construct( \TYPO3\CMS\Core\Mail\MailMessage $object, string $charset ) |
||
| 37 | } |
||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * Adds a source e-mail address of the message. |
||
| 42 | * |
||
| 43 | * @param string $email Source e-mail address |
||
| 44 | * @param string|null $name Name of the user sending the e-mail or null for no name |
||
| 45 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 46 | */ |
||
| 47 | public function from( string $email, string $name = null ) : Iface |
||
| 48 | { |
||
| 49 | if( $email ) |
||
| 50 | { |
||
| 51 | $class = '\Symfony\Component\Mime\Email'; |
||
| 52 | |||
| 53 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 54 | $this->object->addFrom( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
||
| 55 | } else { |
||
| 56 | $this->object->addFrom( $email, $name ); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Adds a destination e-mail address of the target user mailbox. |
||
| 66 | * |
||
| 67 | * @param string $email Destination address of the target mailbox |
||
| 68 | * @param string|null $name Name of the user owning the target mailbox or null for no name |
||
| 69 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 70 | */ |
||
| 71 | public function to( string $email, string $name = null ) : Iface |
||
| 72 | { |
||
| 73 | if( $email ) |
||
| 74 | { |
||
| 75 | $class = '\Symfony\Component\Mime\Email'; |
||
| 76 | |||
| 77 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 78 | $this->object->addTo( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
||
| 79 | } else { |
||
| 80 | $this->object->addTo( $email, $name ); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Adds a destination e-mail address for a copy of the message. |
||
| 90 | * |
||
| 91 | * @param string $email Destination address for a copy |
||
| 92 | * @param string|null $name Name of the user owning the target mailbox or null for no name |
||
| 93 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 94 | */ |
||
| 95 | public function cc( string $email, string $name = null ) : Iface |
||
| 96 | { |
||
| 97 | if( $email ) |
||
| 98 | { |
||
| 99 | $class = '\Symfony\Component\Mime\Email'; |
||
| 100 | |||
| 101 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 102 | $this->object->addCc( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
||
| 103 | } else { |
||
| 104 | $this->object->addCc( $email, $name ); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | return $this; |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Adds a destination e-mail address for a hidden copy of the message. |
||
| 114 | * |
||
| 115 | * @param string $email Destination address for a hidden copy |
||
| 116 | * @param string|null $name Name of the user owning the target mailbox or null for no name |
||
| 117 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 118 | */ |
||
| 119 | public function bcc( string $email, string $name = null ) : Iface |
||
| 120 | { |
||
| 121 | if( $email ) |
||
| 122 | { |
||
| 123 | $class = '\Symfony\Component\Mime\Email'; |
||
| 124 | |||
| 125 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 126 | $this->object->addBcc( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
||
| 127 | } else { |
||
| 128 | $this->object->addBcc( $email, $name ); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Adds the return e-mail address for the message. |
||
| 138 | * |
||
| 139 | * @param string $email E-mail address which should receive all replies |
||
| 140 | * @param string|null $name Name of the user which should receive all replies or null for no name |
||
| 141 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 142 | */ |
||
| 143 | public function replyTo( string $email, string $name = null ) : Iface |
||
| 144 | { |
||
| 145 | if( $email ) |
||
| 146 | { |
||
| 147 | $class = '\Symfony\Component\Mime\Email'; |
||
| 148 | |||
| 149 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 150 | $this->object->addReplyTo( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
||
| 151 | } else { |
||
| 152 | $this->object->addReplyTo( $email, $name ); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Adds a custom header to the message. |
||
| 162 | * |
||
| 163 | * @param string $name Name of the custom e-mail header |
||
| 164 | * @param string $value Text content of the custom e-mail header |
||
| 165 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 166 | */ |
||
| 167 | public function header( string $name, string $value ) : Iface |
||
| 168 | { |
||
| 169 | if( $name ) |
||
| 170 | { |
||
| 171 | $class = '\Symfony\Component\Mime\Email'; |
||
| 172 | |||
| 173 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 174 | $this->object->getHeaders()->add( new \Symfony\Component\Mime\Header\UnstructuredHeader( $name, $value ) ); |
||
| 175 | } else { |
||
| 176 | $this->object->getHeaders()->addTextHeader( $name, $value ); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | return $this; |
||
| 181 | } |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Sends the e-mail message to the mail server. |
||
| 186 | * |
||
| 187 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 188 | */ |
||
| 189 | public function send() : Iface |
||
| 190 | { |
||
| 191 | $this->object->send(); |
||
| 192 | return $this; |
||
| 193 | } |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Sets the e-mail address and name of the sender of the message (higher precedence than "From"). |
||
| 198 | * |
||
| 199 | * @param string $email Source e-mail address |
||
| 200 | * @param string|null $name Name of the user who sent the message or null for no name |
||
| 201 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 202 | */ |
||
| 203 | public function sender( string $email, string $name = null ) : Iface |
||
| 204 | { |
||
| 205 | if( $email ) |
||
| 206 | { |
||
| 207 | $class = '\Symfony\Component\Mime\Email'; |
||
| 208 | |||
| 209 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 210 | $this->object->setSender( new \Symfony\Component\Mime\Address( $email, (string) $name ) ); |
||
|
|
|||
| 211 | } else { |
||
| 212 | $this->object->setSender( $email, $name ); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Sets the subject of the message. |
||
| 222 | * |
||
| 223 | * @param string $subject Subject of the message |
||
| 224 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 225 | */ |
||
| 226 | public function subject( string $subject ) : Iface |
||
| 227 | { |
||
| 228 | if( $subject ) |
||
| 229 | { |
||
| 230 | $class = '\Symfony\Component\Mime\Email'; |
||
| 231 | |||
| 232 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 233 | $this->object->setSubject( $subject ); |
||
| 234 | } else { |
||
| 235 | $this->object->setSubject( $subject ); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Sets the text body of the message. |
||
| 245 | * |
||
| 246 | * @param string $message Text body of the message |
||
| 247 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 248 | */ |
||
| 249 | public function text( string $message ) : Iface |
||
| 250 | { |
||
| 251 | if( $message ) |
||
| 252 | { |
||
| 253 | $class = '\Symfony\Component\Mime\Email'; |
||
| 254 | |||
| 255 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 256 | $this->object->text( $message, $this->charset ); |
||
| 257 | } elseif( class_exists( '\Swift_Mailer' ) ) { |
||
| 258 | $this->object->setBody( $message ); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | return $this; |
||
| 263 | } |
||
| 264 | |||
| 265 | |||
| 266 | /** |
||
| 267 | * Sets the HTML body of the message. |
||
| 268 | * |
||
| 269 | * @param string $message HTML body of the message |
||
| 270 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 271 | */ |
||
| 272 | public function html( string $message ) : Iface |
||
| 273 | { |
||
| 274 | if( $message ) |
||
| 275 | { |
||
| 276 | $class = '\Symfony\Component\Mime\Email'; |
||
| 277 | |||
| 278 | if( class_exists( $class ) && $this->object instanceof $class ) { |
||
| 279 | $this->object->html( $message, $this->charset ); |
||
| 280 | } elseif( class_exists( '\Swift_Mailer' ) ) { |
||
| 281 | $this->object->addPart( $message, 'text/html' ); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Adds an attachment to the message. |
||
| 291 | * |
||
| 292 | * @param string $data Binary or string |
||
| 293 | * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.) |
||
| 294 | * @param string|null $filename Name of the attached file (or null if inline disposition is used) |
||
| 295 | * @param string $disposition Type of the disposition ("attachment" or "inline") |
||
| 296 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
| 297 | */ |
||
| 298 | public function attach( string $data, string $mimetype, string $filename, string $disposition = 'attachment' ) : Iface |
||
| 299 | { |
||
| 300 | if( $data ) |
||
| 301 | { |
||
| 302 | $class = '\Symfony\Component\Mime\Email'; |
||
| 303 | |||
| 304 | if( class_exists( $class ) && $this->object instanceof $class ) |
||
| 305 | { |
||
| 306 | $this->object->attach( $data, $filename, $mimetype ); |
||
| 307 | } |
||
| 308 | elseif( class_exists( '\Swift_Attachment' ) ) |
||
| 309 | { |
||
| 310 | $part = \Swift_Attachment::newInstance( $data, $filename, $mimetype ); |
||
| 311 | $part->setDisposition( $disposition ); |
||
| 312 | $this->object->attach( $part ); |
||
| 313 | } |
||
| 314 | else |
||
| 315 | { |
||
| 316 | throw new \RuntimeException( 'Symfony mailer or Swiftmailer package missing' ); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | return $this; |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Embeds an attachment into the message and returns its reference. |
||
| 326 | * |
||
| 327 | * @param string $data Binary or string |
||
| 328 | * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.) |
||
| 329 | * @param string|null $filename Name of the attached file |
||
| 330 | * @return string Content ID for referencing the attachment in the HTML body |
||
| 331 | */ |
||
| 332 | public function embed( string $data, string $mimetype, string $filename ) : string |
||
| 333 | { |
||
| 334 | $class = '\Symfony\Component\Mime\Email'; |
||
| 335 | |||
| 336 | if( !$filename ) { |
||
| 337 | $filename = md5( $data ); |
||
| 338 | } |
||
| 339 | |||
| 340 | if( class_exists( $class ) && $this->object instanceof $class ) |
||
| 341 | { |
||
| 342 | $this->object->embed( $data, $filename, $mimetype ); |
||
| 343 | return 'cid:' . $filename; |
||
| 344 | } |
||
| 345 | elseif( class_exists( '\Swift_EmbeddedFile' ) ) |
||
| 346 | { |
||
| 347 | $part = \Swift_EmbeddedFile::newInstance( $data, $filename, $mimetype ); |
||
| 348 | return $this->object->embed( $part ); |
||
| 349 | } |
||
| 350 | |||
| 351 | throw new \RuntimeException( 'Symfony mailer or Swiftmailer package missing' ); |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns the internal TYPO3 mail message object. |
||
| 357 | * |
||
| 358 | * @return \TYPO3\CMS\Core\Mail\MailMessage TYPO3 mail message object |
||
| 359 | */ |
||
| 360 | public function object() : \TYPO3\CMS\Core\Mail\MailMessage |
||
| 361 | { |
||
| 362 | return $this->object; |
||
| 363 | } |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Clones the internal objects. |
||
| 368 | */ |
||
| 369 | public function __clone() |
||
| 372 | } |
||
| 373 | } |
||
| 374 |