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

JobUrl   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 6.86 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 2
dl 7
loc 102
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrlHelper() 0 5 1
A setParamsHelper() 0 5 1
A setServerUrlHelper() 0 5 1
B __invoke() 0 53 8
A setOptions() 7 7 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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