JobQueueExtension::generatePath()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace JMS\JobQueueBundle\Twig;
4
5
class JobQueueExtension extends \Twig\Extension\AbstractExtension
6
{
7
    private $linkGenerators = array();
8
9
    public function __construct(array $generators = array())
10
    {
11
        $this->linkGenerators = $generators;
12
    }
13
14
    public function getTests()
15
    {
16
        return array(
17
            new \Twig\TwigTest('jms_job_queue_linkable', array($this, 'isLinkable'))
18
        );
19
    }
20
21
    public function getFunctions()
22
    {
23
        return array(
24
            new \Twig\TwigFunction('jms_job_queue_path', array($this, 'generatePath'), array('is_safe' => array('html' => true)))
25
        );
26
    }
27
28
    public function getFilters()
29
    {
30
        return array(
31
            new \Twig\TwigFilter('jms_job_queue_linkname', array($this, 'getLinkname')),
32
            new \Twig\TwigFilter('jms_job_queue_args', array($this, 'formatArgs'))
33
        );
34
    }
35
36
    public function formatArgs(array $args, $maxLength = 60)
37
    {
38
        $str = '';
39
        $first = true;
40
        foreach ($args as $arg) {
41
            $argLength = strlen($arg);
42
43
            if (!$first) {
44
                $str .= ' ';
45
            }
46
            $first = false;
47
48
            if (strlen($str) + $argLength > $maxLength) {
49
                $str .= substr($arg, 0, $maxLength - strlen($str) - 4) . '...';
50
                break;
51
            }
52
53
            $str .= escapeshellarg($arg);
54
        }
55
56
        return $str;
57
    }
58
59
    public function isLinkable($entity)
60
    {
61
        foreach ($this->linkGenerators as $generator) {
62
            if ($generator->supports($entity)) {
63
                return true;
64
            }
65
        }
66
67
        return false;
68
    }
69
70 View Code Duplication
    public function generatePath($entity)
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...
71
    {
72
        foreach ($this->linkGenerators as $generator) {
73
            if ($generator->supports($entity)) {
74
                return $generator->generate($entity);
75
            }
76
        }
77
78
        throw new \RuntimeException(sprintf('The entity "%s" has no link generator.', get_class($entity)));
79
    }
80
81 View Code Duplication
    public function getLinkname($entity)
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...
82
    {
83
        foreach ($this->linkGenerators as $generator) {
84
            if ($generator->supports($entity)) {
85
                return $generator->getLinkname($entity);
86
            }
87
        }
88
89
        throw new \RuntimeException(sprintf('The entity "%s" has no link generator.', get_class($entity)));
90
    }
91
92
    public function getName()
93
    {
94
        return 'jms_job_queue';
95
    }
96
}
97