Passed
Push — master ( 169181...4782ef )
by Rougin
03:08
created

Result   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 7
eloc 17
dl 0
loc 77
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A manager() 0 3 1
A execute() 0 24 4
A mapping() 0 5 1
A __construct() 0 3 1
1
<?php
2
3
namespace Rougin\Windstorm\Doctrine;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\NativeQuery;
7
use Doctrine\ORM\Query\ResultSetMapping;
8
use Rougin\Windstorm\ResultInterface;
9
10
/**
11
 * Result
12
 *
13
 * @package Windstorm
14
 * @author  Rougin Gutib <[email protected]>
15
 */
16
class Result implements ResultInterface
17
{
18
    /**
19
     * @var \Doctrine\ORM\EntityManager
20
     */
21
    protected $manager;
22
23
    /**
24
     * @var \Doctrine\ORM\Query\ResultSetMapping|null
25
     */
26
    protected $mapping = null;
27
28
    /**
29
     * Initializes the entity manager instance.
30
     *
31
     * @param \Doctrine\ORM\EntityManager $manager
32
     */
33 6
    public function __construct(EntityManager $manager)
34
    {
35 6
        $this->manager = $manager;
36 6
    }
37
38
    /**
39
     * Returns the entity manager instance.
40
     *
41
     * @return \Doctrine\ORM\EntityManager
42
     */
43 3
    public function manager()
44
    {
45 3
        return $this->manager;
46
    }
47
48
    /**
49
     * Executes an SQL statement with its bindings and types.
50
     *
51
     * @param  string $sql
52
     * @param  array  $bindings
53
     * @param  array  $types
54
     * @return mixed
55
     */
56 6
    public function execute($sql, array $bindings, array $types)
57
    {
58 6
        $connection = $this->manager->getConnection();
59
60 6
        if (strpos($sql, 'SELECT') === false)
61 6
        {
62
            return $connection->executeUpdate($sql, $bindings, $types);
63
        }
64
65 6
        if ($this->mapping === null)
66 6
        {
67 3
            return $connection->executeQuery($sql, $bindings, $types);
68
        }
69
70 3
        $query = new NativeQuery($this->manager);
71
72 3
        $query->setSql($sql)->setResultSetMapping($this->mapping);
73
74 3
        foreach ($bindings as $key => $binding)
75
        {
76 3
            $query->setParameter($key, $binding, $types[$key]);
77 3
        }
78
79 3
        return (array) $query->getResult();
80
    }
81
82
    /**
83
     * Sets the result set mapping instance.
84
     *
85
     * @param  \Doctrine\ORM\Query\ResultSetMapping $mapping
86
     * @return self
87
     */
88 3
    public function mapping(ResultSetMapping $mapping)
89
    {
90 3
        $this->mapping = $mapping;
91
92 3
        return $this;
93
    }
94
}
95