/
Loader.php
144 lines (125 loc) · 2.8 KB
/
Loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Message\Mothership\ReferAFriend\Reward\Config;
use Message\Mothership\ReferAFriend\Reward\Type\Collection as Types;
use Message\Cog\DB\QueryBuilderFactory;
use Message\Cog\DB\Result;
use Message\Cog\Localisation\Translator;
/**
* Class Loader
* @package Message\Mothership\ReferAFriend\Reward\Config
*
* @author Thomas Marchant <thomas@mothership.ec>
*
* Class for loading configurations from the database
*/
class Loader
{
/**
* @var QueryBuilderFactory
*/
private $_qbFactory;
/**
* @var Types
*/
private $_types;
/**
* @var ConfigFactory
*/
private $_configFactory;
private $_columns = [
'reward_config_id AS id',
'name',
'type',
'message',
'created_at AS createdAt',
];
public function __construct(QueryBuilderFactory $qbFactory, Types $types, ConfigFactory $configFactory)
{
$this->_qbFactory = $qbFactory;
$this->_types = $types;
$this->_configFactory = $configFactory;
}
/**
* Get all active (non-deleted) configurations from the database
*
* @return array
*/
public function getCurrent()
{
$result = $this->_getSelect()
->where('deleted_at IS NULL')
->orderBy('createdAt DESC')
->getQuery()
->run()
;
return $this->_bind($result);
}
/**
* Get a specific configuration by its ID
*
* @param int $id
*
* @return ConfigProxy
*/
public function getByID($id)
{
$result = $this->_getSelect()
->where('reward_config_id = :id?i', ['id' => $id])
->getQuery()
->run()
;
return $this->_bind($result, false);
}
/**
* Get all configurations, including deleted ones
*
* @return array
*/
public function getAll()
{
$result = $this->_getSelect()
->orderBy('createdAt DESC')
->getQuery()
->run()
;
return $this->_bind($result);
}
/**
* Get an instance of the QueryBuilder with the correct columns and table already set
*
* @return \Message\Cog\DB\QueryBuilder
*/
private function _getSelect()
{
return $this->_qbFactory
->getQueryBuilder()
->select($this->_columns)
->from('refer_a_friend_reward_config')
;
}
/**
* Loop through database result and bind to a ConfigProxy object. If $asArray is set to false, a single configuration
* will be returned.
*
* @param Result $result
* @param bool $asArray
*
* @return array | ConfigProxy
*/
private function _bind(Result $result, $asArray = true)
{
$configs = [];
foreach ($result as $row) {
$config = $this->_configFactory->getConfigProxy();
$config->setID((int) $row->id);
$config->setName($row->name);
$config->setType($this->_types->get($row->type));
$config->setMessage($row->message);
$createdAt = new \DateTime;
$createdAt->setTimestamp($row->createdAt);
$config->setCreatedAt($createdAt);
$configs[] = $config;
}
return $asArray ? $configs : array_shift($configs);
}
}