| Total Complexity | 11 |
| Total Lines | 172 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 18 | class DatasetMapper |
||
| 19 | { |
||
| 20 | private $userId; |
||
| 21 | private $db; |
||
| 22 | private $logger; |
||
| 23 | const TABLE_NAME = 'analytics_dataset'; |
||
| 24 | |||
| 25 | public function __construct( |
||
| 26 | $userId, |
||
| 27 | IDBConnection $db, |
||
| 28 | LoggerInterface $logger |
||
| 29 | ) |
||
| 30 | { |
||
| 31 | $this->userId = $userId; |
||
| 32 | $this->db = $db; |
||
| 33 | $this->logger = $logger; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * get datasets |
||
| 38 | * @return array |
||
| 39 | * @throws Exception |
||
| 40 | */ |
||
| 41 | public function index(): array |
||
| 42 | { |
||
| 43 | $sql = $this->db->getQueryBuilder(); |
||
| 44 | $sql->from(self::TABLE_NAME) |
||
| 45 | ->select('id') |
||
| 46 | ->addSelect('name') |
||
| 47 | ->addSelect('dimension1') |
||
| 48 | ->addSelect('dimension2') |
||
| 49 | ->addSelect('value') |
||
| 50 | ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId))) |
||
| 51 | ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter('2'))) |
||
| 52 | ->addOrderBy('name', 'ASC'); |
||
| 53 | $statement = $sql->execute(); |
||
|
1 ignored issue
–
show
|
|||
| 54 | $result = $statement->fetchAll(); |
||
| 55 | $statement->closeCursor(); |
||
| 56 | return $result; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * create dataset |
||
| 61 | * @param $name |
||
| 62 | * @param $dimension1 |
||
| 63 | * @param $dimension2 |
||
| 64 | * @param $value |
||
| 65 | * @return int |
||
| 66 | * @throws Exception |
||
| 67 | */ |
||
| 68 | public function create($name, $dimension1, $dimension2, $value): int |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * get single dataset |
||
| 86 | * @param int $id |
||
| 87 | * @param string|null $user_id |
||
| 88 | * @return array |
||
| 89 | * @throws Exception |
||
| 90 | */ |
||
| 91 | public function read(int $id, string $user_id = null): array |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * update dataset |
||
| 110 | * @param $id |
||
| 111 | * @param $name |
||
| 112 | * @param $dimension1 |
||
| 113 | * @param $dimension2 |
||
| 114 | * @param $value |
||
| 115 | * @return bool |
||
| 116 | * @throws Exception |
||
| 117 | */ |
||
| 118 | public function update($id, $name, $dimension1, $dimension2, $value): bool |
||
| 119 | { |
||
| 120 | $name = $this->truncate($name, 64); |
||
| 121 | $sql = $this->db->getQueryBuilder(); |
||
| 122 | $sql->update(self::TABLE_NAME) |
||
| 123 | ->set('name', $sql->createNamedParameter($name)) |
||
| 124 | ->set('dimension1', $sql->createNamedParameter($dimension1)) |
||
| 125 | ->set('dimension2', $sql->createNamedParameter($dimension2)) |
||
| 126 | ->set('value', $sql->createNamedParameter($value)) |
||
| 127 | ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId))) |
||
| 128 | ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($id))); |
||
| 129 | $sql->execute(); |
||
|
1 ignored issue
–
show
|
|||
| 130 | return true; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * delete dataset |
||
| 135 | * @param $id |
||
| 136 | * @return bool |
||
| 137 | * @throws Exception |
||
| 138 | */ |
||
| 139 | public function delete($id): bool |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * get the newest timestamp of the data of a dataset |
||
| 151 | * @param $datasetId |
||
| 152 | * @return int |
||
| 153 | * @throws Exception |
||
| 154 | */ |
||
| 155 | public function getLastUpdate($datasetId): int |
||
| 156 | { |
||
| 157 | $sql = $this->db->getQueryBuilder(); |
||
| 158 | $sql->from('analytics_facts') |
||
| 159 | ->select($sql->func()->max('timestamp')) |
||
| 160 | ->where($sql->expr()->eq('dataset', $sql->createNamedParameter($datasetId))); |
||
| 161 | return (int)$sql->execute()->fetchOne(); |
||
|
1 ignored issue
–
show
|
|||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * get the report owner |
||
| 166 | * @param $datasetId |
||
| 167 | * @return string |
||
| 168 | * @throws Exception |
||
| 169 | */ |
||
| 170 | public function getOwner($datasetId): string |
||
| 171 | { |
||
| 172 | $sql = $this->db->getQueryBuilder(); |
||
| 173 | $sql->from(self::TABLE_NAME) |
||
| 174 | ->select('user_id') |
||
| 175 | ->where($sql->expr()->eq('id', $sql->createNamedParameter($datasetId))); |
||
| 176 | return (string)$sql->execute()->fetchOne(); |
||
|
1 ignored issue
–
show
|
|||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * truncates fields do DB-field size |
||
| 181 | * |
||
| 182 | * @param $string |
||
| 183 | * @param int $length |
||
| 184 | * @param string $dots |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | private function truncate($string, int $length, string $dots = "..."): string |
||
| 190 | } |
||
| 191 | } |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.