Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

AbstractSqlStatementRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 68
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 11 2
A compile() 0 13 3
1
<?php
2
3
/**
4
 * TechDivision\Import\Repositories\AbstractSqlStatementRepository
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Repositories;
22
23
/**
24
 * Abstract repository class for the SQL statements to use.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
abstract class AbstractSqlStatementRepository implements SqlStatementRepositoryInterface
33
{
34
35
    /**
36
     * The initializes SQL statements.
37
     *
38
     * @var array
39
     */
40
    private $preparedStatements = array();
41
42
    /**
43
     * The array with the compiler instances.
44
     *
45
     * @var \IteratorAggregate<\TechDivision\Import\Utils\SqlCompilerInterface>
46
     */
47
    private $compilers = array();
48
49
    /**
50
     * Initializes the SQL statement repository with the primary key and table prefix utility.
51
     *
52
     * @param \IteratorAggregate<\TechDivision\Import\Utils\SqlCompilerInterface> $compilers The array with the compiler instances
0 ignored issues
show
Documentation introduced by
The doc-type \IteratorAggregate<\Tech...s\SqlCompilerInterface> could not be parsed: Expected "|" or "end of type", but got "<" at position 18. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
53
     */
54 2
    public function __construct(\IteratorAggregate $compilers)
55
    {
56 2
        $this->compilers = $compilers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $compilers of type object<IteratorAggregate> is incompatible with the declared type array of property $compilers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57 2
    }
58
59
    /**
60
     * Returns the SQL statement with the passed ID.
61
     *
62
     * @param string $id The ID of the SQL statement to return
63
     *
64
     * @return string The SQL statement
65
     * @throws \Exception Is thrown, if the SQL statement with the passed key cannot be found
66
     */
67 2
    public function load($id)
68
    {
69
70
        // try to find the SQL statement with the passed key
71 2
        if (isset($this->preparedStatements[$id])) {
72 1
            return $this->preparedStatements[$id];
73
        }
74
75
        // throw an exception if NOT available
76 1
        throw new \Exception(sprintf('Can\'t find SQL statement with ID %s', $id));
77
    }
78
79
    /**
80
     * Compiles the passed SQL statements.
81
     *
82
     * @param array $statements The SQL statements to compile
83
     *
84
     * @return void
85
     */
86 2
    protected function compile(array $statements)
87
    {
88
89
        // iterate over all statements and compile them
90 2
        foreach ($statements as $key => $statement) {
91
            // compile the statement
92 2
            foreach ($this->compilers as $compiler) {
93 2
                $statement = $compiler->compile($statement);
94
            }
95
            // add the comiled statemnent to the list
96 2
            $this->preparedStatements[$key] = $statement;
97
        }
98 2
    }
99
}
100