Completed
Push — master ( 4c14c6...4ad177 )
by Mathias
25s queued 15s
created

JsonLdProvider::getLocations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 20
ccs 5
cts 5
cp 1
rs 9.8333
cc 2
nc 2
nop 1
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()
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
        return Json::encode($array);
99
    }
100
101
    /**
102
     * try to get the logo of an organization. Fallback: logoRef of job posting
103
     */
104 4
    private function getLogo() {
105
        $organization = $this->job->getOrganization();
106 4
107 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

107
        $organizationLogo = ($organization && $organization->getImage())? $organization->getImage()->getUri() : $this->job->/** @scrutinizer ignore-call */ getLogoRef();
Loading history...
108 4
        return $organizationLogo;
109 4
    }
110
111 4
    /**
112
     * Generates a location array
113 4
     *
114 4
     * @param Collection $locations,
115 4
     *
116 4
     * @return array
117 4
     */
118 4
    private function getLocations($locations)
119
    {
120
        $array=[];
121
        foreach ($locations as $location) { /* @var \Core\Entity\LocationInterface $location */
122
            array_push(
123 4
                $array,
124
                [
125
                    '@type' => 'Place',
126
                    'address' => [
127
                        '@type' => 'PostalAddress',
128
                        'streetAddress' => $location->getStreetname() .' '.$location->getStreetnumber(),
129
                        'postalCode' => $location->getPostalCode(),
130
                        'addressLocality' => $location->getCity(),
131
                        'addressCountry' => $location->getCountry(),
132
                        'addressRegion' => $location->getRegion(),
133 4
                    ]
134
                ]
135 4
            );
136
        }
137 4
        return $array;
138
    }
139
140 4
    /**
141
     * Generates a description from template values
142
     *
143
     * @param TemplateValuesInterface $values
144
     *
145 4
     * @return string
146 4
     */
147 4
    private function getDescription(TemplateValuesInterface $values)
148 4
    {
149 4
        $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

149
        /** @scrutinizer ignore-call */ 
150
        $html = $values->getHtml();
Loading history...
150 4
151
        if ($html) {
152
            $description=sprintf("%s", $values->getHtml() );
153
        } else {
154 4
            $description=sprintf(
155
                "<p>%s</p>".
156
                "<h1>%s</h1>".
157 4
                "<h3>Requirements</h3><p>%s</p>".
158
                "<h3>Qualifications</h3><p>%s</p>".
159 4
                "<h3>Benefits</h3><p>%s</p>",
160
                $values->getDescription(),
161 4
                $values->getTitle(),
162 3
                $values->getRequirements(),
163
                $values->getQualifications(),
164
                $values->getBenefits()
165
            );
166
        }
167 1
168 1
        return $description;
169
    }
170 1
171 1
    private function generateSalary()
172 1
    {
173
        $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

173
        /** @scrutinizer ignore-call */ 
174
        $salary = $this->job->getSalary();
Loading history...
174
175
        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...
176
            return [];
177
        }
178
179
        return [
180
            'baseSalary' => [
181
                '@type' => 'MonetaryAmount',
182
                'currency' => $salary->getCurrency(),
183
                'value' => [
184
                    '@type' => 'QuantitiveValue',
185
                    'value' => $salary->getValue(),
186
                    'unitText' => $salary->getUnit()
187
                ],
188
            ],
189
        ];
190
    }
191
}
192