| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Database\Repository; |
| 4 |
|
| 5 |
use Depicter\Database\Entity\DocumentAnalytics; |
| 6 |
use Depicter\Utility\Sanitize; |
| 7 |
|
| 8 |
class DocumentAnalyticsRepository |
| 9 |
{ |
| 10 |
|
| 11 |
private const ALLOWED_ORDER_BY = ['created_at', 'id', 'source_id', 'event_type']; |
| 12 |
private const ALLOWED_ORDER = ['ASC', 'DESC']; |
| 13 |
private const ALLOWED_EVENT_TYPES = ['view', 'click', 'impression', 'submission']; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var DocumentAnalytics |
| 17 |
*/ |
| 18 |
private DocumentAnalytics $documentAnalytics; |
| 19 |
|
| 20 |
|
| 21 |
public function __construct(){ |
| 22 |
$this->documentAnalytics = DocumentAnalytics::new(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Access to an instance of DocumentAnalytics entity |
| 27 |
* |
| 28 |
* @return DocumentAnalytics |
| 29 |
*/ |
| 30 |
public function documentAnalytics(): DocumentAnalytics{ |
| 31 |
return DocumentAnalytics::new(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Create a document analytics record |
| 36 |
* |
| 37 |
* @param int $documentId Document ID |
| 38 |
* @param string $eventType Event type (e.g., 'view', 'click', 'impression') |
| 39 |
* |
| 40 |
* @return mixed |
| 41 |
* @throws \Exception |
| 42 |
*/ |
| 43 |
public function create( $sourceId, $eventType ) { |
| 44 |
if ( ! in_array( $eventType, self::ALLOWED_EVENT_TYPES ) ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
$ipAddress = \Depicter::geoLocate()->getIP(); |
| 49 |
$userSession = \Depicter::authorization()->getUserSession(); |
| 50 |
$userID = is_user_logged_in() ? get_current_user_id() : 0; |
| 51 |
|
| 52 |
return $this->documentAnalytics()->create([ |
| 53 |
'source_id' => $sourceId, |
| 54 |
'event_type' => $eventType, |
| 55 |
'session_id' => $userSession, |
| 56 |
'user_id' => $userID, |
| 57 |
'ip_address' => $ipAddress, |
| 58 |
'created_at' => $this->documentAnalytics()->currentDateTime() |
| 59 |
]); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get analytics by document ID |
| 64 |
* |
| 65 |
* @param int $documentId |
| 66 |
* @param array $args |
| 67 |
* |
| 68 |
* @return array |
| 69 |
* @throws \Exception |
| 70 |
*/ |
| 71 |
public function getByDocumentId( $documentId, $args = [] ): array { |
| 72 |
$query = $this->documentAnalytics()->where('source_id', $documentId); |
| 73 |
|
| 74 |
if ( ! empty( $args['event_type'] ) && in_array( $args['event_type'], self::ALLOWED_EVENT_TYPES ) ) { |
| 75 |
$query->where('event_type', $args['event_type']); |
| 76 |
} |
| 77 |
|
| 78 |
if (!empty($args['dateStart'])) { |
| 79 |
$args['dateStart'] = $this->normalizeDateTime( $args['dateStart'], 'start' ); |
| 80 |
$query->where('created_at', '>=', $args['dateStart']); |
| 81 |
} |
| 82 |
|
| 83 |
if (!empty($args['dateEnd'])) { |
| 84 |
$args['dateEnd'] = $this->normalizeDateTime( $args['dateEnd'], 'end' ); |
| 85 |
$query->where('created_at', '<=', $args['dateEnd']); |
| 86 |
} |
| 87 |
|
| 88 |
$orderBy = in_array( $args['orderBy'] ?? '', self::ALLOWED_ORDER_BY, true ) ? $args['orderBy'] : 'created_at'; |
| 89 |
$order = in_array( strtoupper( $args['order'] ?? '' ), self::ALLOWED_ORDER, true ) ? strtoupper($args['order']) : 'DESC'; |
| 90 |
$query->orderBy($orderBy, $order); |
| 91 |
|
| 92 |
if (!empty($args['perPage'])) { |
| 93 |
$page = $args['page'] ?? 1; |
| 94 |
if ($pager = $query->paginate($args['perPage'], $page)) { |
| 95 |
$results = $pager->getResults(); |
| 96 |
} else { |
| 97 |
$results = []; |
| 98 |
} |
| 99 |
} else { |
| 100 |
$results = $query->findAll()->get(); |
| 101 |
} |
| 102 |
|
| 103 |
return $results ? $results->toArray() : []; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get analytics count by document ID and event type |
| 108 |
* |
| 109 |
* @param int $documentId |
| 110 |
* @param string $eventType |
| 111 |
* @param array $args |
| 112 |
* |
| 113 |
* @return int |
| 114 |
* @throws \Exception |
| 115 |
*/ |
| 116 |
public function getCount( $documentId, $eventType = '', $args = [] ): int { |
| 117 |
$query = DocumentAnalytics::new()->where('source_id', $documentId); |
| 118 |
|
| 119 |
if (!empty($eventType) && in_array( $eventType, self::ALLOWED_EVENT_TYPES ) ) { |
| 120 |
$query->where('event_type', $eventType); |
| 121 |
} |
| 122 |
|
| 123 |
if (!empty($args['dateStart'])) { |
| 124 |
$args['dateStart'] = $this->normalizeDateTime( $args['dateStart'], 'start' ); |
| 125 |
$query->where('created_at', '>=', $args['dateStart']); |
| 126 |
} |
| 127 |
|
| 128 |
if (!empty($args['dateEnd'])) { |
| 129 |
$args['dateEnd'] = $this->normalizeDateTime( $args['dateEnd'], 'end' ); |
| 130 |
$query->where('created_at', '<=', $args['dateEnd']); |
| 131 |
} |
| 132 |
|
| 133 |
return $query->count(); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Delete analytics by document ID |
| 138 |
* |
| 139 |
* @param int $documentId |
| 140 |
* |
| 141 |
* @return bool |
| 142 |
* @throws \Exception |
| 143 |
*/ |
| 144 |
public function deleteByDocumentId( $documentId ) { |
| 145 |
$analytics = $this->documentAnalytics()->where('source_id', $documentId)->findAll()->get(); |
| 146 |
|
| 147 |
if ($analytics && $analytics->count()) { |
| 148 |
foreach ($analytics as $analytic) { |
| 149 |
$analytic->delete(); |
| 150 |
} |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Delete analytics by ID |
| 159 |
* |
| 160 |
* @param mixed $id Can be an ID or list of comma separated IDs |
| 161 |
* |
| 162 |
* @return bool |
| 163 |
* @throws \Exception |
| 164 |
*/ |
| 165 |
public function delete( $id ) { |
| 166 |
$succeed = false; |
| 167 |
|
| 168 |
if( is_array( $id ) ){ |
| 169 |
$ids = $id; |
| 170 |
} elseif( false !== strpos( $id, ',' ) ){ |
| 171 |
$ids = explode(',', $id ); |
| 172 |
} else { |
| 173 |
$ids = [$id]; |
| 174 |
} |
| 175 |
|
| 176 |
foreach( $ids as $id ){ |
| 177 |
$id = Sanitize::int( $id ); |
| 178 |
if( $analytic = $this->documentAnalytics()->findById( $id ) ){ |
| 179 |
$analytic->delete(); |
| 180 |
$succeed = true; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return $succeed; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Normalize date to include time |
| 189 |
* |
| 190 |
* @param string $date |
| 191 |
* @param string $type |
| 192 |
* |
| 193 |
* @return string |
| 194 |
*/ |
| 195 |
public function normalizeDateTime( string $date, $type = 'start' ){ |
| 196 |
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)){ |
| 197 |
return $date . ' ' . ($type === 'start' ? '00:00:00' : '23:59:59'); |
| 198 |
} |
| 199 |
return $date; |
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
|