Completed
Push — develop ( 344496...9659b8 )
by Carsten
35:19
created

JsonLdProvider::toJsonLd()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.9688
c 0
b 0
f 0
cc 5
nc 16
nop 0
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
    public function __construct(JobInterface $job)
42
    {
43
        $this->job = $job;
44
    }
45
46
47
    public function toJsonLd()
48
    {
49
        $organization = $this->job->getOrganization();
50
        $organizationName = $organization ? $organization->getOrganizationName()->getName() : $this->job->getCompany();
51
52
        $dateStart = $this->job->getDatePublishStart();
53
        $dateStart = $dateStart ? $dateStart->format('Y-m-d H:i:s') : null;
54
	    $dateEnd = $this->job->getDatePublishEnd();
55
        $dateEnd = $dateEnd ? $dateEnd->format('Y-m-d H:i:s') : null;
56
        if (!$dateEnd){
0 ignored issues
show
Bug Best Practice introduced by
The expression $dateEnd of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57
            $dateEnd = new \DateTime($dateStart);
58
            $dateEnd->add(new \DateInterval("P180D"));
59
            $dateEnd = $dateEnd->format('Y-m-d H:i:s');
60
        }
61
62
        $array=[
63
            '@context'=>'http://schema.org/',
64
            '@type' => 'JobPosting',
65
            'title' => $this->job->getTitle(),
66
            'description' => $this->getDescription($this->job->getTemplateValues()),
67
            'datePosted' => $dateStart,
68
            'identifier' => [
69
                '@type' => 'PropertyValue',
70
                'value' => $this->job->getApplyId(),
71
                'name' => $organizationName,
72
            ],
73
            'hiringOrganization' => [
74
                '@type' => 'Organization',
75
                '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
            'jobLocation' => $this->getLocations($this->job->getLocations()),
81
            'employmentType' => $this->job->getClassifications()->getEmploymentTypes()->getValues(),
82
            'validThrough' => $dateEnd
83
        ];
84
85
        return Json::encode($array);
86
    }
87
88
    /**
89
     * Generates a location array
90
     *
91
     * @param Collection $locations,
0 ignored issues
show
Documentation introduced by
There is no parameter named $locations,. Did you maybe mean $locations?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
92
     *
93
     * @return array
94
     */
95
    private function getLocations($locations){
96
        $array=[];
97
        foreach($locations as $location){ /* @var \Core\Entity\LocationInterface $location */
98
            array_push(
99
                $array,
100
                [
101
                    '@type' => 'Place',
102
                    'address' => [
103
                        '@type' => 'PostalAddress',
104
                        'streetAddress' => $location->getStreetname() .' '.$location->getStreetnumber(),
105
                        'postalCode' => $location->getPostalCode(),
106
                        'addressLocality' => $location->getCity(),
107
                        'addressCountry' => $location->getCountry(),
108
                        'addressRegion' => $location->getRegion(),
109
                    ]
110
                ]);
111
        }
112
        return $array;
113
    }
114
115
    /**
116
     * Generates a description from template values
117
     *
118
     * @param TemplateValuesInterface $values
119
     *
120
     * @return string
121
     */
122
    private function getDescription(TemplateValuesInterface $values) {
123
124
        $description=sprintf(
125
            "<p>%s</p>".
126
            "<h1>%s</h1>".
127
            "<h3>Requirements</h3><p>%s</p>".
128
            "<h3>Qualifications</h3><p>%s</p>".
129
            "<h3>Benefits</h3><p>%s</p>",
130
            $values->getDescription(),
131
            $values->getTitle(),
132
            $values->getRequirements(),
133
            $values->getQualifications(),
134
            $values->getBenefits()
135
        );
136
        return $description;
137
    }
138
}
139