GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MailmanFilesystemLogger   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 144
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A log() 0 14 1
A getMessageInfo() 0 15 1
A getMessageHTMLContent() 0 6 1
A getMessageEMLContent() 0 4 1
A getMessageLogFilePath() 0 7 1
A getMessageLogDirectoryPath() 0 6 1
A prepareStorage() 0 13 3
1
<?php
2
3
namespace Qodeboy\Mailman\Logger;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Qodeboy\Mailman\Contracts\MailmanSwiftMessageAdapter;
7
8
/**
9
 * Class MailmanFilesystemLogger.
10
 */
11
class MailmanFilesystemLogger extends AbstractMailmanLogger
12
{
13
    /**
14
     * Base path where email logs are stored.
15
     *
16
     * @var string
17
     */
18
    protected $storagePath;
19
20
    /**
21
     * FileSystem adapter.
22
     *
23
     * @var Filesystem
24
     */
25
    protected $fileSystem;
26
27
    /**
28
     * MailmanFilesystemLogger constructor.
29
     */
30
    public function __construct()
31
    {
32
        $this->storagePath = config('mailman.storage.filesystem.path');
33
        $this->fileSystem = app(Filesystem::class);
34
    }
35
36
    /**
37
     * Log given Swift_Mime_Message email message.
38
     *
39
     * @param MailmanSwiftMessageAdapter $message
40
     *
41
     * @return mixed
42
     */
43
    public function log(MailmanSwiftMessageAdapter $message)
44
    {
45
        $this->prepareStorage($message);
46
47
        $this->fileSystem->put(
48
            $this->getMessageLogFilePath($message).'.html',
49
            $this->getMessageHTMLContent($message)
50
        );
51
52
        $this->fileSystem->put(
53
            $this->getMessageLogFilePath($message).'.eml',
54
            $this->getMessageEMLContent($message)
55
        );
56
    }
57
58
    /**
59
     * Generate a human readable HTML comment with message info.
60
     *
61
     * @param MailmanSwiftMessageAdapter $message
62
     *
63
     * @return string
64
     */
65
    protected function getMessageInfo(MailmanSwiftMessageAdapter $message)
66
    {
67
        return sprintf(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return sprintf('<!-- Sta...message->getSubject()); (string) is incompatible with the return type declared by the abstract method Qodeboy\Mailman\Logger\A...nLogger::getMessageInfo of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
            "<!--\nStatus: %s, \nMessageID: %s, \nContent-Type: %s, \nFrom:%s, \nTo:%s, \nReply-To:%s, \nCC:%s, \nBCC:%s, \nSubject:%s\n-->\n",
69
            $message->getStatus(),
70
            $message->getId(),
71
            $message->getContentType(),
72
            json_encode($message->getFrom()),
73
            json_encode($message->getTo()),
74
            json_encode($message->getReplyTo()),
75
            json_encode($message->getCc()),
76
            json_encode($message->getBcc()),
77
            $message->getSubject()
78
        );
79
    }
80
81
    /**
82
     * Get the HTML content for the log file.
83
     *
84
     * @param  MailmanSwiftMessageAdapter $message
85
     *
86
     * @return string
87
     */
88
    protected function getMessageHTMLContent(MailmanSwiftMessageAdapter $message)
89
    {
90
        $messageInfo = $this->getMessageInfo($message);
91
92
        return $messageInfo.$message->getBody();
93
    }
94
95
    /**
96
     * Get the EML content for the log file.
97
     *
98
     * @param  MailmanSwiftMessageAdapter $message
99
     *
100
     * @return string
101
     */
102
    protected function getMessageEMLContent(MailmanSwiftMessageAdapter $message)
103
    {
104
        return $message->toString();
105
    }
106
107
    /**
108
     * Get the path to the email log file.
109
     *
110
     * @param  MailmanSwiftMessageAdapter $message
111
     *
112
     * @return string
113
     */
114
    protected function getMessageLogFilePath(MailmanSwiftMessageAdapter $message)
115
    {
116
        $messageLogDirectory = $this->getMessageLogDirectoryPath($message);
117
        $messageLogFileName = str_replace(['@', '.'], ['_at_', '_'], $message->getId());
118
119
        return $messageLogDirectory.'/'.str_slug($messageLogFileName);
120
    }
121
122
    /**
123
     * Get the path to the email log directory.
124
     *
125
     * @param MailmanSwiftMessageAdapter $message
126
     *
127
     * @return string
128
     */
129
    protected function getMessageLogDirectoryPath(MailmanSwiftMessageAdapter $message)
130
    {
131
        list($messageId) = explode('@', $message->getId());
132
133
        return storage_path($this->storagePath.'/'.$messageId);
134
    }
135
136
    /**
137
     * Create required directories for logging.
138
     *
139
     * @param MailmanSwiftMessageAdapter $message
140
     */
141
    protected function prepareStorage(MailmanSwiftMessageAdapter $message)
142
    {
143
        $messageLogStorageDirectory = storage_path($this->storagePath);
144
        if (! $this->fileSystem->exists($messageLogStorageDirectory)) {
145
            $this->fileSystem->makeDirectory($messageLogStorageDirectory);
146
            $this->fileSystem->put($messageLogStorageDirectory.'/.gitignore', "*\n!.gitignore");
147
        }
148
149
        $messageLogDirectory = $this->getMessageLogDirectoryPath($message);
150
        if (! $this->fileSystem->exists($messageLogDirectory)) {
151
            $this->fileSystem->makeDirectory($messageLogDirectory);
152
        }
153
    }
154
}
155