1 <?php
2 /* --------------------------------------------------------------
3 EmailRepository.php 2015-01-29 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('EmailRepositoryInterface');
13
14 /**
15 * Class EmailRepository
16 *
17 * Handles the database operations that concern the email records of the
18 * database. It provides a layer for more complicated methods that use the
19 * writer, reader and deleter.
20 *
21 * @category System
22 * @package Email
23 * @subpackage Repository
24 */
25 class EmailRepository implements EmailRepositoryInterface
26 {
27 /**
28 * E-Mail writer.
29 * @var EmailWriterInterface
30 */
31 protected $writer;
32
33 /**
34 * E-Mail reader.
35 * @var EmailReaderInterface
36 */
37 protected $reader;
38
39 /**
40 * E-Mail deleter.
41 * @var EmailDeleterInterface
42 */
43 protected $deleter;
44
45
46 /**
47 * Class Constructor
48 *
49 * @param EmailWriterInterface $writer Used for database writing operations.
50 * @param EmailReaderInterface $reader Used for database reading operations.
51 * @param EmailDeleterInterface $deleter Used for database deleting operations.
52 */
53 public function __construct(EmailWriterInterface $writer,
54 EmailReaderInterface $reader,
55 EmailDeleterInterface $deleter)
56 {
57 $this->writer = $writer;
58 $this->reader = $reader;
59 $this->deleter = $deleter;
60 }
61
62
63 /**
64 * Writes an email record into the database.
65 *
66 * @param EmailInterface $email Contains the email information.
67 */
68 public function write(EmailInterface $email)
69 {
70 $this->writer->write($email);
71 }
72
73
74 /**
75 * Returns an email record by ID.
76 *
77 * @param IdType $id Database ID of the record to be returned.
78 *
79 * @throws UnexpectedValueException If record does not exist.
80 *
81 * @return EmailInterface Returns the email object that matches the ID.
82 */
83 public function getById(IdType $id)
84 {
85 $collection = $this->reader->get(array('email_id' => (int)(string)$id));
86
87 if($collection->count() === 0)
88 {
89 throw new UnexpectedValueException('No email record matches the provided $id: ' . $id);
90 }
91
92 return $collection->getItem(0);
93 }
94
95
96 /**
97 * Find email by ID
98 *
99 * This method will try to find the email record that matches provided ID and
100 * will return NULL on when the record does not exist.
101 *
102 * @param IdType $id Email record id to be found.
103 *
104 * @return EmailInterface|null Returns email object or null on failure.
105 */
106 public function findById(IdType $id)
107 {
108 $collection = $this->reader->get(array('email_id' => (int)(string)$id));
109
110 if($collection->count() > 0)
111 {
112 return $collection->getItem(0);
113 }
114 else
115 {
116 return null;
117 }
118 }
119
120
121 /**
122 * Returns a collection of pending emails.
123 *
124 * @return EmailCollection Returns the pending email objects.
125 */
126 public function getPending()
127 {
128 return $this->reader->get(array('is_pending' => true));
129 }
130
131
132 /**
133 * Returns a collection of sent emails.
134 *
135 * @return EmailCollection Returns the sent email objects.
136 */
137 public function getSent()
138 {
139 return $this->reader->get(array('is_pending' => false));
140 }
141
142
143 /**
144 * Returns all email records from the database.
145 *
146 * @return EmailCollection Returns all email objects.
147 */
148 public function getAll()
149 {
150 return $this->reader->get();
151 }
152
153
154 /**
155 * Removes all information of an email record from the database.
156 *
157 * This method will remove ALL the email information, from the tables that
158 * contain information about the specified email.
159 *
160 * @param EmailInterface $email Contains the email information.
161 */
162 public function delete(EmailInterface $email)
163 {
164 $this->deleter->delete($email);
165 }
166
167
168 /**
169 * Filter email records with provided keyword string.
170 *
171 * @param string $p_keyword String to be used for filtering the email records.
172 * @param array $limit (optional) Array that contains LIMIT and OFFSET value
173 * e.g. array( 'limit' => 10, 'offset' => 5 )
174 * @param array $order (optional) Contains arrays with column, direction pairs
175 * e.g. array( 'column' => 'direction' )
176 *
177 * @return EmailCollection Returns a collection containing the email records.
178 */
179 public function filter($p_keyword, array $limit = array(), array $order = array())
180 {
181 return $this->reader->filter($p_keyword, $limit, $order);
182 }
183
184
185 /**
186 * Get the current count of the email records in the database.
187 *
188 * @param string $p_filterKeyword (optional) If provided the records will be filtered.
189 *
190 * @return int Returns the row number of the email table.
191 */
192 public function getRecordCount($p_filterKeyword = '')
193 {
194 return $this->reader->getRecordCount($p_filterKeyword);
195 }
196 }