Completed
Push — develop ( 2c7f98...cb8417 )
by Carsten
14s
created

Mail::getTemplate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
namespace Core\Controller\Plugin;
4
5
use Interop\Container\ContainerInterface;
6
use Zend\EventManager\EventManagerInterface;
7
use Zend\Log\LoggerInterface;
8
use Zend\Mail\Transport\TransportInterface;
9
use Zend\ModuleManager\ModuleManagerInterface;
10
use Zend\Mvc\Controller\Plugin\PluginInterface;
11
use Zend\Stdlib\DispatchableInterface as Dispatchable;
12
use Zend\Mail\Message;
13
use Zend\Mail\Transport\Sendmail;
14
use Zend\Mail\AddressList;
15
use Zend\EventManager\Event;
16
use Zend\Stdlib\Parameters;
17
use Zend\Stdlib\ArrayUtils;
18
use Zend\View\Resolver\ResolverInterface;
19
20
class Mail extends Message implements PluginInterface
21
{
22
    /**
23
     * @var Dispatchable
24
     */
25
    protected $controller;
26
    
27
    /**
28
     * @var array
29
     */
30
    protected $param = array();
31
    
32
    /**
33
     * @var array
34
     */
35
    protected $config = array();
36
37
    /**
38
     * @var LoggerInterface
39
     */
40
    protected $mailLogger;
41
42
    /**
43
     * @var ResolverInterface
44
     */
45
    protected $viewResolver;
46
47
    /**
48
     * @var EventManagerInterface
49
     */
50
    protected $eventManager;
51
52
    /**
53
     * @var ModuleManagerInterface
54
     */
55
    protected $moduleManager;
56
57
    /**
58
     * @var TransportInterface
59
     */
60
    protected $transport;
61
62
    /**
63
     * Mail constructor.
64
     * @param LoggerInterface $mailLogger
65
     * @param ResolverInterface $viewResolver
66
     * @param EventManagerInterface $eventManager
67
     * @param ModuleManagerInterface $moduleManager
68
     */
69
    public function __construct(
70
        LoggerInterface $mailLogger,
71
        ResolverInterface $viewResolver,
72
        EventManagerInterface $eventManager,
73
        ModuleManagerInterface $moduleManager
74
    )
75
    {
76
        $this->mailLogger       = $mailLogger;
77
        $this->viewResolver     = $viewResolver;
78
        $this->eventManager     = $eventManager;
79
        $this->moduleManager    = $moduleManager;
80
        $this->transport        = new Sendmail();
81
    }
82
    
83
    /**
84
     * Set the current controller instance
85
     *
86
     * @param  Dispatchable $controller
87
     * @return void
88
     */
89
    public function setController(Dispatchable $controller)
90
    {
91
        $this->controller = $controller;
92
    }
93
94
    /**
95
     * Get the current controller instance
96
     *
97
     * @return null|Dispatchable
98
     */
99
    public function getController()
100
    {
101
        return $this->controller;
102
    }
103
104
    /**
105
     * Set mail transport to be use
106
     *
107
     * @param TransportInterface $transport
108
     * @return $this
109
     */
110
    public function setTransport(TransportInterface $transport)
111
    {
112
        $this->transport = $transport;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return TransportInterface
119
     */
120
    public function getTransport()
121
    {
122
        return $this->transport;
123
    }
124
125
    public function __invoke(array $param = array())
126
    {
127
        $this->param = $param;
128
        $this->config = array();
129
        return $this;
130
    }
131
    
132
    protected function stringFromMailHeader($header)
133
    {
134
        $erg = '';
135
        if ($header instanceof AddressList) {
136
            $A = ArrayUtils::iteratorToArray($header);
0 ignored issues
show
Coding Style introduced by
$A does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
137
            $AA = array();
0 ignored issues
show
Coding Style introduced by
$AA does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
138
            while (!empty($A)) {
0 ignored issues
show
Coding Style introduced by
$A does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
139
                $AA[] = array_shift($A)->toString();
0 ignored issues
show
Coding Style introduced by
$AA does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
140
            }
141
            $erg = implode(',', $AA);
0 ignored issues
show
Coding Style introduced by
$AA does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
142
        }
143
        return $erg;
144
    }
145
    
146
    public function __toString()
147
    {
148
        $template = $this->getTemplate();
149
        $to = $this->stringFromMailHeader($this->getTo());
150
        $cc = $this->stringFromMailHeader($this->getCc());
151
        $bcc = $this->stringFromMailHeader($this->getBcc());
152
        $from = $this->stringFromMailHeader($this->getFrom());
153
        $replyTo = $this->stringFromMailHeader($this->getReplyTo());
154
        
155
        return str_pad($template, 30)
156
                . 'to: ' . str_pad($to, 50)
157
                . 'cc: ' . str_pad($cc, 50)
158
                . 'bcc: ' . str_pad($bcc, 50)
159
                . 'from: ' . str_pad($from, 50)
160
                . 'replyTo: ' . str_pad($replyTo, 50)
161
                //. str_pad(implode(',', ArrayUtils::iteratorToArray($this->getSender())),50)
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
162
                . 'subject: ' . str_pad($this->getSubject(), 50);
163
    }
164
    
165
    public function template($template)
166
    {
167
        $controller =  get_class($this->controller);
168
        
169
        $event = new Event();
170
        $eventManager = $this->eventManager;
171
        // @TODO: check if change this value into ['Mail'] not causing any errors!
172
        $eventManager->setIdentifiers(['Mail']);
173
        $p = new Parameters(array('mail' => $this, 'template' => $template));
174
        $event->setParams($p);
175
        $eventManager->trigger('template.pre', $event);
176
        
177
        // get all loaded modules
178
        $moduleManager = $this->moduleManager;
179
        $loadedModules = $moduleManager->getModules();
180
        //get_called_class
181
        $controllerIdentifier = strtolower(substr($controller, 0, strpos($controller, '\\')));
182
        $viewResolver = $this->viewResolver;
183
                
184
        $templateHalf = 'mail/' . $template;
185
        $resource = $viewResolver->resolve($templateHalf);
186
        
187
        if (empty($resource)) {
188
            $templateFull = $controllerIdentifier . '/mail/' . $template;
189
            $resource = $viewResolver->resolve($templateFull);
190
        }
191
        
192
        $__vars = $this->param;
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
193
        if (array_key_exists('this', $__vars)) {
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
194
            unset($__vars['this']);
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
195
        }
196
        extract($__vars);
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
197
        unset($__vars); // remove $__vars from local scope
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
198
        
199
        if ($resource) {
200
            try {
201
                ob_start();
202
                include $resource;
203
                $content = ob_get_clean();
204
                $this->setBody($content);
205
            } catch (\Exception $ex) {
206
                ob_end_clean();
207
                throw $ex;
208
            }
209
            $__vars = get_defined_vars();
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
210
            foreach ($this->param as $key => $value) {
211
                if (isset($__vars[$key])) {
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
212
                    unset($__vars[$key]);
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
213
                }
214
            }
215
            unset($__vars['content'],$__vars['controllerIdentifier'],$__vars['controller'],$__vars['resource'],$__vars['template'],$__vars['viewResolver']);
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
216
            $this->config = $__vars;
0 ignored issues
show
Coding Style introduced by
$__vars does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
217
        }
218
    }
219
    
220
    protected function getTemplate()
221
    {
222
        $template = null;
0 ignored issues
show
Unused Code introduced by
$template is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
223
        if (isset($this->config['templateFull'])) {
224
            $template = $this->config['templateFull'];
225
        } elseif (isset($this->config['templateHalf'])) {
226
            $template = $this->config['templateHalf'];
227
        } else {
228
              throw new \InvalidArgumentException('No template provided for Mail.');
229
        }
230
        return $template;
231
    }
232
    
233
    public function informationComplete()
234
    {
235
        $log = $this->mailLogger;
236
        $template = $this->getTemplate();
237 View Code Duplication
        if (isset($this->config['from'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
238
            $from = $this->config['from'];
239
        } else {
240
            $log->err('A from email address must be provided (Variable $from) in Template: ' . $template);
241
            throw new \InvalidArgumentException('A from email address must be provided (Variable $from) in Template: ' . $template);
242
        }
243 View Code Duplication
        if (isset($this->config['fromName'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
            $fromName = $this->config['fromName'];
245
        } else {
246
            $log->err('A from name must be provided (Variable $fromName) in Template: ' . $template);
247
            throw new \InvalidArgumentException('A from name must be provided (Variable $fromName) in Template: ' . $template);
248
        }
249 View Code Duplication
        if (isset($this->config['subject'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
            $subject = $this->config['subject'];
251
        } else {
252
            $log->err('A subject must be provided (Variable $subject) in Template: ' . $template);
253
            throw new \InvalidArgumentException('A subject must be provided (Variable $subject) in Template: ' . $template);
254
        }
255
        $this->setFrom($from, $fromName);
256
        $this->setSubject($subject);
257
        return $this;
258
    }
259
260
    public function send()
0 ignored issues
show
Coding Style introduced by
function send() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
261
    {
262
        $log = $this->mailLogger;
263
        $this->getHeaders()->addHeaderLine('X-Mailer', 'php/YAWIK');
264
265
        $this->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=UTF-8');
266
267
        $transport = $this->transport;
268
        $erg = false;
269
        try {
270
            $transport->send($this);
271
            $erg = true;
272
            $log->info($this);
273
        } catch (\Exception $e) {
274
            $log->err('Mail failure ' . $e->getMessage());
275
        }
276
        return $erg;
277
    }
278
    
279
	/**
280
	 * @param ContainerInterface $container
281
	 *
282
	 * @return static
283
	 */
284
    public static function factory(ContainerInterface $container)
285
    {
286
        //@TODO: need to define transport to be use during ::send()
287
        $mailLog        = $container->get('Log/Core/Mail');
288
        $viewResolver   = $container->get('ViewResolver');
289
        $eventManager   = $container->get('EventManager');
290
        $moduleManager  = $container->get('ModuleManager');
291
        return new static($mailLog,$viewResolver,$eventManager,$moduleManager);
292
    }
293
}
294