Completed
Push — next ( ba018c...5105e3 )
by Mathias
10:34
created

DataTransformer::setApplyUrlHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
17
/**
18
 * Transformer for converting a Yawik Job to Stackoverflow XML
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @codeCoverageIgnore
22
 * @todo write test
23
 * @since 0.1.0
24
 */
25
class DataTransformer 
26
{
27
28
    /**
29
     * Url helper for the apply link.
30
     *
31
     * @var ApplyUrl
32
     */
33
    protected $applyUrlHelper;
34
35
    /**
36
     * @param ApplyUrl $applyUrlHelper
37
     *
38
     * @return self
39
     */
40
    public function setApplyUrlHelper($applyUrlHelper)
41
    {
42
        $this->applyUrlHelper = $applyUrlHelper;
43
44
        return $this;
45
    }
46
47
    /**
48
     * @return ApplyUrl
49
     */
50
    public function getApplyUrlHelper()
51
    {
52
        return $this->applyUrlHelper;
53
    }
54
55
56
    /**
57
     * Transform a job to stackoverflow XML
58
     *
59
     * @param JobInterface $job
60
     * @param array        $options
61
     *
62
     * @return string
63
     */
64
    public function transform(JobInterface $job, $options = [])
65
    {
66
        $applyUrl = $this->getApplyUrlHelper();
67
68
        /* @var \Jobs\Entity\Job $job */
69
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><job></job>');
70
71
        /*
72
         * FOR TESTING
73
         */
74
        $xml->addChild('test', 'true');
75
76
77
        $xml->addChild('action', isset($options['action']) ? $options['action'] : 'post');
78
79
        if (isset($options['externalId'])) {
80
            $xml->addChild('jobid', $options['externalId']);
81
        }
82
83
        $xml->addChild('title', $job->getTitle());
84
        $xml->addChild('company', $job->getOrganization()->getOrganizationName()->getName());
85
        $xml->addChild('companyurl', $job->getOrganization()->getContact()->getWebsite() ?: 'http://cross-solution.de');
86
        if ($image = $job->getOrganization()->getImage()) {
0 ignored issues
show
Unused Code introduced by
$image is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
            $xml->addChild('logourl', $job->getOrganization()->getImage()->getUri());
88
        }
89
90
        if ($companyDesc = $job->getOrganization()->getDescription()) {
91
            $xml->addChild('aboutcompany', $companyDesc);
92
        }
93
94
        $xml->addChild('vendorid', $job->getId());
95
96
        $atsMode = $job->getAtsMode();
97
        if ($atsMode->isDisabled()) {
98
            $xml->addChild('howtoapply', 'postalisch');
99
        } else if ($atsMode->isEmail()) {
100
            $xml->addChild('howtoapply', $atsMode->getEmail());
101
        } else if ($atsMode->isUri()) {
102
            $xml->addChild('howtoapply', $atsMode->getUri());
103
        } else if (is_callable($applyUrl)) {
104
            $xml->addChild('howtoapply', $applyUrl($job, ['linkOnly' => true, 'absolute' => true]));
105
        } else {
106
            $xml->addChild('howtoapply', 'postalisch');
107
        }
108
109
110
111
        $locations = $job->getLocations();
112
        if ($locations->count()) {
113
            $loc = $xml->addChild('locations');
114
115
            foreach ($locations as $location) {
116
                $l = $loc->addChild('location', $location->getCity());
117
                $coords = $location->getCoordinates();
118
                if (CoordinatesInterface::TYPE_POINT == $coords->getType()) {
119
                    $c = $coords->getCoordinates();
120
                    $l->addAttribute('lon', $c[0]);
121
                    $l->addAttribute('lat', $c[1]);
122
                }
123
            }
124
        }
125
126
        foreach (['topspot', 'featured', 'remote', 'relocation', 'visasponsorship', 'sysadmin'] as $boolOpt) {
127
            if (isset($options[$boolOpt])) {
128
                $xml->addChild($boolOpt, $options[$boolOpt] ? 'true' : 'false');
129
            }
130
        }
131
132
        foreach (['length', 'coupon', 'pixel'] as $strOpt) {
133
            if (isset($options[$strOpt])) {
134
                $xml->addChild($strOpt, $options[$strOpt]);
135
            }
136
        }
137
138
        $xml->addChild('description', $job->getTemplateValues()->getDescription()  ?: '<p>Porta magna lectus architect evangelist. Monad dolores unibody gubergren combinator VC. NoSQL viral pre-IPO disruptive in event-driven vel :). Exploit monkey patch gubergren functional agile. Consetetur justo architect async crowdfunding eos usability! Kasd massa dolores tincidunt web-scale vesting schedule diam :). Et stet in agile.</p>');
139
140
        if ($requirements = $job->getTemplateValues()->getRequirements()) {
141
            $xml->addChild('requirements', $requirements);
142
        }
143
144
        $tags = $xml->addChild('tags');
145
        if (isset($options['keywords']) && is_array($options['keyword'])) {
146
            foreach ($options['keywords'] as $keyword) {
147
                $tags->addChild('tag', $keyword);
148
            }
149
        } else {
150
            $tags->addChild('tag', 'none');
151
        }
152
153 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...
154
            $regions = $xml->addChild('advertisingregioncodes');
155
            foreach ($options['advertisingregions'] as $adreg) {
156
                $regions->addChild('regioncode', $adreg);
157
            }
158
        }
159
160 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...
161
            $countries = $xml->addChild('advertisingcountrycodes');
162
            foreach ($options['advertisingcountries'] as $adcountry) {
163
                $countries->addChild('regioncode', $adcountry);
164
            }
165
        }
166
167
168
        return $xml->asXml();
169
    }
170
}