| 1 | 
           efrain | 
           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 | 
           use LeadersLinked\Model\Device;
  | 
        
        
            | 
            | 
           19 | 
              | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           class DeviceHistoryMapper extends MapperCommon
  | 
        
        
            | 
            | 
           23 | 
           {
  | 
        
        
            | 
            | 
           24 | 
               const _TABLE = 'tbl_device_history';
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
               /**
  | 
        
        
            | 
            | 
           28 | 
                *
  | 
        
        
            | 
            | 
           29 | 
                * @var DeviceHistoryMapper
  | 
        
        
            | 
            | 
           30 | 
                */
  | 
        
        
            | 
            | 
           31 | 
               private static $_instance;
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
               /**
  | 
        
        
            | 
            | 
           34 | 
                *
  | 
        
        
            | 
            | 
           35 | 
                * @param AdapterInterface $adapter
  | 
        
        
            | 
            | 
           36 | 
                */
  | 
        
        
            | 
            | 
           37 | 
               private function __construct($adapter)
  | 
        
        
            | 
            | 
           38 | 
               {
  | 
        
        
            | 
            | 
           39 | 
                   parent::__construct($adapter);
  | 
        
        
            | 
            | 
           40 | 
               }
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /**
  | 
        
        
            | 
            | 
           43 | 
                *
  | 
        
        
            | 
            | 
           44 | 
                * @param AdapterInterface $adapter
  | 
        
        
            | 
            | 
           45 | 
                * @return DeviceHistoryMapper
  | 
        
        
            | 
            | 
           46 | 
                */
  | 
        
        
            | 
            | 
           47 | 
               public static function getInstance($adapter)
  | 
        
        
            | 
            | 
           48 | 
               {
  | 
        
        
            | 
            | 
           49 | 
                   if(self::$_instance == null) {
  | 
        
        
            | 
            | 
           50 | 
                       self::$_instance = new DeviceHistoryMapper($adapter);
  | 
        
        
            | 
            | 
           51 | 
                   }
  | 
        
        
            | 
            | 
           52 | 
                   return self::$_instance;
  | 
        
        
            | 
            | 
           53 | 
               }
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
               /**
  | 
        
        
            | 
            | 
           57 | 
                *
  | 
        
        
            | 
            | 
           58 | 
                * @param string $device_id
  | 
        
        
            | 
            | 
           59 | 
                * @param int $user_id
  | 
        
        
            | 
            | 
           60 | 
                * @param string $ip
  | 
        
        
            | 
            | 
           61 | 
                * @return DeviceHistory
  | 
        
        
            | 
            | 
           62 | 
                */
  | 
        
        
            | 
            | 
           63 | 
               public function fetchOneByDeviceIdAndUserIdAndIp($device_id, $user_id, $ip)
  | 
        
        
            | 
            | 
           64 | 
               {
  | 
        
        
            | 
            | 
           65 | 
                   $select = $this->sql->select(self::_TABLE);
  | 
        
        
            | 
            | 
           66 | 
                   $select->where->equalTo('device_id', $device_id);
  | 
        
        
            | 
            | 
           67 | 
                   $select->where->equalTo('user_id', $user_id);
  | 
        
        
            | 
            | 
           68 | 
                   $select->where->equalTo('ip', $ip);
  | 
        
        
            | 
            | 
           69 | 
                   $select->limit(1);
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                   $prototype = new DeviceHistory();
  | 
        
        
            | 
            | 
           72 | 
                   return $this->executeFetchOneObject($select, $prototype);
  | 
        
        
            | 
            | 
           73 | 
               }
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
               /**
  | 
        
        
            | 
            | 
           77 | 
                *
  | 
        
        
            | 
            | 
           78 | 
                * @param int $application_id
  | 
        
        
            | 
            | 
           79 | 
                * @param int $user_id
  | 
        
        
            | 
            | 
           80 | 
                * @return string
  | 
        
        
            | 
            | 
           81 | 
                */
  | 
        
        
            | 
            | 
           82 | 
               public function fetchLastDeviceTokenByApplicationIdAndUserId($application_id, $user_id)
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
               {
  | 
        
        
            | 
            | 
           85 | 
                   $select = $this->sql->select();
  | 
        
        
            | 
            | 
           86 | 
                   $select->columns([]);
  | 
        
        
            | 
            | 
           87 | 
                   $select->from(['tb1' => DeviceHistoryMapper::_TABLE  ]);
  | 
        
        
            | 
            | 
           88 | 
                   $select->join(['tb2' => DeviceMapper::_TABLE], 'tb1.device_id  = tb2.id', ['token']);
  | 
        
        
            | 
            | 
           89 | 
                   $select->where->equalTo('tb2.application_id', $application_id);
  | 
        
        
            | 
            | 
           90 | 
                   $select->where->equalTo('tb1.user_id', $user_id);
  | 
        
        
            | 
            | 
           91 | 
                   $select->order('tb1.updated_on  desc');
  | 
        
        
            | 
            | 
           92 | 
                   $select->limit(1);
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
                   $record = $this->executeFetchOneArray($select);
  | 
        
        
            | 
            | 
           96 | 
                   if($record) {
  | 
        
        
            | 
            | 
           97 | 
                       return $record['token'];
  | 
        
        
            | 
            | 
           98 | 
                   }
  | 
        
        
            | 
            | 
           99 | 
                   return;
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
               }
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
               /**
  | 
        
        
            | 
            | 
           104 | 
                *
  | 
        
        
            | 
            | 
           105 | 
                * @param int $application_id
  | 
        
        
            | 
            | 
           106 | 
                * @param int $user_id
  | 
        
        
            | 
            | 
           107 | 
                * @return Device
  | 
        
        
            | 
            | 
           108 | 
                */
  | 
        
        
            | 
            | 
           109 | 
               public function fetchLastDeviceByApplicationIdAndUserId($application_id, $user_id)
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
               {
  | 
        
        
            | 
            | 
           112 | 
                   $select = $this->sql->select();
  | 
        
        
            | 
            | 
           113 | 
                   $select->columns([]);
  | 
        
        
            | 
            | 
           114 | 
                   $select->from(['tb1' => DeviceHistoryMapper::_TABLE  ]);
  | 
        
        
            | 
            | 
           115 | 
                   $select->join(['tb2' => DeviceMapper::_TABLE], 'tb1.device_id  = tb2.id');
  | 
        
        
            | 
            | 
           116 | 
                   $select->where->equalTo('tb2.application_id', $application_id);
  | 
        
        
            | 
            | 
           117 | 
                   $select->where->equalTo('tb1.user_id', $user_id);
  | 
        
        
            | 
            | 
           118 | 
                   $select->order('tb1.updated_on  desc');
  | 
        
        
            | 
            | 
           119 | 
                   $select->limit(1);
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                   $prototype = new Device();
  | 
        
        
            | 
            | 
           122 | 
              | 
        
        
            | 
            | 
           123 | 
                   return $this->executeFetchOneObject($select, $prototype);
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
               }
  | 
        
        
            | 
            | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
               /**
  | 
        
        
            | 
            | 
           130 | 
                *
  | 
        
        
            | 
            | 
           131 | 
                * @param DeviceHistory $deviceHistory
  | 
        
        
            | 
            | 
           132 | 
                * @return boolean
  | 
        
        
            | 
            | 
           133 | 
                */
  | 
        
        
            | 
            | 
           134 | 
               public function insert($deviceHistory)
  | 
        
        
            | 
            | 
           135 | 
               {
  | 
        
        
            | 
            | 
           136 | 
                   $hydrator = new ObjectPropertyHydrator();
  | 
        
        
            | 
            | 
           137 | 
                   $values = $hydrator->extract($deviceHistory);
  | 
        
        
            | 
            | 
           138 | 
                   $values = $this->removeEmpty($values);
  | 
        
        
            | 
            | 
           139 | 
              | 
        
        
            | 
            | 
           140 | 
                   $insert = $this->sql->insert(self::_TABLE);
  | 
        
        
            | 
            | 
           141 | 
                   $insert->values($values);
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
                   //echo $insert->getSqlString($this->adapter->platform); exit;
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
            | 
            | 
           145 | 
              | 
        
        
            | 
            | 
           146 | 
                   return $this->executeInsert($insert);
  | 
        
        
            | 
            | 
           147 | 
              | 
        
        
            | 
            | 
           148 | 
               }
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
               /**
  | 
        
        
            | 
            | 
           151 | 
                *
  | 
        
        
            | 
            | 
           152 | 
                * @param DeviceHistory $deviceHistory
  | 
        
        
            | 
            | 
           153 | 
                * @return boolean
  | 
        
        
            | 
            | 
           154 | 
                */
  | 
        
        
            | 
            | 
           155 | 
               public function update($deviceHistory)
  | 
        
        
            | 
            | 
           156 | 
               {
  | 
        
        
            | 
            | 
           157 | 
                   $hydrator = new ObjectPropertyHydrator();
  | 
        
        
            | 
            | 
           158 | 
                   $values = $hydrator->extract($deviceHistory);
  | 
        
        
            | 
            | 
           159 | 
                   $values = $this->removeEmpty($values);
  | 
        
        
            | 
            | 
           160 | 
              | 
        
        
            | 
            | 
           161 | 
                   $update = $this->sql->update(self::_TABLE);
  | 
        
        
            | 
            | 
           162 | 
                   $update->set($values);
  | 
        
        
            | 
            | 
           163 | 
                   $update->where->equalTo('device_id', $deviceHistory->device_id);
  | 
        
        
            | 
            | 
           164 | 
                   $update->where->equalTo('user_id', $deviceHistory->user_id);
  | 
        
        
            | 
            | 
           165 | 
                   $update->where->equalTo('ip', $deviceHistory->ip);
  | 
        
        
            | 
            | 
           166 | 
              | 
        
        
            | 
            | 
           167 | 
                   return $this->executeUpdate($update);
  | 
        
        
            | 
            | 
           168 | 
               }
  | 
        
        
            | 
            | 
           169 | 
              | 
        
        
            | 
            | 
           170 | 
               /**
  | 
        
        
            | 
            | 
           171 | 
                *
  | 
        
        
            | 
            | 
           172 | 
                * @return boolean
  | 
        
        
            | 
            | 
           173 | 
                */
  | 
        
        
            | 
            | 
           174 | 
               public function truncate()
  | 
        
        
            | 
            | 
           175 | 
               {
  | 
        
        
            | 
            | 
           176 | 
                   $sql = 'DELETE FROM ' . self::_TABLE;
  | 
        
        
            | 
            | 
           177 | 
                   return $this->executeSentenceWithParameters($sql);
  | 
        
        
            | 
            | 
           178 | 
               }
  | 
        
        
            | 
            | 
           179 | 
              | 
        
        
            | 
            | 
           180 | 
              | 
        
        
            | 
            | 
           181 | 
              | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
           }
  |