Completed
Pull Request — master (#644)
by Carsten
07:49
created

JsonLdProvider::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright https://yawik.org/COPYRIGHT.php
8
 */
9
10
/** */
11
namespace Jobs\Entity\Decorator;
12
13
use Doctrine\Common\Collections\Collection;
14
use Jobs\Entity\JobInterface;
15
use Jobs\Entity\JsonLdProviderInterface;
16
use Jobs\Entity\TemplateValuesInterface;
17
use Laminas\Json\Json;
18
19
/**
20
 * Decorates a job with implementing a toJsonLd method.
21
 *
22
 * This decorator *does not* delegate other methods.
23
 *
24
 * @author Mathias Gelhausen <[email protected]>
25
 * @author Carsten Bleek <[email protected]>
26
 * @todo write test
27
 */
28
class JsonLdProvider implements JsonLdProviderInterface
29
{
30
31
    /**
32
     * the decorated job entity.
33
     *
34
     * @var \Jobs\Entity\JobInterface|\Jobs\Entity\Job
35
     */
36
    private $job;
37
38
    private $options = [
39
        'server_url' => '',
40
    ];
41 5
42
    /**
43 5
     * @param JobInterface $job
44
     */
45
    public function __construct(JobInterface $job, ?array $options = null)
46
    {
47 4
        $this->job = $job;
48
        if ($options) {
49 4
            $this->setOptions($options);
50 4
        }
51
    }
52 4
53 4
    public function setOptions(array $options): void
54 4
    {
55 4
        $new = array_merge($this->options, $options);
56 4
        $this->options = array_intersect_key($new, $this->options);
57 4
    }
58 4
59 4
    public function toJsonLd($default=[])
60
    {
61
        $organization = $this->job->getOrganization();
62 4
        $organizationName = $organization ? $organization->getOrganizationName()->getName() : $this->job->getCompany();
63 4
64 4
        $dateStart = $this->job->getDatePublishStart();
65 4
        $dateStart = $dateStart ? $dateStart->format('Y-m-d H:i:s') : null;
66 4
        $dateEnd = $this->job->getDatePublishEnd();
67
        $dateEnd = $dateEnd ? $dateEnd->format('Y-m-d H:i:s') : null;
68 4
        if (!$dateEnd) {
69 4
            $dateEnd = new \DateTime($dateStart);
70 4
            $dateEnd->add(new \DateInterval("P180D"));
71
            $dateEnd = $dateEnd->format('Y-m-d H:i:s');
72
        }
73 4
        $logo = $this->getLogo();
74 4
75 4
        $array = [
76
            '@context' => 'http://schema.org/',
77 4
            '@type' => 'JobPosting',
78 4
            'title' => $this->job->getTitle(),
79 4
            'description' => $this->getDescription($this->job->getTemplateValues()),
80
            'datePosted' => $dateStart,
81
            'identifier' => [
82 4
                '@type' => 'PropertyValue',
83
                'value' => $this->job->getApplyId(),
84 4
                'name' => $organizationName,
85
            ],
86
            'hiringOrganization' => [
87
                '@type' => 'Organization',
88
                'name' => $organizationName,
89
                'logo' => $logo ? rtrim($this->options['server_url'], '/') . $logo : '',
90 4
            ],
91 4
            'jobLocation' => $this->getLocations($this->job->getLocations()),
92
            'employmentType' => $this->job->getClassifications()->getEmploymentTypes()->getValues(),
93 4
            'validThrough' => $dateEnd
94 4
        ];
95
96
        $array += $this->generateSalary();
97
98
        /**
99
        * TODO: make this working
100
        
101
        $array=array_merge_recursive($this->getDefault,$default,$array);
102
        
103
        */
104 4
        
105
        return Json::encode($array);
106 4
    }
107 4
108 4
    /**
109 4
     * try to get the logo of an organization. Fallback: logoRef of job posting
110
     */
111 4
    private function getLogo() {
112
        $organization = $this->job->getOrganization();
113 4
114 4
        $organizationLogo = ($organization && $organization->getImage())? $organization->getImage()->getUri() : $this->job->getLogoRef();
115 4
        return $organizationLogo;
116 4
    }
117 4
118 4
    /**
119
     * Generates a location array
120
     *
121
     * @param Collection $locations,
122
     *
123 4
     * @return array
124
     */
125
    private function getLocations($locations)
126
    {
127
        $array=[];
128
        foreach ($locations as $location) { /* @var \Core\Entity\LocationInterface $location */
129
            array_push(
130
                $array,
131
                [
132
                    '@type' => 'Place',
133 4
                    'address' => [
134
                        '@type' => 'PostalAddress',
135 4
                        'streetAddress' => $location->getStreetname() .' '.$location->getStreetnumber(),
136
                        'postalCode' => $location->getPostalCode(),
137 4
                        'addressLocality' => $location->getCity(),
138
                        'addressCountry' => $location->getCountry(),
139
                        'addressRegion' => $location->getRegion(),
140 4
                    ]
141
                ]
142
            );
143
        }
144
        return $array;
145 4
    }
146 4
147 4
    /**
148 4
     * Generates a description from template values
149 4
     *
150 4
     * @param TemplateValuesInterface $values
151
     *
152
     * @return string
153
     */
154 4
    private function getDescription(TemplateValuesInterface $values)
155
    {
156
        $html = $values->getHtml();
157 4
158
        if ($html) {
159 4
            $description=sprintf("%s", $values->getHtml() );
160
        } else {
161 4
            $description=sprintf(
162 3
                "<p>%s</p>".
163
                "<h1>%s</h1>".
164
                "<h3>Requirements</h3><p>%s</p>".
165
                "<h3>Qualifications</h3><p>%s</p>".
166
                "<h3>Benefits</h3><p>%s</p>",
167 1
                $values->getDescription(),
168 1
                $values->getTitle(),
169
                $values->getRequirements(),
170 1
                $values->getQualifications(),
171 1
                $values->getBenefits()
172 1
            );
173
        }
174
175
        return $description;
176
    }
177
178
    private function generateSalary()
179
    {
180
        $salary = $this->job->getSalary();
181
182
        if (!$salary || null === $salary->getValue()) {
183
            return [];
184
        }
185
186
        return [
187
            'baseSalary' => [
188
                '@type' => 'MonetaryAmount',
189
                'currency' => $salary->getCurrency(),
190
                'value' => [
191
                    '@type' => 'QuantitiveValue',
192
                    'value' => $salary->getValue(),
193
                    'unitText' => $salary->getUnit()
194
                ],
195
            ],
196
        ];
197
    }
198
199
    private function getDefault(){
200
        return [
201
            '@context'=>'http://schema.org/',
202
            '@type' => 'JobPosting',
203
            'identifier' => [
204
                '@type' => 'PropertyValue',
205
            ],
206
            'hiringOrganization' => [
207
                '@type' => 'Organization',
208
            ],
209
            'jobLocation' => [
210
                '@type' => 'Place',
211
                'address' => [
212
                    '@type' => 'PostalAddress',
213
                    'addressCountry' => 'DE',
214
                ]
215
            ],
216
            'baseSalary' => [
217
                '@type' => 'MonetaryAmount',
218
                'currency' => 'EUR',
219
                'value' => [
220
                    '@type' => 'QuantitiveValue',
221
                    'value' => 'node',
222
                    'unitText' => 'YEAR'
223
                ]
224
            ]
225
                ];
226
    }
227
228
}
229
230
231
232
233
234
array(10) {
235
    ["@context"]=>
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_DOUBLE_ARROW on line 235 at column 16
Loading history...
236
    string(18) "http://schema.org/"
237
    ["@type"]=>
238
    string(10) "JobPosting"
239
    ["title"]=>
240
    string(40) "Auszubildende/r Fachinformatiker AE / SI"
241
    ["description"]=>
242
    string(192) "<p>test</p><h1>Auszubildende/r Fachinformatiker AE / SI</h1><h3>Requirements</h3><p><p>gd fgdfg dfg dfg df</p></p><h3>Qualifications</h3><p></p><h3>Benefits</h3><p><p>s gdf gdfg df gdf</p></p>"
243
    ["datePosted"]=>
244
    string(19) "2020-06-12 18:59:45"
245
    ["identifier"]=>
246
    array(3) {
247
      ["@type"]=>
248
      string(13) "PropertyValue"
249
      ["value"]=>
250
      string(24) "5c8115410acec3d470a03e41"
251
      ["name"]=>
252
      string(8) "Firma XY"
253
    }
254
    ["hiringOrganization"]=>
255
    array(3) {
256
      ["@type"]=>
257
      string(12) "Organization"
258
      ["name"]=>
259
      string(8) "Firma XY"
260
      ["logo"]=>
261
      string(72) "/file/Organizations.OrganizationImage/58f8e3124e197f5d78e3fed7/YAWIK.jpg"
262
    }
263
    ["jobLocation"]=>
264
    array(1) {
265
      [0]=>
266
      array(2) {
267
        ["@type"]=>
268
        string(5) "Place"
269
        ["address"]=>
270
        array(6) {
271
          ["@type"]=>
272
          string(13) "PostalAddress"
273
          ["streetAddress"]=>
274
          string(1) " "
275
          ["postalCode"]=>
276
          NULL
277
          ["addressLocality"]=>
278
          string(6) "Vechta"
279
          ["addressCountry"]=>
280
          NULL
281
          ["addressRegion"]=>
282
          string(13) "Niedersachsen"
283
        }
284
      }
285
    }
286
    ["employmentType"]=>
287
    array(1) {
288
      [0]=>
289
      string(9) "permanent"
290
    }
291
    ["validThrough"]=>
292
    string(19) "2020-12-09 18:59:45"
293
  }