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

JsonLd::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
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\View\Helper;
12
13
use Jobs\Entity\Decorator\JsonLdProvider;
14
use Jobs\Entity\JobInterface;
15
use Jobs\Entity\JsonLdProviderInterface;
16
use Zend\View\Helper\AbstractHelper;
17
18
/**
19
 * Print the JSON-LD representation of a job.
20
 * 
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @todo write test 
23
 */
24
class JsonLd extends AbstractHelper
25
{
26
    /**
27
     *
28
     *
29
     * @var JobInterface
30
     */
31
    private $job;
32
33
    /**
34
     * Print the JSON-LD representation of a job.
35
     *
36
     * Wraps it in <script type="application/ld+json"> tag
37
     *
38
     * @param JsonLdProvider|null $job
39
     *
40
     * @return string
41
     */
42
    public function __invoke($job = null)
43
    {
44
        $job = $job ?: $this->job;
45
46
        if (null === $job) {
47
            return '';
48
        }
49
50
        $jsonLdProvider = new JsonLdProvider($job);
0 ignored issues
show
Bug introduced by
It seems like $job defined by $job ?: $this->job on line 44 can also be of type object<Jobs\Entity\Decorator\JsonLdProvider>; however, Jobs\Entity\Decorator\Js...Provider::__construct() does only seem to accept object<Jobs\Entity\JobInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
52
        return '<script type="application/ld+json">'
53
               . $jsonLdProvider->toJsonLd()
54
               . '</script>';
55
56
    }
57
58
    /**
59
     * Set the default job to use, if invoked without arguments.
60
     *
61
     * @param JobInterface $job
62
     *
63
     * @return self
64
     */
65
    public function setJob(JobInterface $job)
66
    {
67
        $this->job = $job;
68
69
        return $this;
70
    }
71
72
73
    
74
}