Passed
Push — develop ( 28c299...928e2a )
by Mathias
12:40
created

JsonLdProvider::getRate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

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

148
        /** @scrutinizer ignore-call */ 
149
        $salary = $this->job->getSalary();
Loading history...
149
150 2
        return ($salary && !is_null($salary->getValue())) ?
151
            ($salary->getValue() . ' ' . $salary->getCurrency() . '/' . $salary->getUnit()) :
152 2
            /*@translate*/ 'Undefined';
153
    }
154
}
155