Completed
Push — master ( 71a600...f9363f )
by Mathias
22s queued 15s
created

JsonLdProvider::getDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 24
ccs 0
cts 0
cp 0
rs 9.6333
cc 1
nc 1
nop 0
crap 2
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();
0 ignored issues
show
introduced by
$organization is of type Organizations\Entity\OrganizationInterface, thus it always evaluated to true.
Loading history...
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
        $array = array_replace_recursive($this->getDefault(), $default, $array);
99
100
        return Json::encode($array);
101
    }
102
103
    /**
104 4
     * try to get the logo of an organization. Fallback: logoRef of job posting
105
     */
106 4
    private function getLogo() {
107 4
        $organization = $this->job->getOrganization();
108 4
109 4
        $organizationLogo = ($organization && $organization->getImage())? $organization->getImage()->getUri() : $this->job->getLogoRef();
0 ignored issues
show
Bug introduced by
The method getLogoRef() does not exist on Jobs\Entity\JobInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jobs\Entity\JobInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        $organizationLogo = ($organization && $organization->getImage())? $organization->getImage()->getUri() : $this->job->/** @scrutinizer ignore-call */ getLogoRef();
Loading history...
110
        return $organizationLogo;
111 4
    }
112
113 4
    /**
114 4
     * Generates a location array
115 4
     *
116 4
     * @param Collection $locations,
117 4
     *
118 4
     * @return array
119
     */
120
    private function getLocations($locations)
121
    {
122
        $array=[];
123 4
        foreach ($locations as $location) { /* @var \Core\Entity\LocationInterface $location */
124
            array_push(
125
                $array,
126
                [
127
                    '@type' => 'Place',
128
                    'address' => [
129
                        '@type' => 'PostalAddress',
130
                        'streetAddress' => $location->getStreetname() .' '.$location->getStreetnumber(),
131
                        'postalCode' => $location->getPostalCode(),
132
                        'addressLocality' => $location->getCity(),
133 4
                        'addressCountry' => $location->getCountry(),
134
                        'addressRegion' => $location->getRegion(),
135 4
                    ]
136
                ]
137 4
            );
138
        }
139
        return $array;
140 4
    }
141
142
    /**
143
     * Generates a description from template values
144
     *
145 4
     * @param TemplateValuesInterface $values
146 4
     *
147 4
     * @return string
148 4
     */
149 4
    private function getDescription(TemplateValuesInterface $values)
150 4
    {
151
        $html = $values->getHtml();
0 ignored issues
show
Bug introduced by
The method getHtml() does not exist on Jobs\Entity\TemplateValuesInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jobs\Entity\TemplateValuesInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
        /** @scrutinizer ignore-call */ 
152
        $html = $values->getHtml();
Loading history...
152
153
        if ($html) {
154 4
            $description=sprintf("%s", $values->getHtml() );
155
        } else {
156
            $description=sprintf(
157 4
                "<p>%s</p>".
158
                "<h1>%s</h1>".
159 4
                "<h3>Requirements</h3><p>%s</p>".
160
                "<h3>Qualifications</h3><p>%s</p>".
161 4
                "<h3>Benefits</h3><p>%s</p>",
162 3
                $values->getDescription(),
163
                $values->getTitle(),
164
                $values->getRequirements(),
165
                $values->getQualifications(),
166
                $values->getBenefits()
167 1
            );
168 1
        }
169
170 1
        return $description;
171 1
    }
172 1
173
    private function generateSalary()
174
    {
175
        $salary = $this->job->getSalary();
0 ignored issues
show
Bug introduced by
The method getSalary() does not exist on Jobs\Entity\JobInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jobs\Entity\JobInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
        /** @scrutinizer ignore-call */ 
176
        $salary = $this->job->getSalary();
Loading history...
176
177
        if (!$salary || null === $salary->getValue()) {
0 ignored issues
show
introduced by
The condition null === $salary->getValue() is always false.
Loading history...
introduced by
$salary is of type Jobs\Entity\Salary, thus it always evaluated to true.
Loading history...
178
            return [];
179
        }
180
181
        return [
182
            'baseSalary' => [
183
                '@type' => 'MonetaryAmount',
184
                'currency' => $salary->getCurrency(),
185
                'value' => [
186
                    '@type' => 'QuantitiveValue',
187
                    'value' => $salary->getValue(),
188
                    'unitText' => $salary->getUnit()
189
                ],
190
            ],
191
        ];
192
    }
193
194
    private function getDefault(){
195
        return [
196
            '@context'=>'http://schema.org/',
197
            '@type' => 'JobPosting',
198
            'identifier' => [
199
                '@type' => 'PropertyValue',
200
            ],
201
            'hiringOrganization' => [
202
                '@type' => 'Organization',
203
            ],
204
            'jobLocation' => [
205
                '@type' => 'Place',
206
                'address' => [
207
                    '@type' => 'PostalAddress',
208
                    'addressCountry' => 'DE',
209
                ]
210
            ],
211
            'baseSalary' => [
212
                '@type' => 'MonetaryAmount',
213
                'currency' => 'EUR',
214
                'value' => [
215
                    '@type' => 'QuantitiveValue',
216
                    'value' => 'node',
217
                    'unitText' => 'YEAR'
218
                ]
219
            ]
220
                ];
221
    }
222
}
223