Completed
Push — master ( f7f008...2d0f6c )
by
unknown
02:21
created

UqlExtensionContainer::getFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Netdudes\DataSourceryBundle\Extension;
4
5
use Netdudes\DataSourceryBundle\Extension\Exception\FunctionNotFoundException;
6
use Netdudes\DataSourceryBundle\Extension\Exception\InvalidExtensionTypeException;
7
8
class UqlExtensionContainer
9
{
10
    /**
11
     * @var UqlExtensionInterface[]
12
     */
13
    private $extensions = [];
14
15
    /**
16
     * @var UqlFunctionInterface[]
17
     */
18
    private $functions = [];
19
20
    /**
21
     * @return UqlExtensionInterface[]
22
     */
23
    public function getExtensions()
24
    {
25
        return $this->extensions;
26
    }
27
28
    /**
29
     * @param string $name
30
     *
31
     * @return UqlFunctionInterface
32
     * @throws FunctionNotFoundException
33
     */
34
    public function getFunction($name)
35
    {
36
        if (!(isset($this->getFunctions()[$name]))) {
37
            throw new FunctionNotFoundException("Could not find UQL function $name");
38
        }
39
40
        return $this->getFunctions()[$name];
41
    }
42
43
    /**
44
     * @return UqlFunctionInterface[]
45
     */
46
    public function getFunctions()
47
    {
48
        return $this->functions;
49
    }
50
51
    /**
52
     * Adds an extension to the container. This function is called during the compiler pass.
53
     *
54
     * @param UqlExtensionInterface $extension
55
     *
56
     * @throws InvalidExtensionTypeException
57
     */
58
    public function addExtension(UqlExtensionInterface $extension)
59
    {
60
        $this->extensions[] = $extension;
61
62
        foreach ($extension->getFunctions() as $function) {
63
            if (!($function instanceof UqlFunctionInterface)) {
64
                throw new InvalidExtensionTypeException("Function extensions must implement the UqlFunctionInterface");
65
            }
66
            $this->functions[$function->getName()] = $function;
67
        }
68
    }
69
}
70