Notifier::createTableLine()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 27
ccs 0
cts 25
cp 0
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Jean Silva <[email protected]>
4
 * @license MIT
5
 */
6
namespace Jeancsil\FlightSpy\Notifier\Email;
7
8
use Jeancsil\FlightSpy\Api\DataTransfer\SessionParameters;
9
use Jeancsil\FlightSpy\Service\Currency\PriceFormatter;
10
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchWriterTrait;
11
use Jeancsil\FlightSpy\Service\ElasticSearch\ElasticSearchRequester;
12
use Jeancsil\FlightSpy\Notifier\Deal;
13
use Jeancsil\FlightSpy\Notifier\NotifiableInterface;
14
use Postmark\PostmarkClient;
15
16
class Notifier implements NotifiableInterface
17
{
18
    use ElasticSearchWriterTrait;
19
20
    /**
21
     * @var ElasticSearchRequester
22
     */
23
    private $elasticSearchRequester;
24
    /**
25
     * @var PriceFormatter
26
     */
27
    private $priceFormatter;
28
    /**
29
     * @var PostmarkClient
30
     */
31
    private $mailer;
32
    private $html;
33
    private $from;
34
    private $to;
35
    private $subject;
36
37
    private $tableLines = [];
38
39
    public function __construct(PostmarkClient $mailer, $html, $from, $to, $subject)
40
    {
41
        $this->mailer = $mailer;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
42
        $this->from = $from;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
43
        $this->to = $to;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
44
        $this->subject = $subject;
45
46
        $this->initializeHtmlTemplate($html);
47
    }
48
49
    /** @inheritdoc */
50
    public function notify(array $deals, SessionParameters $sessionParameters)
51
    {
52
        $notifications = $this->createNotifications($sessionParameters, $deals);
53
54
        if (empty($this->tableLines)) {
55
            return;
56
        }
57
58
        /**
59
         * @var string $identifier
60
         * @var EmailNotification $notification
61
         */
62
        foreach ($notifications as $identifier => $notification) {
63
            $this->elasticSearchWriter
64
                ->writeOne([
65
                    'identifier' => $identifier,
66
                    'notified' => $this->to
67
                ]);
68
        }
69
70
        $this->mailer->sendEmail(
71
            $this->from,
72
            $this->to,
73
            $this->subject,
74
            str_replace('<!--NewLine-->', implode('', $this->tableLines), $this->html)
75
        );
76
    }
77
78
    /** @inheritdoc */
79
    public function wasNotified(Deal $deal, $notifyTo)
80
    {
81
        return $this->elasticSearchRequester
82
            ->wasNotified(
83
                $deal->getIdentifier(),
84
                $notifyTo
85
            );
86
    }
87
88
89
    /** @inheritdoc */
90
    public function createNotifications(SessionParameters $parameters, array $deals = [])
91
    {
92
        $notifications = [];
93
        /** @var Deal $deal */
94
        foreach ($deals as $deal) {
95
            if ($this->wasNotified($deal, $this->to)) {
96
                continue;
97
            }
98
99
            $this->tableLines[] = $this->createTableLine(
100
                $deal->getAgentName(),
101
                $this->priceFormatter->format($deal->getPrice(), $parameters->currency),
102
                $deal->getDeepLinkUrl()
103
            );
104
105
            $notifications[$deal->getIdentifier()] = new EmailNotification();
106
        }
107
108
        return $notifications;
109
    }
110
111
    /**
112
     * @param ElasticSearchRequester $elasticSearchRequester
113
     */
114
    public function setElasticSearchRequester(ElasticSearchRequester $elasticSearchRequester)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $elasticSearchRequester exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
115
    {
116
        $this->elasticSearchRequester = $elasticSearchRequester;
117
    }
118
119
    /**
120
     * @param PriceFormatter $priceFormatter
121
     */
122
    public function setPriceFormatter(PriceFormatter $priceFormatter)
123
    {
124
        $this->priceFormatter = $priceFormatter;
125
    }
126
127
    /**
128
     * @param $htmlTemplate
129
     */
130
    private function initializeHtmlTemplate($htmlTemplate)
131
    {
132
        $this->html = file_get_contents($htmlTemplate);
133
    }
134
135
    /**
136
     * @param $agentName
137
     * @param $price
138
     * @param $deepLink
139
     * @return string
140
     */
141
    private function createTableLine($agentName, $price, $deepLink)
142
    {
143
        $deepLinkHtml = '';
144
145
        if ($deepLink) {
146
            $deepLinkHtml = '<a href="' . $deepLink . '" 
147
               target="_blank" 
148
               style="text-decoration:underline;background-color:#ffffff;
149
                border:solid 1px #3498db;border-radius:5px;
150
                box-sizing:border-box;color:#3498db;
151
                cursor:pointer;font-size:12px;
152
                margin:0;padding:3px 3px;
153
                text-decoration:none;text-transform:capitalize;
154
                background-color:#3498db;border-color:#3498db;color:#ffffff;">Book</a>';
155
        }
156
157
        return sprintf(
158
            '<tr>
159
                <td style="font-family:sans-serif;font-size:14px;vertical-align:top;padding-bottom:15px;">%s</td>
160
                <td style="font-family:sans-serif;font-size:14px;vertical-align:top;padding-bottom:15px;">%s</td>
161
                <td style="font-family:sans-serif;font-size:14px;vertical-align:top;padding-bottom:15px;">%s</td>
162
            </tr>',
163
            $agentName,
164
            $price,
165
            $deepLinkHtml
166
        );
167
    }
168
}
169