DomainParserExtension::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace EmanueleMinotto\DomainParserBundle\Twig;
4
5
use Pdp\Parser;
6
use Twig_Extension;
7
use Twig_SimpleFunction;
8
use Twig_SimpleTest;
9
10
class DomainParserExtension extends Twig_Extension
11
{
12
    /**
13
     * Pdp Parser.
14
     *
15
     * @var Parser
16
     */
17
    private $parser;
18
19
    /**
20
     * Contructor used for the parser dependency.
21
     *
22
     * @param Parser $parser
23
     */
24 9
    public function __construct(Parser $parser)
25
    {
26 9
        $this->parser = $parser;
27 9
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 9
    public function getFunctions()
33
    {
34
        return [
35
            new Twig_SimpleFunction('parse_url', function ($url) {
36 3
                return $this->parser
37 3
                    ->parseUrl($url)
38 3
                    ->toArray();
39 9
            }),
40 9
            new Twig_SimpleFunction('parse_host', function ($host) {
41 3
                return $this->parser
42 3
                    ->parseHost($host)
43 3
                    ->toArray();
44 9
            }),
45 9
        ];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 9
    public function getTests()
52
    {
53
        return [
54 9
            new Twig_SimpleTest(
55 9
                'valid suffix',
56 9
                [$this->parser, 'isSuffixValid']
57 9
            ),
58 9
        ];
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 9
    public function getName()
65
    {
66 9
        return 'domain_parser_extension';
67
    }
68
}
69