This is gonna be fun to sort out...
[rock.divinelegy.git] / DataAccess / FileRepository.php
1 <?php
2
3 namespace DataAccess;
4
5 use DataAccess\IFileRepository;
6 use DataAccess\DataMapper\IDataMapper;
7 use DataAccess\Queries\IQueryBuilderFactory;
8
9 class FileRepository implements IFileRepository
10 {
11 private $_dataMapper;
12 private $_queryBuilderFactory;
13
14 public function __construct(IDataMapper $dataMapper, IQueryBuilderFactory $queryBuilderFactory) {
15 $this->_dataMapper = $dataMapper;
16 $this->_queryBuilderFactory = $queryBuilderFactory;
17 }
18
19 public function findById($id) {
20 $queryBuilder = $this->_queryBuilderFactory->createInstance();
21 $queryBuilder->where('id', '=', $id);
22
23 $results = $this->_dataMapper->map(
24 'File',
25 $queryBuilder
26 );
27
28 return reset($results);
29 }
30
31 public function findAll()
32 {
33 $queryBuilder = $this->_queryBuilderFactory->createInstance();
34 $queryBuilder->where('id', '>', 0);
35
36 return $this->_dataMapper->map(
37 'File',
38 $queryBuilder
39 );
40 }
41
42 public function findRange($id, $limit)
43 {
44 return $this->_dataMapper->findRange(
45 'User',
46 'SELECT * FROM %s WHERE id>=' . $id . ' LIMIT ' . $limit
47 );
48 }
49
50 public function findByHash($hash)
51 {
52 $queryBuilder = $this->_queryBuilderFactory->createInstance();
53 $queryBuilder->where('hash', '=', "$hash");
54
55 $results = $this->_dataMapper->map(
56 'File',
57 $queryBuilder
58 );
59
60 //XXX: Hack. Sometimes instead of getting a real array back we get the
61 //lazyload thing because I was an idiot with the database. Originally
62 //I simply did return reset($results) but if we don't have an array that
63 //won't work. So instead do a foreach (lazyload thing is iterable) and just
64 //return the first element.
65 //
66 //XXX: Disregard, I fixed the DB at home so that wasn't an issue and never
67 //put it up on the live server. Idiot.
68 return reset($results);
69 }
70
71 public function save(\Domain\Entities\IFile $file) {
72 return $this->_dataMapper->save($file);
73 }
74 }