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.
Completed
Push — master ( 2d5952...15b212 )
by Tyler
02:03
created

MailgunHandler::buildHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Tylercd100\Monolog\Handler;
4
5
use Exception;
6
use Monolog\Logger;
7
use Monolog\Handler\SocketHandler;
8
9
/**
10
 * Mailgun - Monolog Handler
11
 */
12
class MailgunHandler extends SocketHandler
13
{
14
    private $host;
15
    private $version;
16
    private $domain;
17
    private $token;
18
    private $to;
19
    private $subject;
20
    private $from;
21
22 15
    public function __construct($to, $subject, $from, $token, $domain, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.mailgun.net', $version = 'v3')
23
    {
24 15
        if($version !== 'v3'){
25 3
            throw new Exception("Version '{$version}' is not supported");
26
        }
27
28 12
        $connectionString = $useSSL ? 'ssl://'.$host.':443' : $host.':80';
29 12
        parent::__construct($connectionString, $level, $bubble);
30
31 12
        $this->to = $to;
32 12
        $this->subject = $subject;
33 12
        $this->from = $from;
34 12
        $this->host = $host;
35 12
        $this->version = $version;
36 12
        $this->domain = $domain;
37 12
        $this->token = $token;
38 12
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @param  array  $record
44
     * @return string
45
     */
46 9
    protected function generateDataStream($record)
47
    {
48 9
        $content = $this->buildContent($record);
49 9
        return $this->buildHeader($content) . $content;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @param  array  $record
56
     * @return string
57
     */
58 9
    protected function buildContent($record)
59
    {
60
        $dataArray = array(
61 9
            'from'    => $this->from,
62 9
            'to'      => $this->to,
63 9
            'subject' => $this->subject,
64 9
            'text'    => $record['formatted']
65 9
        );
66
67 9
        return http_build_query($dataArray);
68
    }
69
70
    /**
71
     * Builds the URL for the API call
72
     * 
73
     * @return string
74
     */
75 9
    protected function buildRequestUrl()
76
    {
77 9
        return "POST /{$this->version}/{$this->domain}/messages HTTP/1.1\r\n";
78
    }
79
80
    /**
81
     * Builds the header of the API call
82
     *
83
     * @param  string $content
84
     * @return string
85
     */
86 9
    private function buildHeader($content)
87
    {
88 9
        $auth = base64_encode("api:".$this->token);
89
90 9
        $header = $this->buildRequestUrl();
91
92 9
        $header .= "Host: {$this->host}\r\n";
93 9
        $header .= "Authorization: Basic ".$auth."\r\n";
94 9
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
95 9
        $header .= "Cache-Control: no-cache\r\n";
96 9
        $header .= "Content-Length: " . strlen($content) . "\r\n";
97 9
        $header .= "\r\n";
98
99 9
        return $header;
100
    }
101
}
102