DataTransformer::transform()   F
last analyzed

Complexity

Conditions 40
Paths > 20000

Size

Total Lines 146

Duplication

Lines 21
Ratio 14.38 %

Code Coverage

Tests 0
CRAP Score 1640

Importance

Changes 0
Metric Value
dl 21
loc 146
ccs 0
cts 103
cp 0
rs 0
c 0
b 0
f 0
cc 40
nc 344064
nop 2
crap 1640

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 Core\Entity\Collection\ArrayCollection;
14
use Jobs\Entity\CoordinatesInterface;
15
use Jobs\Entity\JobInterface;
16
use Jobs\Entity\Location;
17
use Jobs\View\Helper\ApplyUrl;
18
use Organizations\ImageFileCache\Manager as ImageManager;
19
use StackoverflowApi\Utils\XmlBuilder;
20
use Zend\View\Helper\ServerUrl;
21
22
/**
23
 * Transformer for converting a Yawik Job to Stackoverflow XML
24
 * 
25
 * @author Mathias Gelhausen <[email protected]>
26
 * @todo write test
27
 * @since 0.1.0
28
 */
29
class DataTransformer 
30
{
31
32
    /**
33
     * Url helper for the apply link.
34
     *
35
     * @var ApplyUrl
36
     */
37
    protected $applyUrlHelper;
38
39
    /**
40
     * Server url view Helper
41
     *
42
     * @var ServerUrl
43
     */
44
    protected $serverUrlHelper;
45
46
    /**
47
     * Organization Image File Cache Manager
48
     *
49
     * @var ImageManager
50
     */
51
    protected $organizationImageManager;
52
53
    /**
54
     *
55
     *
56
     * @var JobDescriptionFilter
57
     */
58
    protected $descriptionFilter;
59
60
    /**
61
     * @param ApplyUrl $applyUrlHelper
62
     *
63
     * @return self
64
     */
65
    public function setApplyUrlHelper($applyUrlHelper)
66
    {
67
        $this->applyUrlHelper = $applyUrlHelper;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return ApplyUrl
74
     */
75
    public function getApplyUrlHelper()
76
    {
77
        return $this->applyUrlHelper;
78
    }
79
80
    /**
81
     * @param \Organizations\ImageFileCache\Manager $organizationImageManager
82
     *
83
     * @return self
84
     */
85
    public function setOrganizationImageManager($organizationImageManager)
86
    {
87
        $this->organizationImageManager = $organizationImageManager;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return \Organizations\ImageFileCache\Manager
94
     */
95
    public function getOrganizationImageManager()
96
    {
97
        return $this->organizationImageManager;
98
    }
99
100
    /**
101
     * @param \Zend\View\Helper\ServerUrl $serverUrlHelper
102
     *
103
     * @return self
104
     */
105
    public function setServerUrlHelper($serverUrlHelper)
106
    {
107
        $this->serverUrlHelper = $serverUrlHelper;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return \Zend\View\Helper\ServerUrl
114
     */
115
    public function getServerUrlHelper()
116
    {
117
        return $this->serverUrlHelper;
118
    }
119
120
    /**
121
     * @return JobDescriptionFilter
122
     */
123
    public function getDescriptionFilter()
124
    {
125
        if (!$this->descriptionFilter) {
126
            $this->descriptionFilter = new JobDescriptionFilter();
127
        }
128
        
129
        return $this->descriptionFilter;
130
    }
131
132
    /**
133
     * @param JobDescriptionFilter $descriptionFilter
134
     *
135
     * @return self
136
     */
137
    public function setDescriptionFilter($descriptionFilter)
138
    {
139
        $this->descriptionFilter = $descriptionFilter;
140
141
        return $this;
142
    }
143
144
145
146
147
    /**
148
     * Transform a job to stackoverflow XML
149
     *
150
     * @param JobInterface $job
151
     * @param array        $options
152
     *
153
     * @return string
154
     */
155
    public function transform(JobInterface $job, $options = [])
156
    {
157
        $applyUrl = $this->getApplyUrlHelper();
158
        $serverUrl = $this->getServerUrlHelper();
159
        $imageManager = $this->getOrganizationImageManager();
160
161
        $jobSpec = [
162
            'action' => isset($options['action']) ? $options['action'] : 'post',
163
            //'test' => 'true',
164
165
        ];
166
167
        if (isset($options['externalId'])) {
168
            $jobSpec['jobid'] = $options['externalId'];
169
        }
170
171
        $jobSpec[':title'] = $job->getTitle();
172
        $jobSpec[':company'] = $job->getOrganization()->getOrganizationName()->getName();
173
        $jobSpec[':companyurl'] = $job->getOrganization()->getContact()->getWebsite() ?: '';
174
175
        if (($image = $job->getOrganization()->getImage()) && $serverUrl && $imageManager) {
176
            $imageUri = $imageManager->getUri($image);
177
            $jobSpec['logourl'] = $serverUrl($imageUri);
178
        }
179
180
        if ($companyDesc = $job->getOrganization()->getDescription()) {
181
            $jobSpec[':aboutcompany'] = $companyDesc;
182
        }
183
184
        $jobSpec['vendorid'] = $job->getId();
185
186
        if (!empty($options['applyUrl'])) {
187
            $jobSpec['howtoapply'] = $options['applyUrl'];
188
        } else if ($contactEmail = $job->getContactEmail()) {
189
            $jobSpec['howtoapply'] = $contactEmail;
190
        } else {
191
            $atsMode = $job->getAtsMode();
192
            if ($atsMode->isDisabled()) {
193
                $jobSpec['howtoapply'] = 'postalisch';
194
            } else if ($atsMode->isEmail()) {
195
                $jobSpec['howtoapply'] = $atsMode->getEmail();
196
            } else if ($atsMode->isUri()) {
197
                $jobSpec['howtoapply'] = $atsMode->getUri();
198
            } else if (is_callable($applyUrl) && $serverUrl) {
199
                $jobSpec['howtoapply'] = $serverUrl($applyUrl($job, ['linkOnly' => true, 'absolute' => true]));
200
            } else {
201
                $jobSpec['howtoapply'] =  'postalisch';
202
            }
203
        }
204
205
206
207
        /* Location override by additional data */
208
        if (isset($options['location'])) {
209
            $location = new Location($options['location']);
210
            $locations = new ArrayCollection([$location]);
211
        } else {
212
            $locations = $job->getLocations();
213
        }
214
215
        if ($locations->count()) {
216
            $loc = [];
217
218
            foreach ($locations as $location) {
219
                /* \Jobs\Entity\Location $location */
220
                $tmpLoc = [];
221
                $coords = $location->getCoordinates();
222
                $postalCode = $location->getPostalCode();
223
                $city = $location->getCity();
224
                $country = $location->getCountry();
225
                $region = $location->getRegion();
226
227
                $str = '';
228
                if ($postalCode) { $str .= $postalCode . ' '; }
229
                if ($city) { $str .= $city; }
230
                if ($region) { $str .= ', ' . $region; }
231
                if ($country) { $str .= ', ' . $country; }
232
233
234
                /*
235
                 * see https://talent.stackoverflow.com/de/api/doc
236
                 * Value of location tag should only be the city name.
237
                 * Additional address information must be in the "address" attribute
238
                 */
239
                $tmpLoc['_text'] = $city;
240
                if ($str != $city) { $tmpLoc['@address'] = $str; }
241
                if ($coords) {
242
                    $coords = $coords->getCoordinates();
243
                    $tmpLoc['@lon'] = str_replace(',', '.', (string) $coords[0]);
244
                    $tmpLoc['@lat'] = str_replace(',', '.', (string) $coords[1]);
245
                }
246
247
                $loc[] = $tmpLoc;
248
            }
249
250
            $jobSpec['locations']['location'] = $loc;
251
        }
252
253
        foreach (['topspot', 'featured', 'remote', 'relocation', 'visasponsorship', 'sysadmin'] as $boolOpt) {
254
            if (isset($options[$boolOpt])) {
255
                $jobSpec[$boolOpt] = $options[$boolOpt] ? 'true' : 'false';
256
            }
257
        }
258
259
        foreach (['length', 'coupon', 'pixel'] as $strOpt) {
260
            if (isset($options[$strOpt])) {
261
                $jobSpec[$strOpt] = $options[$strOpt];
262
            }
263
        }
264
265
        $link = $job->getLink();
266
        $jobSpec[':description'] = $link ? $this->getDescriptionFilter()->filter($link) : '<p></p>';
267
268
        if ($requirements = $job->getTemplateValues()->getRequirements()) {
269
            $jobSpec[':requirements'] = $requirements;
270
        }
271
272
        $tags = [];
273 View Code Duplication
        if (isset($options['keywords']) && is_array($options['keyword'])) {
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...
274
            foreach ($options['keywords'] as $keyword) {
275
                $tags[] = ['tag' => $keyword];
276
            }
277
        } else {
278
            $tags[] = ['tag' => 'none'];
279
        }
280
        $jobSpec['tags'] = $tags;
281
282 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...
283
            $regions = [];
284
            foreach ($options['advertisingregions'] as $adreg) {
285
                $regions[] = ['regioncode' => $adreg];
286
            }
287
            $jobSpec['advertisingregions'] = $regions;
288
        }
289
290 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...
291
            $countries = [];
292
            foreach ($options['advertisingcountries'] as $adcountry) {
293
                $countries[] = ['regioncode' => $adcountry];
294
            }
295
            $jobSpec['advertisingcountrycodes'] = $countries;
296
        }
297
298
299
        return XmlBuilder::createXml(['job' => $jobSpec]);
300
    }
301
}
302