Passed
Push — master ( a6772f...2df7c7 )
by Andrea
23:31 queued 19:36
created

VersioneExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
5
use Symfony\Component\Process\Process;
6
use Symfony\Component\Cache\Simple\FilesystemCache;
7
8
class VersioneExtension extends \Twig\Extension\AbstractExtension
9
{
10
    private $projectpath;
11
12 26
    public function __construct($projectpath)
13
    {
14 26
        $this->projectpath = $projectpath;
15 26
    }
16
17 1
    public function getFunctions()
18
    {
19
        return array(
20 1
            new \Twig_SimpleFunction('versione_tag_git', array($this, 'versioneTagGit', 'is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

20
            /** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('versione_tag_git', array($this, 'versioneTagGit', 'is_safe' => array('html'))),
Loading history...
21
        );
22
    }
23
24 12
    public function versioneTagGit()
25
    {
26 12
        if ($this->isWindows()) {
27
            return 0;
28
        }
29 12
        $cache = new FilesystemCache();
30 12
        if ($cache->has('git_tag')) {
31 12
            $version = $cache->get('git_tag');
32
        } else {
33 2
            $projectDir = $this->projectpath;
34 2
            $process = new Process(array('git', 'describe', '--tags'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35 2
            $process->setWorkingDirectory($projectDir);
36 2
            $process->setTimeout(60 * 100);
37 2
            $process->run();
38 2
            if ($process->isSuccessful()) {
39 2
                $out = explode(chr(10), $process->getOutput());
40
41 2
                $version = isset($out[0]) ? $out[0] : '0';
42 2
                $cache->set('git_tag', $version);
43
            } else {
44
                $version = '0';
45
            }
46
        }
47
48 12
        return $version;
49
    }
50
51 12
    private function isWindows()
52
    {
53 12
        if (PHP_OS == 'WINNT') {
54
            return true;
55
        } else {
56 12
            return false;
57
        }
58
    }
59
}
60