Completed
Push — master ( 4e5a32...533976 )
by methylbro
9s
created

CreateCampaign::email()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 24
cts 24
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 21
nc 2
nop 1
crap 3
1
<?php
2
3
/**
4
 * This file is part of the Mediapart Selligent Client API
5
 *
6
 * CC BY-NC-SA <https://github.com/mediapart/selligent>
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mediapart\Selligent\Request;
13
14
use Mediapart\Selligent\Broadcast\Campaign;
15
use Mediapart\Selligent\Broadcast\Target;
16
use Mediapart\Selligent\Broadcast\Email;
17
18
/**
19
 *
20
 */
21
class CreateCampaign
22
{
23
    /**
24
     * @var \XMLWriter $writer
25
     */
26
    private $writer;
27
28
    /**
29
     * @param \XMLWriter $writer
30
     */
31 1
    public function __construct(\XMLWriter $writer)
32
    {
33 1
        $writer->openMemory();
34 1
        $writer->setIndent(true);
35
36 1
        $this->writer = $writer;
37 1
    }
38
39
    /**
40
     * @param Campaign $campaign
41
     * @return string Xml
42
     */
43 1
    public function basedOn(Campaign $campaign)
44
    {
45 1
        $this->writer->startDocument('1.0');
46 1
        $this->writer->startElement('API');
47
48 1
        $this->campaign($campaign);
49 1
        $this->emails($campaign->getEmails());
50
51 1
        $this->writer->endElement();
52 1
        $this->writer->endDocument();
53
        
54 1
        return $this->writer->outputMemory(TRUE);
55
    }
56
57
    /**
58
     * @param string $name
59
     * @param Array $attributes 
60
     * @param boolena $end
61
     */
62 1
    private function element($name, $attributes = array(), $end = true)
63
    {
64 1
        $this->writer->startElement($name);
65
66 1
        foreach ($attributes as $key => $value) {
67 1
            $this->attr($key, $value);
68 1
        }
69
70 1
        if ($end) {
71 1
            $this->writer->endElement();
72 1
        }
73 1
    }
74
75
    /**
76
     * @param string $name
77
     * @param string $value
78
     */
79 1
    private function attr($name, $value)
80
    {
81 1
        $this->writer->writeAttribute($name, $value);
82 1
    }
83
84
    /**
85
     * @param Campaign $campaign
86
     */
87 1
    private function campaign(Campaign $campaign)
88
    {
89 1
        $this->element(
90 1
            'CAMPAIGN', 
91
            [
92 1
                'NAME' => $campaign->getName(),
93 1
                'STATE' => $campaign->getState(),
94 1
                'FOLDERID' => $campaign->getFolderId(),
95 1
                'START_DT' => $campaign->getStartDate()->format('Ymdhmi'),
96 1
                'DESCRIPTION' => $campaign->getDescription(),
97 1
                'MACATEGORY' => $campaign->getMaCategory(),
98 1
                'PRODUCTID' => $campaign->getProductId(),
99 1
                'CLASHPLANID' => $campaign->getClashPlanId(),
100
            ]
101 1
        );
102 1
    }
103
104
    /**
105
     * @param Email[] $emails
106
     */
107 1
    private function emails($emails)
108
    {
109 1
        $this->writer->startElement('EMAILS');
110
111 1
        foreach ($emails as $email) {
112 1
            $this->email($email);
113 1
        }
114
115 1
        $this->writer->endElement();
116 1
    }
117
118
    /**
119
     * @param Target $target
120
     */
121 1
    private function target(Target $target)
122
    {
123 1
        $this->element(
124 1
            'TARGET',
125
            [
126 1
                'LISTID' => $target->getListId(),
127 1
                'PRIORITY_FIELD' => $target->getPriorityField(),
128 1
                'PRIORITY_SORTING' => $target->getPrioritySorting(),
129 1
                'SEGMENTID' => $target->getSegmentId(),
130 1
                'CONSTRAINT' => $target->getConstraint(),
131 1
                'SCOPES' => $target->getScopes(),
132
            ]
133 1
        );
134 1
    }
135
136
    /**
137
     * @param Email $email
138
     */
139 1
    private function email(Email $email)
140
    {
141 1
        $this->element(
142 1
            'EMAIL', 
143
            [
144 1
                'NAME' => $email->getName(),
145 1
                'FOLDERID' => $email->getFolderId(),
146 1
                'MAILDOMAINID' => $email->getMailDomainId(),
147 1
                'LIST_UNSUBSCRIBE' => $email->canUnsubscribe() ? 'TRUE' : 'FALSE',
148 1
                'QUEUEID' => $email->getQueueId(),
149 1
                'TAG' => $email->getTag(),
150 1
                'MACATEGORY' => $email->getMaCategory(),
151 1
            ],
152
            false
153 1
        );
154
155 1
        $this->target($email->getTarget());
156
157 1
        $this->writer->startElement('CONTENT');
158 1
        foreach ($email->getContent() as $key => $value) {
159 1
            $this->writer->startElement($key);
160 1
            $this->writer->startCData();
161 1
            $this->writer->text($value);
162 1
            $this->writer->endCData();
163 1
            $this->writer->endElement();
164 1
        }
165 1
        $this->writer->endElement();
166
167 1
        $this->writer->endElement();
168 1
    }
169
}
170