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.

Email::extractRecipients()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php namespace RuleCom\Notifier\Channels;
2
3
use GuzzleHttp\Client;
4
use Monolog\Handler\StreamHandler;
5
use Monolog\Logger;
6
7
class Email implements Channel
8
{
9
    /**
10
     * @var Client
11
     */
12
    private $guzzle;
13
14
    /**
15
     * @var string
16
     */
17
    private $apiKey;
18
19
    /**
20
     * @var string
21
     */
22
    private $subject = '';
23
24
    /**
25
     * @var array
26
     */
27
    private $from = ['name' => '', 'email' => ''];
28
29
    /**
30
     * @var array
31
     */
32
    private $to = ['name' => '', 'email' => ''];
33
34
    /**
35
     * @var array
36
     */
37
    private $content = ['html' => '', 'plain' => ''];
38
39
    /**
40
     * @var bool
41
     */
42
    private $debug = false;
43
44
    /**
45
     * @var string
46
     */
47
    private $logPath;
48
49
    /**
50
     * @var Logger
51
     */
52
    private $logger;
53
54
    public function __construct(Client $guzzle, Logger $logger = null)
55
    {
56
        $this->guzzle = $guzzle;
57
        $this->logger = $logger;
58
    }
59
60
    /**
61
     * @param string $apiKey
62
     * @return $this
63
     */
64
    public function apiKey($apiKey)
65
    {
66
        $this->apiKey = $apiKey;
67
        return $this;
68
    }
69
70
    /**
71
     * @param string $subject
72
     * @return $this
73
     */
74
    public function subject($subject)
75
    {
76
        $this->subject = $subject;
77
        return $this;
78
    }
79
80
    /**
81
     * @param array $from
82
     * @return $this
83
     */
84
    public function from(array $from)
85
    {
86
        $this->from = $from;
87
        return $this;
88
    }
89
90
    /**
91
     * @param array $to
92
     * @return $this
93
     */
94
    public function to(array $to)
95
    {
96
        $this->to = $to;
97
        return $this;
98
    }
99
100
    /**
101
     * @param array $content
102
     * @return $this
103
     */
104
    public function content($content)
105
    {
106
        $this->content = $content;
107
        return $this;
108
    }
109
110
    public function debug($logPath)
111
    {
112
        $this->debug = true;
113
        $this->logPath = $logPath;
114
        return $this;
115
    }
116
    /**
117
     * Dispatches notification message to Rule
118
     */
119
    public function dispatch()
120
    {
121
        if ($this->debug) {
122
            return $this->fakeDispatch();
123
        }
124
125
        foreach ($this->extractRecipients() as $recipient) {
126
            $this->guzzle->post('https://app.rule.io/api/v2/transactionals', [
127
                'json' => [
128
                    'apikey' => $this->apiKey,
129
                    'transaction_type' => 'email',
130
                    'transaction_name' => $this->subject,
131
                    'subject' => $this->subject,
132
                    'from' => $this->from,
133
                    'to' => $recipient,
134
                    'content' => [
135
                        'html' => base64_encode($this->content['html']),
136
                        'plain' => base64_encode($this->content['plain'])
137
                    ]
138
                ]
139
            ]);
140
        }
141
    }
142
143
    /**
144
     * @return array
145
     */
146
    private function extractRecipients()
147
    {
148
        if (is_array(reset($this->to))) {
149
            return $this->to;
150
        }
151
152
        return [$this->to];
153
    }
154
155
    /**
156
     * Fakes dispatch by logging instead
157
     */
158
    private function fakeDispatch()
159
    {
160
        $this->logger->pushHandler(new StreamHandler($this->logPath));
161
        $this->logger->addInfo('Email:', [
162
            'from' => $this->from,
163
            'to' => $this->extractRecipients(),
164
            'subject' => $this->subject,
165
            'content' => $this->content
166
        ]);
167
    }
168
}
169