Completed
Push — master ( 5a9770...5a03d4 )
by Antoine
08:45
created

UpdateTemplateParametersListener::update()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 8.9197
cc 4
eloc 9
nc 3
nop 1
crap 4
1
<?php
2
/**
3
 * Copyright (c) 2014 Roave, LLC.
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 *   * Redistributions of source code must retain the above copyright
11
 *     notice, this list of conditions and the following disclaimer.
12
 *
13
 *   * Redistributions in binary form must reproduce the above copyright
14
 *     notice, this list of conditions and the following disclaimer in
15
 *     the documentation and/or other materials provided with the
16
 *     distribution.
17
 *
18
 *   * Neither the names of the copyright holders nor the names of the
19
 *     contributors may be used to endorse or promote products derived
20
 *     from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
 * POSSIBILITY OF SUCH DAMAGE.
34
 *
35
 * @author    Antoine Hedgecock
36
 *
37
 * @copyright 2014 Roave, LLC
38
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
39
 */
40
41
namespace Roave\EmailTemplates\Service\Template\Listener;
42
43
use Roave\EmailTemplates\Entity\TemplateEntity;
44
use Roave\EmailTemplates\Options\TemplateServiceOptions;
45
use Roave\EmailTemplates\Repository\TemplateRepositoryInterface;
46
use Roave\EmailTemplates\Service\TemplateService;
47
use Roave\EmailTemplates\Service\TemplateServiceInterface;
48
use Zend\EventManager\Event;
49
use Zend\EventManager\EventManagerInterface;
50
use Zend\EventManager\ListenerAggregateInterface;
51
use Zend\EventManager\ListenerAggregateTrait;
52
53
class UpdateTemplateParametersListener implements ListenerAggregateInterface
54
{
55
    use ListenerAggregateTrait;
56
57
    /**
58
     * @var TemplateRepositoryInterface
59
     */
60
    private $templateRepository;
61
    /**
62
     * @var TemplateServiceOptions
63
     */
64
    private $options;
65
66
    /**
67
     * @param TemplateRepositoryInterface $templateRepository
68
     * @param TemplateServiceOptions      $options
69
     */
70
    public function __construct(TemplateRepositoryInterface $templateRepository, TemplateServiceOptions $options)
71
    {
72
        $this->options            = $options;
73
        $this->templateRepository = $templateRepository;
74
    }
75
76
    /**
77
     * Attach one or more listeners
78
     *
79
     * Implementors may add an optional $priority argument; the EventManager
80
     * implementation will pass this to the aggregate.
81
     *
82
     * @param EventManagerInterface $events
83
     *
84
     * @return void
85
     */
86 1
    public function attach(EventManagerInterface $events)
87
    {
88 1
        $this->listeners[] = $events->attach(TemplateService::EVENT_RENDER, [$this, 'update']);
89 1
    }
90
91
    /**
92
     * @param $event
93
     */
94 2
    public function update(Event $event)
95
    {
96
        /**
97
         * @var TemplateEntity           $template
98
         * @var array|\Traversable       $parameters
99
         * @var TemplateServiceInterface $templateService
100
         */
101 2
        $service    = $event->getTarget();
102 2
        $template   = $event->getParam('template');
103 2
        $parameters = $event->getParam('parameters');
104
105 2
        if (!$template->getUpdateParameters() && !$this->options->isAlwaysUpdateParameters()) {
106 1
            return;
107
        }
108
109
        // fetch all the templates with the id
110 1
        $templates = $this->templateRepository->getById($template->getId());
111
112 1
        foreach ($templates as $t) {
113 1
            $service->update(['parameters' => $parameters, 'updateParams' => false], $t);
114
        }
115 1
    }
116
}
117