Passed
Push — master ( 52ecda...402e55 )
by Juan
09:00
created

NativeQueryBuilder::findFromSqlKey()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 25
rs 9.7998
1
<?php
2
3
namespace Micayael\NativeQueryFromFileBuilderBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\NonUniqueResultException;
7
use Doctrine\ORM\NoResultException;
8
use Doctrine\ORM\Query\ResultSetMappingBuilder;
9
use Micayael\NativeQueryFromFileBuilderBundle\Helper\NativeQueryBuilderHelper;
10
use Symfony\Component\Cache\Adapter\AdapterInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
class NativeQueryBuilder implements NativeQueryBuilderInterface
14
{
15
    /**
16
     * @var EntityManagerInterface
17
     */
18
    private $em;
19
20
    /**
21
     * @var null|EventDispatcherInterface
22
     */
23
    private $eventDispatcher;
24
25
    /**
26
     * @var null|AdapterInterface
27
     */
28
    private $cache;
29
30
    private $helper;
31
32
    public function __construct(EntityManagerInterface $em, ?EventDispatcherInterface $eventDispatcher, ?AdapterInterface $cache, array $config)
33
    {
34
        $this->em = $em;
35
        $this->eventDispatcher = $eventDispatcher;
36
        $this->cache = $cache;
37
38
        $this->helper = new NativeQueryBuilderHelper($this->eventDispatcher, $this->cache, $config);
39
    }
40
41
    public function findOneFromSqlKey(string $key, array $params = [], ResultSetMappingBuilder $rsm = null)
42
    {
43
        try {
44
            $sql = $this->helper->getSqlFromYamlKey($key, $params);
45
46
            $ret = [];
47
48
            if ($rsm) {
49
                $nativeQuery = $this->em
50
                    ->createNativeQuery($sql, $rsm);
51
52
                foreach ($params as $key => $value) {
53
                    $nativeQuery->setParameter($key, $value);
54
                }
55
56
                $ret[] = $nativeQuery->getSingleResult();
57
            } else {
58
                $ret = $this->em
59
                    ->getConnection()
60
                    ->fetchAll($sql, $params);
61
            }
62
63
            if (empty($ret)) {
64
                return null;
65
            }
66
67
            if (count($ret) > 1) {
68
                throw new NonUniqueResultException(sprintf('Se han encontrado %d filas', count($ret)));
69
            }
70
71
            return $ret[0];
72
        } catch (NoResultException $e) {
73
            return null;
74
        }
75
    }
76
77
    public function findFromSqlKey(string $key, array $params = [], ?string $orderBy, ResultSetMappingBuilder $rsm = null): array
78
    {
79
        if ($orderBy) {
80
            $params['orderby'] = $orderBy;
81
        }
82
83
        $sql = $this->helper->getSqlFromYamlKey($key, $params);
84
85
        if ($rsm) {
86
            $nativeQuery = $this->em
87
                ->createNativeQuery($sql, $rsm)
88
            ;
89
90
            foreach ($params as $key => $value) {
91
                $nativeQuery->setParameter($key, $value);
92
            }
93
94
            $ret = $nativeQuery->getResult();
95
        } else {
96
            $ret = $this->em
97
                ->getConnection()
98
                ->fetchAll($sql, $params);
99
        }
100
101
        return $ret;
102
    }
103
104
    public function findScalarFromSqlKey(string $key, array $params = [])
105
    {
106
        $sql = $this->helper->getSqlFromYamlKey($key, $params);
107
108
        $ret = $this->em
109
            ->getConnection()
110
            ->fetchColumn($sql, $params);
111
112
        return $ret;
113
    }
114
}
115