Passed
Push — develop ( 24b5e4...fa8d1c )
by Carsten
07:00
created

JsonLdProvider::getLocations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
c 0
b 0
f 0
ccs 13
cts 13
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  2013 - 2017 Cross Solution <http://cross-solution.de>
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 Zend\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
    /**
39
     * @param JobInterface $job
40
     */
41 5
    public function __construct(JobInterface $job)
42
    {
43 5
        $this->job = $job;
44 5
    }
45
46
47 4
    public function toJsonLd()
48
    {
49 4
        $organization = $this->job->getOrganization();
50 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...
51
52 4
        $dateStart = $this->job->getDatePublishStart();
53 4
        $dateStart = $dateStart ? $dateStart->format('Y-m-d H:i:s') : null;
54 4
        $dateEnd = $this->job->getDatePublishEnd();
55 4
        $dateEnd = $dateEnd ? $dateEnd->format('Y-m-d H:i:s') : null;
56 4
        if (!$dateEnd) {
57 4
            $dateEnd = new \DateTime($dateStart);
58 4
            $dateEnd->add(new \DateInterval("P180D"));
59 4
            $dateEnd = $dateEnd->format('Y-m-d H:i:s');
60
        }
61
        $array=[
62 4
            '@context'=>'http://schema.org/',
63 4
            '@type' => 'JobPosting',
64 4
            'title' => $this->job->getTitle(),
65 4
            'description' => $this->getDescription($this->job->getTemplateValues()),
66 4
            'datePosted' => $dateStart,
67
            'identifier' => [
68 4
                '@type' => 'PropertyValue',
69 4
                'value' => $this->job->getApplyId(),
70 4
                'name' => $organizationName,
71
            ],
72
            'hiringOrganization' => [
73 4
                '@type' => 'Organization',
74 4
                'name' => $organizationName,
75
                // @TODO add the link to the Logo of the company
76
                // https://developers.google.com/search/docs/data-types/logo
77
                // 'logo' => $this->job->getOrganization()->getImage()->getUri(),
78
            ],
79 4
            'jobLocation' => $this->getLocations($this->job->getLocations()),
80 4
            'employmentType' => $this->job->getClassifications()->getEmploymentTypes()->getValues(),
81 4
            'validThrough' => $dateEnd
82
        ];
83
84 4
        $array += $this->generateSalary();
85
86 4
        return Json::encode($array);
87
    }
88
89
    /**
90
     * Generates a location array
91
     *
92
     * @param Collection $locations,
93
     *
94
     * @return array
95
     */
96 4
    private function getLocations($locations)
97
    {
98 4
        $array=[];
99 4
        foreach ($locations as $location) { /* @var \Core\Entity\LocationInterface $location */
100 4
            array_push(
101 4
                $array,
102
                [
103 4
                    '@type' => 'Place',
104
                    'address' => [
105 4
                        '@type' => 'PostalAddress',
106 4
                        'streetAddress' => $location->getStreetname() .' '.$location->getStreetnumber(),
107 4
                        'postalCode' => $location->getPostalCode(),
108 4
                        'addressLocality' => $location->getCity(),
109 4
                        'addressCountry' => $location->getCountry(),
110 4
                        'addressRegion' => $location->getRegion(),
111
                    ]
112
                ]
113
            );
114
        }
115 4
        return $array;
116
    }
117
118
    /**
119
     * Generates a description from template values
120
     *
121
     * @param TemplateValuesInterface $values
122
     *
123
     * @return string
124
     */
125 4
    private function getDescription(TemplateValuesInterface $values)
126
    {
127 4
        $description=sprintf(
128
            "<p>%s</p>".
129
            "<h1>%s</h1>".
130
            "<h3>Requirements</h3><p>%s</p>".
131
            "<h3>Qualifications</h3><p>%s</p>".
132 4
            "<h3>Benefits</h3><p>%s</p>",
133 4
            $values->getDescription(),
134 4
            $values->getTitle(),
135 4
            $values->getRequirements(),
136 4
            $values->getQualifications(),
137 4
            $values->getBenefits()
138
        );
139 4
        return $description;
140
    }
141
142 4
    private function generateSalary()
143
    {
144 4
        $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

144
        /** @scrutinizer ignore-call */ 
145
        $salary = $this->job->getSalary();
Loading history...
145
146 4
        if (!$salary || null === $salary->getValue()) {
0 ignored issues
show
introduced by
$salary is of type Jobs\Entity\Salary, thus it always evaluated to true.
Loading history...
introduced by
The condition null === $salary->getValue() is always false.
Loading history...
147 3
            return [];
148
        }
149
150
        return [
151
            'baseSalary' => [
152 1
                '@type' => 'MonetaryAmount',
153 1
                'currency' => $salary->getCurrency(),
154
                'value' => [
155 1
                    '@type' => 'QuantitiveValue',
156 1
                    'value' => $salary->getValue(),
157 1
                    'unitText' => $salary->getUnit()
158
                ],
159
            ],
160
        ];
161
    }
162
}
163