CreateCampaign::element()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 4
nop 3
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
    const DATETIME_FORMAT = 'YmdHis';
24
25
    /**
26
     * @var \XMLWriter $writer
27
     */
28
    private $writer;
29
30
    /**
31
     * @param \XMLWriter $writer
32
     */
33 5
    public function __construct(\XMLWriter $writer)
34
    {
35 5
        $writer->openMemory();
36 5
        $writer->setIndent(true);
37
38 5
        $this->writer = $writer;
39 5
    }
40
41
    /**
42
     * @param Campaign $campaign
43
     * @return string Xml
44
     */
45 5
    public function basedOn(Campaign $campaign)
46
    {
47 5
        $this->writer->startDocument('1.0');
48 5
        $this->writer->startElement('API');
49
50 5
        $this->campaign($campaign);
51 5
        $this->emails($campaign->getEmails());
52
53 5
        $this->writer->endElement();
54 5
        $this->writer->endDocument();
55
        
56 5
        return $this->writer->outputMemory(TRUE);
57
    }
58
59
    /**
60
     * @param string $name
61
     * @param Array $attributes 
62
     * @param boolena $end
63
     */
64 5
    private function element($name, $attributes = array(), $end = true)
65
    {
66 5
        $this->writer->startElement($name);
67
68 5
        foreach ($attributes as $key => $value) {
69 5
            $this->attr($key, $value);
70
        }
71
72 5
        if ($end) {
73 5
            $this->writer->endElement();
74
        }
75 5
    }
76
77
    /**
78
     * @param string $name
79
     * @param string $value
80
     */
81 5
    private function attr($name, $value)
82
    {
83 5
        $this->writer->writeAttribute($name, $value);
84 5
    }
85
86
    /**
87
     * @param Campaign $campaign
88
     */
89 5
    private function campaign(Campaign $campaign)
90
    {
91 5
        $this->element(
92 5
            'CAMPAIGN', 
93
            [
94 5
                'NAME' => $campaign->getName(),
95 5
                'STATE' => $campaign->getState(),
96 5
                'FOLDERID' => $campaign->getFolderId(),
97 5
                'START_DT' => $campaign->getStartDate()->format(self::DATETIME_FORMAT),
98 5
                'DESCRIPTION' => $campaign->getDescription(),
99 5
                'MACATEGORY' => $campaign->getMaCategory(),
100 5
                'PRODUCTID' => $campaign->getProductId(),
101 5
                'CLASHPLANID' => $campaign->getClashPlanId(),
102
            ]
103
        );
104 5
    }
105
106
    /**
107
     * @param Email[] $emails
108
     */
109 5
    private function emails($emails)
110
    {
111 5
        $this->writer->startElement('EMAILS');
112
113 5
        foreach ($emails as $email) {
114 2
            $this->email($email);
115
        }
116
117 5
        $this->writer->endElement();
118 5
    }
119
120
    /**
121
     * @param Target $target
122
     */
123 2
    private function target(Target $target)
124
    {
125 2
        $this->element(
126 2
            'TARGET',
127
            [
128 2
                'LISTID' => $target->getListId(),
129 2
                'PRIORITY_FIELD' => $target->getPriorityField(),
130 2
                'PRIORITY_SORTING' => $target->getPrioritySorting(),
131 2
                'SEGMENTID' => $target->getSegmentId(),
132 2
                'CONSTRAINT' => $target->getConstraint(),
133 2
                'SCOPES' => $target->getScopes(),
134
            ]
135
        );
136 2
    }
137
138
    /**
139
     * @param Email $email
140
     */
141 2
    private function email(Email $email)
142
    {
143 2
        $this->element(
144 2
            'EMAIL', 
145
            [
146 2
                'NAME' => $email->getName(),
147 2
                'FOLDERID' => $email->getFolderId(),
148 2
                'MAILDOMAINID' => $email->getMailDomainId(),
149 2
                'LIST_UNSUBSCRIBE' => $email->canUnsubscribe() ? 'TRUE' : 'FALSE',
150 2
                'QUEUEID' => $email->getQueueId(),
151 2
                'TAG' => $email->getTag(),
152 2
                'MACATEGORY' => $email->getMaCategory(),
153
            ],
154 2
            false
155
        );
156
157 2
        $this->target($email->getTarget());
158
159 2
        $this->writer->startElement('CONTENT');
160 2
        if ($email->hasHyperlinksToSensors()) {
161 1
            $this->attr('HYPERLINKS_TO_SENSORS', 1);
162
        }
163 2
        foreach ($email->getContent() as $key => $value) {
164 1
            $this->writer->startElement($key);
165 1
            $this->writer->startCData();
166 1
            $this->writer->text($value);
167 1
            $this->writer->endCData();
168 1
            $this->writer->endElement();
169
        }
170 2
        $this->writer->endElement();
171
172 2
        $this->writer->endElement();
173 2
    }
174
}
175