b98ddd2440bbf9b4eeac0fc1f8dde0cc18ce3ef6
[rock.divinelegy.git] / DataAccess / UserRepository.php
1 <?php
2
3 namespace DataAccess;
4
5 use DataAccess\IUserRepository;
6 use DataAccess\DataMapper\IDataMapper;
7 use Domain\Entities\IUser;
8 use DataAccess\Queries\IQueryBuilderFactory;
9 use Services\IFacebookSessionFactory;
10
11 //TODO: Implement some sort of caching. Probably OK for now not to worry.
12 class UserRepository implements IUserRepository
13 {
14 private $_dataMapper;
15 private $_queryBuilderFactory;
16 private $_facebookSessionFactory;
17
18 public function __construct(IDataMapper $dataMapper, IQueryBuilderFactory $queryBuilderFactory, IFacebookSessionFactory $faceBookSessionFactory) {
19 $this->_dataMapper = $dataMapper;
20 $this->_queryBuilderFactory = $queryBuilderFactory;
21 $this->_facebookSessionFactory = $faceBookSessionFactory;
22 }
23
24 public function findById($id) {
25 $queryBuilder = $this->_queryBuilderFactory->createInstance();
26 $queryBuilder->where('id', '=', "$id");
27
28 return $this->_dataMapper->map(
29 'User',
30 $queryBuilder
31 );
32 }
33
34 public function findRange($id, $limit)
35 {
36 return $this->_dataMapper->findRange(
37 'User',
38 'SELECT * FROM %s WHERE id>=' . $id . ' LIMIT ' . $limit
39 );
40 }
41
42 public function findByFacebookId($id) {
43 $queryBuilder = $this->_queryBuilderFactory->createInstance();
44 $queryBuilder->where('facebook_id', '=', "$id");
45
46 $results = $this->_dataMapper->map(
47 'User',
48 $queryBuilder
49 );
50
51 return reset($results);
52 }
53
54 public function findByAuthToken($token) {
55 $facebookSession = $this->_facebookSessionFactory->createInstance($token);
56 $id = $facebookSession->getGraphObject(GraphUser::className())->getId();
57
58 return $this->findByFacebookId($id);
59 }
60
61 public function save(IUser $entity) {
62 $this->_dataMapper->save($entity);
63 }
64
65 //TODO: Implement
66 public function remove(IUser $entity) {
67 ;
68 }
69
70 }