Passed
Push — master ( 2de0e2...b170a3 )
by Rougin
02:10
created

Execute   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 7
eloc 20
dl 0
loc 81
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 30 4
A manager() 0 3 1
A __construct() 0 3 1
A mapping() 0 5 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\ExecuteInterface;
9
use Rougin\Windstorm\QueryInterface;
10
11
/**
12
 * Execute
13
 *
14
 * @package Windstorm
15
 * @author  Rougin Gutib <[email protected]>
16
 */
17
class Execute implements ExecuteInterface
18
{
19
    /**
20
     * @var \Doctrine\ORM\EntityManager
21
     */
22
    protected $manager;
23
24
    /**
25
     * @var \Doctrine\ORM\Query\ResultSetMapping|null
26
     */
27
    protected $mapping = null;
28
29
    /**
30
     * Initializes the entity manager instance.
31
     *
32
     * @param \Doctrine\ORM\EntityManager $manager
33
     */
34 6
    public function __construct(EntityManager $manager)
35
    {
36 6
        $this->manager = $manager;
37 6
    }
38
39
    /**
40
     * Returns the entity manager instance.
41
     *
42
     * @return \Doctrine\ORM\EntityManager
43
     */
44 6
    public function manager()
45
    {
46 6
        return $this->manager;
47
    }
48
49
    /**
50
     * Executes an SQL statement with its bindings and types.
51
     *
52
     * @param  \Rougin\Windstorm\QueryInterface $query
53
     * @return mixed
54
     */
55 6
    public function execute(QueryInterface $query)
56
    {
57 6
        $bindings = $query->bindings();
58
59 6
        $sql = $query->sql();
60
61 6
        $types = (array) $query->types();
0 ignored issues
show
Bug introduced by
The method types() does not exist on Rougin\Windstorm\QueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rougin\Windstorm\QueryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        $types = (array) $query->/** @scrutinizer ignore-call */ types();
Loading history...
62
63 6
        $connection = $this->manager->getConnection();
64
65 6
        if (strpos($sql, 'SELECT') === false)
66 6
        {
67
            return new Result($connection->executeUpdate($sql, $bindings, $types));
68
        }
69
70 6
        if ($this->mapping === null)
71 6
        {
72 3
            return new Result($connection->executeQuery($sql, $bindings, $types));
73
        }
74
75 3
        $query = new NativeQuery($this->manager);
76
77 3
        $query->setSql($sql)->setResultSetMapping($this->mapping);
78
79 3
        foreach ($bindings as $key => $binding)
80
        {
81 3
            $query->setParameter($key, $binding, $types[$key]);
82 3
        }
83
84 3
        return new Result((array) $query->getResult());
85
    }
86
87
    /**
88
     * Sets the result set mapping instance.
89
     *
90
     * @param  \Doctrine\ORM\Query\ResultSetMapping $mapping
91
     * @return self
92
     */
93 3
    public function mapping(ResultSetMapping $mapping)
94
    {
95 3
        $this->mapping = $mapping;
96
97 3
        return $this;
98
    }
99
}
100