Code Duplication    Length = 194-194 lines in 10 locations

local/devel/Repositories/CacheSizeRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class CacheSizeRepository
10
{
11
    const TABLE = 'cache_size';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return GeoCacheSizeEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return GeoCacheSizeEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return GeoCacheSizeEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param GeoCacheSizeEntity $entity
109
     * @return GeoCacheSizeEntity
110
     */
111
    public function create(GeoCacheSizeEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->id = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param GeoCacheSizeEntity $entity
131
     * @return GeoCacheSizeEntity
132
     */
133
    public function update(GeoCacheSizeEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['id' => $entity->id]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param GeoCacheSizeEntity $entity
152
     * @return GeoCacheSizeEntity
153
     */
154
    public function remove(GeoCacheSizeEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['id' => $entity->id]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param GeoCacheSizeEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(GeoCacheSizeEntity $entity)
175
    {
176
        return [
177
        'id' => $entity->id,
178
        'name' => $entity->name,
179
        'trans_id' => $entity->transId,
180
        'ordinal' => $entity->ordinal,
181
        'de' => $entity->de,
182
        'en' => $entity->en,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return GeoCacheSizeEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new GeoCacheSizeEntity();
193
        $entity->id = (int) $data['id'];
194
        $entity->name = (string) $data['name'];
195
        $entity->transId = (int) $data['trans_id'];
196
        $entity->ordinal = (int) $data['ordinal'];
197
        $entity->de = (string) $data['de'];
198
        $entity->en = (string) $data['en'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/CoordinatesTypeRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class CoordinatesTypeRepository
10
{
11
    const TABLE = 'coordinates_type';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return CoordinatesTypeEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return CoordinatesTypeEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return CoordinatesTypeEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param CoordinatesTypeEntity $entity
109
     * @return CoordinatesTypeEntity
110
     */
111
    public function create(CoordinatesTypeEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->id = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param CoordinatesTypeEntity $entity
131
     * @return CoordinatesTypeEntity
132
     */
133
    public function update(CoordinatesTypeEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['id' => $entity->id]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param CoordinatesTypeEntity $entity
152
     * @return CoordinatesTypeEntity
153
     */
154
    public function remove(CoordinatesTypeEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['id' => $entity->id]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param CoordinatesTypeEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(CoordinatesTypeEntity $entity)
175
    {
176
        return [
177
        'id' => $entity->id,
178
        'name' => $entity->name,
179
        'trans_id' => $entity->transId,
180
        'image' => $entity->image,
181
        'preposition' => $entity->preposition,
182
        'pp_trans_id' => $entity->ppTransId,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return CoordinatesTypeEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new CoordinatesTypeEntity();
193
        $entity->id = (int) $data['id'];
194
        $entity->name = (string) $data['name'];
195
        $entity->transId = (int) $data['trans_id'];
196
        $entity->image = (string) $data['image'];
197
        $entity->preposition = (string) $data['preposition'];
198
        $entity->ppTransId = (int) $data['pp_trans_id'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/CountriesOptionsRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class CountriesOptionsRepository
10
{
11
    const TABLE = 'countries_options';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return CountriesOptionsEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return CountriesOptionsEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return CountriesOptionsEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param CountriesOptionsEntity $entity
109
     * @return CountriesOptionsEntity
110
     */
111
    public function create(CountriesOptionsEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->country = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param CountriesOptionsEntity $entity
131
     * @return CountriesOptionsEntity
132
     */
133
    public function update(CountriesOptionsEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['country' => $entity->country]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param CountriesOptionsEntity $entity
152
     * @return CountriesOptionsEntity
153
     */
154
    public function remove(CountriesOptionsEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['country' => $entity->country]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param CountriesOptionsEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(CountriesOptionsEntity $entity)
175
    {
176
        return [
177
        'country' => $entity->country,
178
        'display' => $entity->display,
179
        'gmLat' => $entity->gmLat,
180
        'gmLon' => $entity->gmLon,
181
        'gmZoom' => $entity->gmZoom,
182
        'nodeId' => $entity->nodeId,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return CountriesOptionsEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new CountriesOptionsEntity();
193
        $entity->country = (string) $data['country'];
194
        $entity->display = (int) $data['display'];
195
        $entity->gmLat = $data['gmLat'];
196
        $entity->gmLon = $data['gmLon'];
197
        $entity->gmZoom = (int) $data['gmZoom'];
198
        $entity->nodeId = (int) $data['nodeId'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/FieldNoteRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class FieldNoteRepository
10
{
11
    const TABLE = 'field_note';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return FieldNoteEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return FieldNoteEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return FieldNoteEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param FieldNoteEntity $entity
109
     * @return FieldNoteEntity
110
     */
111
    public function create(FieldNoteEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->id = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param FieldNoteEntity $entity
131
     * @return FieldNoteEntity
132
     */
133
    public function update(FieldNoteEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['id' => $entity->id]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param FieldNoteEntity $entity
152
     * @return FieldNoteEntity
153
     */
154
    public function remove(FieldNoteEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['id' => $entity->id]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param FieldNoteEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(FieldNoteEntity $entity)
175
    {
176
        return [
177
        'id' => $entity->id,
178
        'user_id' => $entity->userId,
179
        'geocache_id' => $entity->geocacheId,
180
        'type' => $entity->type,
181
        'date' => $entity->date,
182
        'text' => $entity->text,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return FieldNoteEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new FieldNoteEntity();
193
        $entity->id = (int) $data['id'];
194
        $entity->userId = (int) $data['user_id'];
195
        $entity->geocacheId = (int) $data['geocache_id'];
196
        $entity->type = $data['type'];
197
        $entity->date =  new DateTime($data['date']);
198
        $entity->text = (string) $data['text'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/OkapiStatsTempRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class OkapiStatsTempRepository
10
{
11
    const TABLE = 'okapi_stats_temp';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return OkapiStatsTempEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return OkapiStatsTempEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return OkapiStatsTempEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param OkapiStatsTempEntity $entity
109
     * @return OkapiStatsTempEntity
110
     */
111
    public function create(OkapiStatsTempEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->datetime = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param OkapiStatsTempEntity $entity
131
     * @return OkapiStatsTempEntity
132
     */
133
    public function update(OkapiStatsTempEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['datetime' => $entity->datetime]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param OkapiStatsTempEntity $entity
152
     * @return OkapiStatsTempEntity
153
     */
154
    public function remove(OkapiStatsTempEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['datetime' => $entity->datetime]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param OkapiStatsTempEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(OkapiStatsTempEntity $entity)
175
    {
176
        return [
177
        'datetime' => $entity->datetime,
178
        'consumer_key' => $entity->consumerKey,
179
        'user_id' => $entity->userId,
180
        'service_name' => $entity->serviceName,
181
        'calltype' => $entity->calltype,
182
        'runtime' => $entity->runtime,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return OkapiStatsTempEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new OkapiStatsTempEntity();
193
        $entity->datetime =  new DateTime($data['datetime']);
194
        $entity->consumerKey = (string) $data['consumer_key'];
195
        $entity->userId = (int) $data['user_id'];
196
        $entity->serviceName = (string) $data['service_name'];
197
        $entity->calltype = $data['calltype'];
198
        $entity->runtime = $data['runtime'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/RemovedObjectsRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class RemovedObjectsRepository
10
{
11
    const TABLE = 'removed_objects';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return RemovedObjectsEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return RemovedObjectsEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return RemovedObjectsEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param RemovedObjectsEntity $entity
109
     * @return RemovedObjectsEntity
110
     */
111
    public function create(RemovedObjectsEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->id = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param RemovedObjectsEntity $entity
131
     * @return RemovedObjectsEntity
132
     */
133
    public function update(RemovedObjectsEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['id' => $entity->id]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param RemovedObjectsEntity $entity
152
     * @return RemovedObjectsEntity
153
     */
154
    public function remove(RemovedObjectsEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['id' => $entity->id]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param RemovedObjectsEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(RemovedObjectsEntity $entity)
175
    {
176
        return [
177
        'id' => $entity->id,
178
        'localID' => $entity->localID,
179
        'uuid' => $entity->uuid,
180
        'type' => $entity->type,
181
        'removed_date' => $entity->removedDate,
182
        'node' => $entity->node,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return RemovedObjectsEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new RemovedObjectsEntity();
193
        $entity->id = (int) $data['id'];
194
        $entity->localID = (int) $data['localID'];
195
        $entity->uuid = (string) $data['uuid'];
196
        $entity->type = (int) $data['type'];
197
        $entity->removedDate =  new DateTime($data['removed_date']);
198
        $entity->node = (int) $data['node'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/SavedTextsRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class SavedTextsRepository
10
{
11
    const TABLE = 'saved_texts';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return SavedTextsEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return SavedTextsEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return SavedTextsEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param SavedTextsEntity $entity
109
     * @return SavedTextsEntity
110
     */
111
    public function create(SavedTextsEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->id = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param SavedTextsEntity $entity
131
     * @return SavedTextsEntity
132
     */
133
    public function update(SavedTextsEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['id' => $entity->id]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param SavedTextsEntity $entity
152
     * @return SavedTextsEntity
153
     */
154
    public function remove(SavedTextsEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['id' => $entity->id]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param SavedTextsEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(SavedTextsEntity $entity)
175
    {
176
        return [
177
        'id' => $entity->id,
178
        'date_created' => $entity->dateCreated,
179
        'object_type' => $entity->objectType,
180
        'object_id' => $entity->objectId,
181
        'subtype' => $entity->subtype,
182
        'text' => $entity->text,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return SavedTextsEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new SavedTextsEntity();
193
        $entity->id = (int) $data['id'];
194
        $entity->dateCreated =  new DateTime($data['date_created']);
195
        $entity->objectType = (int) $data['object_type'];
196
        $entity->objectId = (int) $data['object_id'];
197
        $entity->subtype = (int) $data['subtype'];
198
        $entity->text = (string) $data['text'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/StatpicsRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class StatpicsRepository
10
{
11
    const TABLE = 'statpics';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return StatpicsEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return StatpicsEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return StatpicsEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param StatpicsEntity $entity
109
     * @return StatpicsEntity
110
     */
111
    public function create(StatpicsEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->id = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param StatpicsEntity $entity
131
     * @return StatpicsEntity
132
     */
133
    public function update(StatpicsEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['id' => $entity->id]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param StatpicsEntity $entity
152
     * @return StatpicsEntity
153
     */
154
    public function remove(StatpicsEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['id' => $entity->id]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param StatpicsEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(StatpicsEntity $entity)
175
    {
176
        return [
177
        'id' => $entity->id,
178
        'tplpath' => $entity->tplpath,
179
        'previewpath' => $entity->previewpath,
180
        'description' => $entity->description,
181
        'trans_id' => $entity->transId,
182
        'maxtextwidth' => $entity->maxtextwidth,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return StatpicsEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new StatpicsEntity();
193
        $entity->id = (int) $data['id'];
194
        $entity->tplpath = (string) $data['tplpath'];
195
        $entity->previewpath = (string) $data['previewpath'];
196
        $entity->description = (string) $data['description'];
197
        $entity->transId = (int) $data['trans_id'];
198
        $entity->maxtextwidth = (int) $data['maxtextwidth'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/TownsRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class TownsRepository
10
{
11
    const TABLE = 'towns';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return TownsEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return TownsEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return TownsEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param TownsEntity $entity
109
     * @return TownsEntity
110
     */
111
    public function create(TownsEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->country = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param TownsEntity $entity
131
     * @return TownsEntity
132
     */
133
    public function update(TownsEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['country' => $entity->country]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param TownsEntity $entity
152
     * @return TownsEntity
153
     */
154
    public function remove(TownsEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['country' => $entity->country]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param TownsEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(TownsEntity $entity)
175
    {
176
        return [
177
        'country' => $entity->country,
178
        'name' => $entity->name,
179
        'trans_id' => $entity->transId,
180
        'coord_lat' => $entity->coordLat,
181
        'coord_long' => $entity->coordLong,
182
        'maplist' => $entity->maplist,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return TownsEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new TownsEntity();
193
        $entity->country = (string) $data['country'];
194
        $entity->name = (string) $data['name'];
195
        $entity->transId = (int) $data['trans_id'];
196
        $entity->coordLat = $data['coord_lat'];
197
        $entity->coordLong = $data['coord_long'];
198
        $entity->maplist = (int) $data['maplist'];
199
200
        return $entity;
201
    }
202
}
203

local/devel/Repositories/WaypointReportsRepository.php 1 location

@@ 9-202 (lines=194) @@
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class WaypointReportsRepository
10
{
11
    const TABLE = 'waypoint_reports';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return WaypointReportsEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
                    ->select('*')
28
                    ->from(self::TABLE)
29
                    ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @param array $where
48
     * @return WaypointReportsEntity
49
     */
50
    public function fetchOneBy(array $where = [])
51
    {
52
        $queryBuilder = $this->connection->createQueryBuilder()
53
                     ->select('*')
54
                     ->from(self::TABLE)
55
                     ->setMaxResults(1);
56
57
        if (count($where) > 0) {
58
            foreach ($where as $column => $value) {
59
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
60
            }
61
        }
62
63
        $statement = $queryBuilder->execute();
64
65
        $result = $statement->fetch();
66
67
        if ($statement->rowCount() === 0) {
68
            throw new RecordNotFoundException('Record with given where clause not found');
69
        }
70
71
        return $this->getEntityFromDatabaseArray($result);
72
    }
73
74
    /**
75
     * @param array $where
76
     * @return WaypointReportsEntity[]
77
     */
78
    public function fetchBy(array $where = [])
79
    {
80
        $queryBuilder = $this->connection->createQueryBuilder()
81
                     ->select('*')
82
                     ->from(self::TABLE);
83
84
        if (count($where) > 0) {
85
            foreach ($where as $column => $value) {
86
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
87
            }
88
        }
89
90
        $statement = $queryBuilder->execute();
91
92
        $result = $statement->fetchAll();
93
94
        if ($statement->rowCount() === 0) {
95
            throw new RecordsNotFoundException('No records with given where clause found');
96
        }
97
98
        $entities = [];
99
100
        foreach ($result as $item) {
101
            $entities[] = $this->getEntityFromDatabaseArray($item);
102
        }
103
104
        return $entities;
105
    }
106
107
    /**
108
     * @param WaypointReportsEntity $entity
109
     * @return WaypointReportsEntity
110
     */
111
    public function create(WaypointReportsEntity $entity)
112
    {
113
        if (!$entity->isNew()) {
114
            throw new RecordAlreadyExistsException('The entity does already exist.');
115
        }
116
117
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
118
119
        $this->connection->insert(
120
                    self::TABLE,
121
                    $databaseArray
122
                );
123
124
        $entity->reportId = (int) $this->connection->lastInsertId();
125
126
        return $entity;
127
    }
128
129
    /**
130
     * @param WaypointReportsEntity $entity
131
     * @return WaypointReportsEntity
132
     */
133
    public function update(WaypointReportsEntity $entity)
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
140
141
        $this->connection->update(
142
                    self::TABLE,
143
                    $databaseArray,
144
                    ['report_id' => $entity->reportId]
145
                );
146
147
        return $entity;
148
    }
149
150
    /**
151
     * @param WaypointReportsEntity $entity
152
     * @return WaypointReportsEntity
153
     */
154
    public function remove(WaypointReportsEntity $entity)
155
    {
156
        if ($entity->isNew()) {
157
            throw new RecordNotPersistedException('The entity does not exist.');
158
        }
159
160
        $this->connection->delete(
161
                    self::TABLE,
162
                    ['report_id' => $entity->reportId]
163
                );
164
165
        $entity->cacheId = null;
166
167
        return $entity;
168
    }
169
170
    /**
171
     * @param WaypointReportsEntity $entity
172
     * @return []
173
     */
174
    public function getDatabaseArrayFromEntity(WaypointReportsEntity $entity)
175
    {
176
        return [
177
        'report_id' => $entity->reportId,
178
        'date_reported' => $entity->dateReported,
179
        'wp_oc' => $entity->wpOc,
180
        'wp_external' => $entity->wpExternal,
181
        'source' => $entity->source,
182
        'gcwp_processed' => $entity->gcwpProcessed,
183
        ];
184
    }
185
186
    /**
187
     * @param array $data
188
     * @return WaypointReportsEntity
189
     */
190
    public function getEntityFromDatabaseArray(array $data)
191
    {
192
        $entity = new WaypointReportsEntity();
193
        $entity->reportId = (int) $data['report_id'];
194
        $entity->dateReported =  new DateTime($data['date_reported']);
195
        $entity->wpOc = (string) $data['wp_oc'];
196
        $entity->wpExternal = (string) $data['wp_external'];
197
        $entity->source = (string) $data['source'];
198
        $entity->gcwpProcessed = (int) $data['gcwp_processed'];
199
200
        return $entity;
201
    }
202
}
203