Completed
Push — master ( 91bc8f...12870b )
by Nikola
05:50
created

TwigSqlSourceManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 79
wmc 11
lcom 1
cbo 5
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 12 4
A has() 0 4 1
B execute() 0 22 4
A registerExecutor() 0 4 1
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoader Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\Manager;
11
12
use RunOpenCode\Bundle\QueryResourcesLoader\Contract\ExceptionInterface;
13
use RunOpenCode\Bundle\QueryResourcesLoader\Contract\ExecutorInterface;
14
use RunOpenCode\Bundle\QueryResourcesLoader\Contract\ManagerInterface;
15
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\ExecutionException;
16
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\RuntimeException;
17
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\SourceNotFoundException;
18
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\SyntaxException;
19
20
/**
21
 * Class TwigSqlSourceManager
22
 *
23
 * @package RunOpenCode\Bundle\QueryResourcesLoader\Manager
24
 */
25
class TwigSqlSourceManager implements ManagerInterface
26
{
27
    /**
28
     * @var \Twig_Environment
29
     */
30
    protected $twig;
31
32
    /**
33
     * @var ExecutorInterface[]
34
     */
35
    protected $executors;
36
37
    public function __construct(\Twig_Environment $twig)
38
    {
39
        $this->twig = $twig;
40
        $this->executors = array();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function get($name, array $args = array())
47
    {
48
        try {
49
            return $this->twig->render($name, $args);
50
        } catch (\Twig_Error_Loader $e) {
0 ignored issues
show
Bug introduced by
The class Twig_Error_Loader does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
51
            throw new SourceNotFoundException(sprintf('Could not find query source: "%s".', $name), 0, $e);
52
        } catch (\Twig_Error_Syntax $e) {
0 ignored issues
show
Bug introduced by
The class Twig_Error_Syntax does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
53
            throw new SyntaxException(sprintf('Query source "%s" contains Twig syntax error and could not be compiled.', $name), 0, $e);
54
        } catch (\Exception $e) {
55
            throw new RuntimeException('Unknown exception occured', 0, $e);
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function has($name)
63
    {
64
        return $this->twig->getLoader()->exists($name);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function execute($name, array $args = array(), $executor = 'default')
71
    {
72
        if (!array_key_exists($executor, $this->executors)) {
73
            throw  new RuntimeException(sprintf('Requested executor "%s" does not exists.', $executor));
74
        }
75
76
        $executor = $this->executors[$executor];
77
78
        try {
79
            /**
80
             * @var ExecutorInterface $executor
81
             */
82
            return $executor->execute($this->get($name, $args), $args);
83
        } catch (\Exception $e) {
84
85
            if ($e instanceof ExceptionInterface) {
86
                throw $e;
87
            }
88
89
            throw new ExecutionException(sprintf('Query "%s" could not be executed.', $name), 0, $e);
90
        }
91
    }
92
93
    /**
94
     * Register query executor.
95
     *
96
     * @param ExecutorInterface $executor
97
     * @param string $name
98
     */
99
    public function registerExecutor(ExecutorInterface $executor, $name)
100
    {
101
        $this->executors[$name] = $executor;
102
    }
103
}
104