Completed
Push — develop ( e4c992...b72595 )
by
unknown
06:34
created

JobUrl::__invoke()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 53
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
rs 7.1199
cc 8
eloc 36
nc 12
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author    [email protected]
9
 */
10
11
namespace Jobs\View\Helper;
12
13
use Zend\View\Helper\AbstractHelper;
14
use Jobs\Entity\JobInterface as Job;
15
16
/**
17
 * View helper to assemble the link to a job opening
18
 * @method \Core\View\Helper\Params paramsHelper()
19
 *
20
 * @author Carsten Bleek <[email protected]>
21
 * @todo   write test
22
 */
23
class JobUrl extends AbstractHelper
24
{
25
    /**
26
     * Default options
27
     *
28
     * @var array
29
     */
30
    protected $options = [
31
        'absolute' => false,
32
        'linkOnly' => false,
33
        'target' => false,
34
        'rel' => 'nofollow',
35
        'showPendingJobs' => false
36
    ];
37
38
    protected $urlHelper;
39
    protected $paramsHelper;
40
    protected $serverUrlHelper;
41
42
    public function setUrlHelper($helper)
43
    {
44
        $this->urlHelper = $helper;
45
        return $this;
46
    }
47
48
    public function setParamsHelper($helper)
49
    {
50
        $this->paramsHelper = $helper;
51
        return $this;
52
    }
53
54
    public function setServerUrlHelper($helper)
55
    {
56
        $this->serverUrlHelper = $helper;
57
        return $this;
58
    }
59
60
    public function __invoke(Job $jobEntity, $options = [])
61
    {
62
63
        $options= array_merge($this->options, $options);
64
        $paramsHelper = $this->paramsHelper;
65
        $urlHelper = $this->urlHelper;
66
        $serverUrlHelper = $this->serverUrlHelper;
67
        $isExternalLink = false;
68
69
        if (!empty($jobEntity->getLink())) {
70
            $url = $jobEntity->getLink();
71
            $isExternalLink = true;
72
        }elseif($options['showPendingJobs']) {
73
            $url = $urlHelper(
74
                'lang/jobs/approval',
75
                [],
76
                [
77
                    'query' => [
78
                        'id' => $jobEntity->getId()
79
                    ]
80
                ], true);
81
82
        }else{
83
84
            $query = [
85
                'subscriberUri' => $serverUrlHelper([]) . '/subscriber/' . 1,
86
                'id' => $jobEntity->getId()
87
            ];
88
            $route = 'lang/jobs/view';
89
            $params = [
90
                'lang' => $paramsHelper('lang'),
91
            ];
92
            if ($paramsHelper('channel')) {
93
                $params['channel'] = $paramsHelper('channel');
94
            }
95
            $url = $urlHelper($route, $params, array('query' => $query));
96
        }
97
98
        if ($options['linkOnly']){
99
            $result = $url;
100
            if ($options['absolute'] && !$isExternalLink){
101
                $result = $serverUrlHelper($url);
102
            }
103
        }else{
104
            $result = sprintf('<a href="%s" rel="%s" %s>%s</a>',
105
                              $url,
106
                              $options['rel'],
107
                              $options['target']?"target=" . $options['target']:"",
108
                              strip_tags($jobEntity->getTitle()));
109
        }
110
111
        return $result;
112
    }
113
114
    /**
115
     * @param $options
116
     */
117 View Code Duplication
    public function setOptions($options){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
        foreach($options as $key=>$val) {
119
            if (array_key_exists($this->options,$key)) {
120
                $this->options[$key]=$val;
121
            }
122
        }
123
    }
124
}
125