Passed
Push — master ( 9f6880...d5aff1 )
by Juan
09:22
created

NativeQueryBuilder::findScalarFromSqlKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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 EventDispatcherInterface|null
22
     */
23
    private $eventDispatcher;
24
25
    /**
26
     * @var AdapterInterface|null
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 changeEntityManager(EntityManagerInterface $em)
42
    {
43
        $this->em = $em;
44
    }
45
46
    public function findOneFromSqlKey(string $key, array $params = [], ResultSetMappingBuilder $rsm = null)
47
    {
48
        try {
49
            $sql = $this->helper->getSqlFromYamlKey($key, $params);
50
51
            $ret = [];
52
53
            if ($rsm) {
54
                $nativeQuery = $this->em
55
                    ->createNativeQuery($sql, $rsm);
56
57
                foreach ($params as $key => $value) {
58
                    $nativeQuery->setParameter($key, $value);
59
                }
60
61
                $ret[] = $nativeQuery->getSingleResult();
62
            } else {
63
                $ret = $this->em
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::fetchAll() has been deprecated: Use fetchAllAssociative() ( Ignorable by Annotation )

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

63
                $ret = /** @scrutinizer ignore-deprecated */ $this->em

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64
                    ->getConnection()
65
                    ->fetchAll($sql, $params);
66
            }
67
68
            if (empty($ret)) {
69
                return null;
70
            }
71
72
            if (count($ret) > 1) {
73
                throw new NonUniqueResultException(sprintf('Se han encontrado %d filas', count($ret)));
74
            }
75
76
            return $ret[0];
77
        } catch (NoResultException $e) {
78
            return null;
79
        }
80
    }
81
82
    public function findFromSqlKey(string $key, array $params = [], ?string $orderBy, ResultSetMappingBuilder $rsm = null): array
83
    {
84
        if ($orderBy) {
85
            $params['orderby'] = $orderBy;
86
        }
87
88
        $sql = $this->helper->getSqlFromYamlKey($key, $params);
89
90
        if ($rsm) {
91
            $nativeQuery = $this->em
92
                ->createNativeQuery($sql, $rsm)
93
            ;
94
95
            foreach ($params as $key => $value) {
96
                $nativeQuery->setParameter($key, $value);
97
            }
98
99
            $ret = $nativeQuery->getResult();
100
        } else {
101
            $ret = $this->em
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::fetchAll() has been deprecated: Use fetchAllAssociative() ( Ignorable by Annotation )

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

101
            $ret = /** @scrutinizer ignore-deprecated */ $this->em

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
102
                ->getConnection()
103
                ->fetchAll($sql, $params);
104
        }
105
106
        return $ret;
107
    }
108
109
    public function findScalarFromSqlKey(string $key, array $params = [])
110
    {
111
        $sql = $this->helper->getSqlFromYamlKey($key, $params);
112
113
        $ret = $this->em
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::fetchColumn() has been deprecated: Use fetchOne() instead. ( Ignorable by Annotation )

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

113
        $ret = /** @scrutinizer ignore-deprecated */ $this->em

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
114
            ->getConnection()
115
            ->fetchColumn($sql, $params);
116
117
        return $ret;
118
    }
119
}
120