Completed
Push — develop ( d3d9b0...38be37 )
by
unknown
16:43
created

JsonLdProvider::toJsonLd()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 18
nc 1
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
        $organizationName = $this->job->getOrganization()->getOrganizationName()->getName();
50
51
        $array=[
52
            '@context'=>'http://schema.org/',
53
            '@type' => 'JobPosting',
54
            'title' => $this->job->getTitle(),
55
            'description' => $this->getDescription($this->job->getTemplateValues()),
56
            'datePosted' => $this->job->getDatePublishStart()->format('Y-m-d'),
57
            'identifier' => [
58
                '@type' => 'PropertyValue',
59
                'value' => $this->job->getApplyId(),
60
                'name' => $organizationName,
61
            ],
62
            'hiringOrganization' => [
63
                '@type' => 'Organization',
64
                'name' => $organizationName,
65
                // @TODO add the link to the Logo of the company
66
                // https://developers.google.com/search/docs/data-types/logo
67
                // 'logo' => $this->job->getOrganization()->getImage()->getUri(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
            ],
69
            'jobLocation' => $this->getLocations($this->job->getLocations()),
0 ignored issues
show
Documentation introduced by
$this->job->getLocations() is of type string, but the function expects a object<Doctrine\Common\Collections\Collection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
            'employmentType' => $this->job->getClassifications()->getEmploymentTypes()->getValues()
71
        ];
72
73
        return Json::encode($array);
74
    }
75
76
    /**
77
     * Generates a location array
78
     *
79
     * @param Collection $locations
80
     *
81
     * @return array
82
     */
83
    private function getLocations($locations){
84
        $array=[];
85
        foreach($locations as $location){ /* @var \Core\Entity\LocationInterface $location */
86
            array_push(
87
                $array,
88
                [
89
                    '@type' => 'Place',
90
                    'address' => [
91
                        '@type' => 'PostalAddress',
92
                        'postalCode' => $location->getPostalCode(),
93
                        'addressLocality' => $location->getCity(),
94
                        'addressCountry' => $location->getCountry(),
95
                        'addressRegion' => $location->getRegion(),
96
                    ]
97
                ]);
98
        }
99
        return $array;
100
    }
101
102
    /**
103
     * Generates a description from template values
104
     *
105
     * @param TemplateValuesInterface $values
106
     *
107
     * @return string
108
     */
109
    private function getDescription(TemplateValuesInterface $values) {
110
111
        $description=sprintf(
112
            "<p>%s</p>".
113
            "<h1>%s</h1>".
114
            "<h3>Requirements</h3><p>%s</p>".
115
            "<h3>Qualifications</h3><p>%s</p>".
116
            "<h3>Benefits</h3><p>%s</p>",
117
            $values->getDescription(),
118
            $values->getTitle(),
119
            $values->getRequirements(),
120
            $values->getQualifications(),
121
            $values->getBenefits()
122
        );
123
        return $description;
124
    }
125
}