Completed
Push — master ( ead158...0c77a2 )
by Siim
12:04
created

AbstractRepository::findOneOrNullData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 21.01.19
6
 * Time: 8:26
7
 */
8
9
namespace Sf4\Api\Repository;
10
11
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
12
use Doctrine\DBAL\DBALException;
13
use Doctrine\DBAL\Driver\Statement;
14
15
abstract class AbstractRepository extends ServiceEntityRepository
16
{
17
    const DB_NAME = null;
18
19
    /**
20
     * @param $id
21
     * @return array|null
22
     * @throws \Exception
23
     */
24
    public function findDataById($id): ?array
25
    {
26
        $this->throwExceptionWhenDbNameIsNull();
27
28
        return $this->findOneOrNullData(
29
            'SELECT * FROM ' . static::DB_NAME . ' WHERE id = ?',
30
            [
31
                $id
32
            ]
33
        );
34
    }
35
36
    /**
37
     * @param string $sql
38
     * @param array $params
39
     * @return array|null
40
     */
41
    public function findOneOrNullData(string $sql, array $params = []): ?array
42
    {
43
        $statement = $this->getStatement($sql, $params);
44
        if ($statement) {
0 ignored issues
show
introduced by
$statement is of type Doctrine\DBAL\Statement, thus it always evaluated to true.
Loading history...
45
            $data = $statement->fetch();
46
            if ($data !== false) {
47
                return $data;
48
            }
49
        }
50
51
        return null;
52
    }
53
54
    /**
55
     * @param string $sql
56
     * @param array $params
57
     * @return Statement|null
58
     */
59
    protected function getStatement(string $sql, array $params = []): ?Statement
60
    {
61
        try {
62
            $params = array_values($params);
63
            $connection = $this->getEntityManager()->getConnection();
64
            $statement = $connection->prepare($sql);
65
            $statement->execute($params);
66
            return $statement;
67
        } catch (DBALException $e) {
68
            return null;
69
        }
70
    }
71
72
    public function findDataResults(string $sql, array $params = []): array
73
    {
74
        $statement = $this->getStatement($sql, $params);
75
        if ($statement) {
0 ignored issues
show
introduced by
$statement is of type Doctrine\DBAL\Statement, thus it always evaluated to true.
Loading history...
76
            return $statement->fetchAll();
77
        }
78
        return [];
79
    }
80
81
    /**
82
     * @param string $id
83
     * @return mixed|null
84
     */
85
    public function getEntityById(string $id)
86
    {
87
        return $this->getEntityBy('id', $id);
88
    }
89
90
    /**
91
     * @throws \Exception
92
     */
93
    protected function throwExceptionWhenDbNameIsNull()
94
    {
95
        if (static::DB_NAME === null) {
0 ignored issues
show
introduced by
The condition static::DB_NAME === null is always true.
Loading history...
96
            throw new \Exception('Invalid repository');
97
        }
98
    }
99
100
    /**
101
     * @param string $field
102
     * @param string $value
103
     * @return mixed|null
104
     */
105
    protected function getEntityBy(string $field, string $value)
106
    {
107
        if (empty($value)) {
108
            return null;
109
        }
110
111
        $queryBuilder = $this->createQueryBuilder('main');
112
        $queryBuilder->where(
113
            $queryBuilder->expr()->eq('main.' . $field, ':parameter')
114
        );
115
        $queryBuilder->setParameter(':parameter', $value);
116
117
        try {
118
            return $queryBuilder->getQuery()->getOneOrNullResult();
119
        } catch (\Exception $exception) {
120
            return null;
121
        }
122
    }
123
124
    /**
125
     * @param string $id
126
     * @return mixed|null
127
     */
128
    public function getEntityByUuid(string $id)
129
    {
130
        return $this->getEntityBy('uuid', $id);
131
    }
132
}
133