Passed
Push — master ( 864509...8257bd )
by Gabriel
14:12
created

AbstractBuilder::hasItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ByTIC\Notifications\Messages\Builder;
4
5
use Nip\MailModule\Models\EmailsTable\EmailsTrait;
0 ignored issues
show
Bug introduced by
The type Nip\MailModule\Models\EmailsTable\EmailsTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Nip\MailModule\Models\EmailsTable\EmailTrait;
0 ignored issues
show
Bug introduced by
The type Nip\MailModule\Models\EmailsTable\EmailTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Nip\Records\AbstractModels\Record;
8
9
/**
10
 * Class AbstractBuilder
11
 * @package ByTIC\Common\Records\Emails\Builder
12
 */
13
abstract class AbstractBuilder
14
{
15
    protected $item = null;
16
17
    /**
18
     * @var EmailTrait
19
     */
20
    protected $email = null;
21
22
    protected $mergeTags = [];
23
24
25
    /**
26
     * @return bool
27
     */
28
    public function save()
29
    {
30
        if ($this->isActive()) {
31
            $this->createEmail();
32
33
            return true;
34
        }
35
        return false;
36
    }
37
38
    /**
39
     * @return bool
40
     */
41
    public function isActive()
42
    {
43
        return true;
44
    }
45
46
    public function generateEmail()
47
    {
48
        $this->compile();
49
        $this->compileSubject();
50
        $this->compileEmailBody();
51
    }
52
53
    /**
54
     * @return EmailTrait
55
     */
56
    public function createEmail()
57
    {
58
        $this->generateEmail();
59
        $this->saveEmail();
60
        $this->compileAttachments();
61
62
        return $this->getEmail();
63
    }
64
65
    /**
66
     * @return $this
67
     */
68
    protected function compile()
69
    {
70
        return $this;
71
    }
72
73
    protected function compileSubject()
74
    {
75
        $this->getEmail()->subject = $this->generateEmailSubject();
76
    }
77
78
    /**
79
     * @return EmailTrait
80
     */
81
    public function getEmail()
82
    {
83
        if ($this->email === null) {
84
            $this->initEmail();
85
        }
86
87
        return $this->email;
88
    }
89
90
    /**
91
     * @param EmailTrait $email
92
     */
93
    public function setEmail($email)
94
    {
95
        $this->email = $email;
96
    }
97
98
    protected function initEmail()
99
    {
100
        $this->setEmail($this->generateNewEmail());
101
    }
102
103
    /**
104
     * @return EmailTrait
105
     */
106
    protected function generateNewEmail()
107
    {
108
        $email = $this->newBlankEmail();
109
        return $this->hydrateEmail($email);
110
    }
111
112
    /**
113
     * @return EmailTrait
114
     */
115
    protected function newBlankEmail()
116
    {
117
        return $this->getEmailsManager()->getNew();
118
    }
119
120
    /**
121
     * @return EmailsTrait
122
     */
123
    abstract protected function getEmailsManager();
124
125
    /**
126
     * @param EmailTrait $email
127
     * @return mixed
128
     */
129
    protected function hydrateEmail($email)
130
    {
131
        $email->IsHTML('yes');
132
        $email->populateFromConfig();
133
        if ($this->hasItem()) {
134
            $email->populateFromItem($this->getItem());
135
        }
136
        return $email;
137
    }
138
139
    /**
140
     * @return bool
141
     */
142
    public function hasItem()
143
    {
144
        return $this->item instanceof Record;
145
    }
146
147
    /**
148
     * @return Record
149
     */
150
    public function getItem()
151
    {
152
        return $this->item;
153
    }
154
155
    /**
156
     * @param Record|BuilderAwareTrait $item
157
     * @return $this
158
     */
159
    public function setItem($item)
160
    {
161
        $this->item = $item;
162
        return $this;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    abstract protected function generateEmailSubject();
169
170
    protected function compileEmailBody()
171
    {
172
        $this->getEmail()->body = $this->generateEmailBody();
173
174
//        ob_end_clean();
175
//        echo $this->getEmail()->body;
176
//        die();
177
    }
178
179
    /**
180
     * @return null|string
181
     */
182
    abstract protected function generateEmailBody();
183
184
    public function saveEmail()
185
    {
186
        $this->getEmail()
187
            ->setVars($this->getMergeTags())
188
            ->saveRecord();
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    public function getMergeTags()
195
    {
196
        return $this->mergeTags;
197
    }
198
199
    public function compileAttachments()
200
    {
201
    }
202
203
    /**
204
     * @param $name
205
     * @param $value
206
     * @return AbstractBuilder
207
     */
208
    public function setMergeTag($name, $value)
209
    {
210
        $this->mergeTags[$name] = $value;
211
212
        return $this;
213
    }
214
215
    /**
216
     * @param $name
217
     * @return mixed
218
     */
219
    public function getMergeTag($name)
220
    {
221
        return $this->mergeTags[$name];
222
    }
223
}
224