Log::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php namespace nyx\notify\transports\mail\drivers;
2
3
// Internal dependencies
4
use nyx\notify\transports\mail;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, nyx\notify\transports\mail\drivers\mail.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
6
/**
7
 * Log Mail Driver
8
 *
9
 * @package     Nyx\Notify
10
 * @version     0.1.0
11
 * @author      Michal Chojnacki <[email protected]>
12
 * @copyright   2012-2017 Nyx Dev Team
13
 * @link        https://github.com/unyx/nyx
14
 */
15
class Log implements mail\interfaces\Driver
16
{
17
    /**
18
     * The traits of a Log Mail Driver instance.
19
     */
20
    use traits\CountsRecipients;
21
22
    /**
23
     * @var \Psr\Log\LoggerInterface    The Logger used to log Messages to.
24
     */
25
    protected $logger;
26
27
    /**
28
     * Creates a new Log Mail Driver instance.
29
     *
30
     * @param   \Psr\Log\LoggerInterface    $logger The Logger to log Messages to.
31
     */
32
    public function __construct(\Psr\Log\LoggerInterface $logger)
33
    {
34
        $this->logger = $logger;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function send(\Swift_Mime_Message $message, &$failures = null)
41
    {
42
        $this->logger->debug($this->getMimeEntityString($message));
43
44
        return $this->countRecipients($message);
45
    }
46
47
    /**
48
     * Returns the MIME data of a MIME entity represented as a MIME string. Supports nested entities.
49
     *
50
     * @param   \Swift_Mime_MimeEntity  $entity
51
     * @return  string
52
     */
53
    protected function getMimeEntityString(\Swift_Mime_MimeEntity $entity) : string
54
    {
55
        $result = (string) $entity->getHeaders() . PHP_EOL . $entity->getBody();
56
57
        foreach ($entity->getChildren() as $child) {
58
            $result .= PHP_EOL . PHP_EOL . $this->getMimeEntityString($child);
59
        }
60
61
        return $result;
62
    }
63
}
64