Completed
Push — next ( 191339...0efc14 )
by Mathias
10:05
created

DataTransformer::transform()   F

Complexity

Conditions 32
Paths 13440

Size

Total Lines 109
Code Lines 63

Duplication

Lines 12
Ratio 11.01 %

Importance

Changes 0
Metric Value
dl 12
loc 109
rs 2
c 0
b 0
f 0
cc 32
eloc 63
nc 13440
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * YAWIK Stackoverflow API
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2016 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace StackoverflowApi\Client;
12
13
use Jobs\Entity\CoordinatesInterface;
14
use Jobs\Entity\JobInterface;
15
use Jobs\View\Helper\ApplyUrl;
16
use Organizations\ImageFileCache\Manager as ImageManager;
17
use Zend\View\Helper\ServerUrl;
18
19
/**
20
 * Transformer for converting a Yawik Job to Stackoverflow XML
21
 * 
22
 * @author Mathias Gelhausen <[email protected]>
23
 * @codeCoverageIgnore
24
 * @todo write test
25
 * @since 0.1.0
26
 */
27
class DataTransformer 
28
{
29
30
    /**
31
     * Url helper for the apply link.
32
     *
33
     * @var ApplyUrl
34
     */
35
    protected $applyUrlHelper;
36
37
    /**
38
     * Server url view Helper
39
     *
40
     * @var ServerUrl
41
     */
42
    protected $serverUrlHelper;
43
44
    /**
45
     * Organization Image File Cache Manager
46
     *
47
     * @var ImageManager
48
     */
49
    protected $organizationImageManager;
50
51
    /**
52
     * @param ApplyUrl $applyUrlHelper
53
     *
54
     * @return self
55
     */
56
    public function setApplyUrlHelper($applyUrlHelper)
57
    {
58
        $this->applyUrlHelper = $applyUrlHelper;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @return ApplyUrl
65
     */
66
    public function getApplyUrlHelper()
67
    {
68
        return $this->applyUrlHelper;
69
    }
70
71
    /**
72
     * @param \Organizations\ImageFileCache\Manager $organizationImageManager
73
     *
74
     * @return self
75
     */
76
    public function setOrganizationImageManager($organizationImageManager)
77
    {
78
        $this->organizationImageManager = $organizationImageManager;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return \Organizations\ImageFileCache\Manager
85
     */
86
    public function getOrganizationImageManager()
87
    {
88
        return $this->organizationImageManager;
89
    }
90
91
    /**
92
     * @param \Zend\View\Helper\ServerUrl $serverUrlHelper
93
     *
94
     * @return self
95
     */
96
    public function setServerUrlHelper($serverUrlHelper)
97
    {
98
        $this->serverUrlHelper = $serverUrlHelper;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return \Zend\View\Helper\ServerUrl
105
     */
106
    public function getServerUrlHelper()
107
    {
108
        return $this->serverUrlHelper;
109
    }
110
111
112
    /**
113
     * Transform a job to stackoverflow XML
114
     *
115
     * @param JobInterface $job
116
     * @param array        $options
117
     *
118
     * @return string
119
     */
120
    public function transform(JobInterface $job, $options = [])
121
    {
122
        $applyUrl = $this->getApplyUrlHelper();
123
        $serverUrl = $this->getServerUrlHelper();
124
        $imageManager = $this->getOrganizationImageManager();
125
126
        /* @var \Jobs\Entity\Job $job */
127
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><job></job>');
128
129
        /*
130
         * FOR TESTING
131
         */
132
        $xml->addChild('test', 'true');
133
134
135
        $xml->addChild('action', isset($options['action']) ? $options['action'] : 'post');
136
137
        if (isset($options['externalId'])) {
138
            $xml->addChild('jobid', $options['externalId']);
139
        }
140
141
        $xml->addChild('title', $job->getTitle());
142
        $xml->addChild('company', $job->getOrganization()->getOrganizationName()->getName());
143
        $xml->addChild('companyurl', $job->getOrganization()->getContact()->getWebsite() ?: 'http://cross-solution.de');
144
        if ($image = $job->getOrganization()->getImage() && $serverUrl && $imageManager) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $image = ($job->getOrgan...erUrl && $imageManager), Probably Intended Meaning: ($image = $job->getOrgan...erUrl) && $imageManager
Loading history...
145
            $imageUri = $imageManager->getUri($image);
146
            $xml->addChild('logourl', $serverUrl($imageUri));
147
        }
148
149
        if ($companyDesc = $job->getOrganization()->getDescription()) {
150
            $xml->addChild('aboutcompany', $companyDesc);
151
        }
152
153
        $xml->addChild('vendorid', $job->getId());
154
155
        $atsMode = $job->getAtsMode();
156
        if ($atsMode->isDisabled()) {
157
            $xml->addChild('howtoapply', 'postalisch');
158
        } else if ($atsMode->isEmail()) {
159
            $xml->addChild('howtoapply', $atsMode->getEmail());
160
        } else if ($atsMode->isUri()) {
161
            $xml->addChild('howtoapply', $atsMode->getUri());
162
        } else if (is_callable($applyUrl) && $serverUrl) {
163
            $xml->addChild('howtoapply', $serverUrl($applyUrl($job, ['linkOnly' => true, 'absolute' => true])));
164
        } else {
165
            $xml->addChild('howtoapply', 'postalisch');
166
        }
167
168
169
170
        $locations = $job->getLocations();
171
        if ($locations->count()) {
172
            $loc = $xml->addChild('locations');
173
174
            foreach ($locations as $location) {
175
                $l = $loc->addChild('location', $location->getCity());
176
                $coords = $location->getCoordinates();
177
                if (CoordinatesInterface::TYPE_POINT == $coords->getType()) {
178
                    $c = $coords->getCoordinates();
179
                    $l->addAttribute('lon', $c[0]);
180
                    $l->addAttribute('lat', $c[1]);
181
                }
182
            }
183
        }
184
185
        foreach (['topspot', 'featured', 'remote', 'relocation', 'visasponsorship', 'sysadmin'] as $boolOpt) {
186
            if (isset($options[$boolOpt])) {
187
                $xml->addChild($boolOpt, $options[$boolOpt] ? 'true' : 'false');
188
            }
189
        }
190
191
        foreach (['length', 'coupon', 'pixel'] as $strOpt) {
192
            if (isset($options[$strOpt])) {
193
                $xml->addChild($strOpt, $options[$strOpt]);
194
            }
195
        }
196
197
        $xml->addChild('description', html_entity_decode($job->getTemplateValues()->getDescription())  ?: '<p></p>');
198
199
        if ($requirements = $job->getTemplateValues()->getRequirements()) {
200
            $xml->addChild('requirements', $requirements);
201
        }
202
203
        $tags = $xml->addChild('tags');
204
        if (isset($options['keywords']) && is_array($options['keyword'])) {
205
            foreach ($options['keywords'] as $keyword) {
206
                $tags->addChild('tag', $keyword);
207
            }
208
        } else {
209
            $tags->addChild('tag', 'none');
210
        }
211
212 View Code Duplication
        if (isset($options['advertisingregions']) && is_array($options['advertisingregions'])) {
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...
213
            $regions = $xml->addChild('advertisingregioncodes');
214
            foreach ($options['advertisingregions'] as $adreg) {
215
                $regions->addChild('regioncode', $adreg);
216
            }
217
        }
218
219 View Code Duplication
        if (isset($options['advertisingcountries']) && is_array($options['advertisingcountries'])) {
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...
220
            $countries = $xml->addChild('advertisingcountrycodes');
221
            foreach ($options['advertisingcountries'] as $adcountry) {
222
                $countries->addChild('regioncode', $adcountry);
223
            }
224
        }
225
226
227
        return $xml->asXml();
228
    }
229
}