Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Db\ResultSet\HydratingResultSet;
9
use Laminas\Db\Sql\Expression;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
use Laminas\Log\LoggerInterface;
12
use Laminas\Paginator\Paginator;
13
use Laminas\Paginator\Adapter\DbSelect;
14
 
15
use LeadersLinked\Model\DeviceHistory;
16
use LeadersLinked\Mapper\Common\MapperCommon;
17
use Laminas\Hydrator\ArraySerializableHydrator;
18
 
19
 
20
 
21
class DeviceHistoryMapper extends MapperCommon
22
{
23
    const _TABLE = 'tbl_device_history';
24
 
25
 
26
    /**
27
     *
28
     * @var DeviceHistoryMapper
29
     */
30
    private static $_instance;
31
 
32
    /**
33
     *
34
     * @param AdapterInterface $adapter
35
     */
36
    private function __construct($adapter)
37
    {
38
        parent::__construct($adapter);
39
    }
40
 
41
    /**
42
     *
43
     * @param AdapterInterface $adapter
44
     * @return DeviceHistoryMapper
45
     */
46
    public static function getInstance($adapter)
47
    {
48
        if(self::$_instance == null) {
49
            self::$_instance = new DeviceHistoryMapper($adapter);
50
        }
51
        return self::$_instance;
52
    }
53
 
54
 
55
    /**
56
     *
57
     * @param string $device_id
58
     * @param int $user_id
59
     * @param string $ip
60
     * @return DeviceHistory
61
     */
62
    public function fetchOneByDeviceIdAndUserIdAndIp($device_id, $user_id, $ip)
63
    {
64
        $select = $this->sql->select(self::_TABLE);
65
        $select->where->equalTo('device_id', $device_id);
66
        $select->where->equalTo('user_id', $user_id);
67
        $select->where->equalTo('ip', $ip);
68
        $select->limit(1);
69
 
70
        $prototype = new DeviceHistory();
71
        return $this->executeFetchOneObject($select, $prototype);
72
    }
73
 
74
 
75
    /**
76
     *
77
     * @param int $application_id
78
     * @param int $user_id
79
     * @return string
80
     */
81
    public function fetchLastDeviceTokenByApplicationIdAndUserId($application_id, $user_id)
82
 
83
    {
84
        $select = $this->sql->select();
85
        $select->columns([]);
86
        $select->from(['tb1' => DeviceHistoryMapper::_TABLE  ]);
87
        $select->join(['tb2' => DeviceMapper::_TABLE], 'tb1.device_id  = tb2.id', ['token']);
88
        $select->where->equalTo('tb2.application_id', $application_id);
89
        $select->where->equalTo('tb1.user_id', $user_id);
90
        $select->order('tb1.updated_on  desc');
91
        $select->limit(1);
92
 
93
 
94
        $record = $this->executeFetchOneArray($select);
95
        if($record) {
96
            return $record['token'];
97
        }
98
        return;
99
 
100
    }
101
 
102
 
103
 
104
    /**
105
     *
106
     * @param DeviceHistory $deviceHistory
107
     * @return boolean
108
     */
109
    public function insert($deviceHistory)
110
    {
111
        $hydrator = new ObjectPropertyHydrator();
112
        $values = $hydrator->extract($deviceHistory);
113
        $values = $this->removeEmpty($values);
114
 
115
        $insert = $this->sql->insert(self::_TABLE);
116
        $insert->values($values);
117
 
118
        //echo $insert->getSqlString($this->adapter->platform); exit;
119
 
120
 
121
        return $this->executeInsert($insert);
122
 
123
    }
124
 
125
    /**
126
     *
127
     * @param DeviceHistory $deviceHistory
128
     * @return boolean
129
     */
130
    public function update($deviceHistory)
131
    {
132
        $hydrator = new ObjectPropertyHydrator();
133
        $values = $hydrator->extract($deviceHistory);
134
        $values = $this->removeEmpty($values);
135
 
136
        $update = $this->sql->update(self::_TABLE);
137
        $update->set($values);
138
        $update->where->equalTo('device_id', $deviceHistory->device_id);
139
        $update->where->equalTo('user_id', $deviceHistory->user_id);
140
        $update->where->equalTo('ip', $deviceHistory->ip);
141
 
142
        return $this->executeUpdate($update);
143
    }
144
 
145
    /**
146
     *
147
     * @return boolean
148
     */
149
    public function truncate()
150
    {
151
        $sql = 'DELETE FROM ' . self::_TABLE;
152
        return $this->executeSentenceWithParameters($sql);
153
    }
154
 
155
 
156
 
157
 
158
}