Completed
Pull Request — development (#812)
by
unknown
04:52
created

CachesRepository   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 0 21 3
A fetchOneBy() 0 23 4
A fetchBy() 0 27 5
A getIdByWP() 0 22 3
A getEntityFromDatabaseArray() 0 14 1
1
<?php
2
3
namespace Oc\Repository;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\Entity\CachesEntity;
7
use Oc\Repository\Exception\RecordAlreadyExistsException;
8
use Oc\Repository\Exception\RecordNotFoundException;
9
use Oc\Repository\Exception\RecordNotPersistedException;
10
use Oc\Repository\Exception\RecordsNotFoundException;
11
12
class CachesRepository
13
{
14
    const TABLE = 'caches';
15
16
    /** @var Connection */
17
    private $connection;
18
19
    public function __construct(Connection $connection)
20
    {
21
        $this->connection = $connection;
22
    }
23
24
    /**
25
     * @return CachesEntity[]
26
     */
27
    public function fetchAll()
28
    {
29
        $statement = $this->connection->createQueryBuilder()
30
            ->select('*')
31
            ->from(self::TABLE)
32
            ->execute();
33
34
        $result = $statement->fetchAll();
35
36
        if ($statement->rowCount() === 0) {
37
            throw new RecordsNotFoundException('No records found');
38
        }
39
40
        $records = [];
41
42
        foreach ($result as $item) {
43
            $records[] = $this->getEntityFromDatabaseArray($item);
44
        }
45
46
        return $records;
47
    }
48
49
    /**
50
     * @return CachesEntity
51
     */
52
    public function fetchOneBy(array $where = [])
53
    {
54
        $queryBuilder = $this->connection->createQueryBuilder()
55
            ->select('*')
56
            ->from(self::TABLE)
57
            ->setMaxResults(1);
58
59
        if (count($where) > 0) {
60
            foreach ($where as $column => $value) {
61
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
62
            }
63
        }
64
65
        $statement = $queryBuilder->execute();
66
67
        $result = $statement->fetch();
68
69
        if ($statement->rowCount() === 0) {
70
            //            throw new RecordNotFoundException('Record with given where clause not found');
71
        } else {
72
            return $this->getEntityFromDatabaseArray($result);
73
        }
74
    }
75
76
    /**
77
     * @return CachesEntity[]
78
     */
79
    public function fetchBy(array $where = [])
80
    {
81
        $entities = [];
82
83
        $queryBuilder = $this->connection->createQueryBuilder()
84
            ->select('*')
85
            ->from(self::TABLE);
86
87
        if (count($where) > 0) {
88
            foreach ($where as $column => $value) {
89
                $queryBuilder->orWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
90
            }
91
        }
92
93
        $statement = $queryBuilder->execute();
94
        $result = $statement->fetchAll();
95
96
        if ($statement->rowCount() === 0) {
97
            //            throw new RecordsNotFoundException('No records with given where clause found');
98
        } else {
99
            foreach ($result as $item) {
100
                $entities[] = $this->getEntityFromDatabaseArray($item);
101
            }
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @return CachesEntity
109
     */
110
    public function getIdByWP(string $wp = '')
111
    {
112
        $queryBuilder = $this->connection->createQueryBuilder()
113
            ->select('*')
114
            ->from(self::TABLE)
115
            ->setMaxResults(1);
116
117
        if ($wp != '') {
118
            $queryBuilder->where('wp_oc = ' . $queryBuilder->createNamedParameter($wp));
119
            $queryBuilder->orWhere('wp_gc = ' . $queryBuilder->createNamedParameter($wp));
120
        }
121
122
        $statement = $queryBuilder->execute();
123
124
        $result = $statement->fetch();
125
126
        if ($statement->rowCount() === 0) {
127
            //            throw new RecordNotFoundException('Record with given where clause not found');
128
        } else {
129
            return $result['cache_id'];
130
        }
131
    }
132
133
    public function getEntityFromDatabaseArray(array $data)
134
    {
135
        $entity = new CachesEntity();
136
137
        $entity->setCacheId((int) $data['cache_id']);
138
        $entity->setOCid((string) $data['wp_oc']);
139
        $entity->setGCid((string) $data['wp_gc']);
140
        $entity->setName((string) $data['name']);
141
        $entity->setUserId((int) $data['user_id']);
142
143
        // ..
144
145
        return $entity;
146
    }
147
}
148