|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Libraries; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\Cache; |
| 6 | +use \BNETDocs\Libraries\Common; |
| 7 | +use \BNETDocs\Libraries\Database; |
| 8 | +use \BNETDocs\Libraries\DatabaseDriver; |
| 9 | +use \BNETDocs\Libraries\Exceptions\PacketApplicationLayerNotFoundException; |
| 10 | +use \BNETDocs\Libraries\Exceptions\QueryException; |
| 11 | +use \InvalidArgumentException; |
| 12 | +use \PDO; |
| 13 | +use \PDOException; |
| 14 | +use \StdClass; |
| 15 | + |
| 16 | +class PacketApplicationLayer { |
| 17 | + |
| 18 | + protected $id; |
| 19 | + protected $label; |
| 20 | + protected $tag; |
| 21 | + |
| 22 | + public function __construct($data) { |
| 23 | + if (is_numeric($data)) { |
| 24 | + $this->id = (int) $data; |
| 25 | + $this->label = null; |
| 26 | + $this->tag = null; |
| 27 | + $this->refresh(); |
| 28 | + } else if ($data instanceof StdClass) { |
| 29 | + self::normalize($data); |
| 30 | + $this->id = $data->id; |
| 31 | + $this->label = $data->label; |
| 32 | + $this->tag = $data->tag; |
| 33 | + } else { |
| 34 | + throw new InvalidArgumentException("Cannot use data argument"); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static function getAllPacketApplicationLayers() { |
| 39 | + if (!isset(Common::$database)) { |
| 40 | + Common::$database = DatabaseDriver::getDatabaseObject(); |
| 41 | + } |
| 42 | + try { |
| 43 | + $stmt = Common::$database->prepare(" |
| 44 | + SELECT |
| 45 | + `id`, |
| 46 | + `label`, |
| 47 | + `tag` |
| 48 | + FROM `packet_application_layers` |
| 49 | + ORDER BY `id` ASC; |
| 50 | + "); |
| 51 | + if (!$stmt->execute()) { |
| 52 | + throw new QueryException("Cannot refresh packet application layers"); |
| 53 | + } |
| 54 | + $packetapplicationlayers = []; |
| 55 | + while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { |
| 56 | + $packetapplicationlayers[] = new self($row); |
| 57 | + Common::$cache->set( |
| 58 | + "bnetdocs-packetapplicationlayer-" . $row->id, serialize($row), 300 |
| 59 | + ); |
| 60 | + } |
| 61 | + $stmt->closeCursor(); |
| 62 | + return $packetapplicationlayers; |
| 63 | + } catch (PDOException $e) { |
| 64 | + throw new QueryException("Cannot refresh packet application layers", $e); |
| 65 | + } |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + public function getId() { |
| 70 | + return $this->id; |
| 71 | + } |
| 72 | + |
| 73 | + public function getLabel() { |
| 74 | + return $this->label; |
| 75 | + } |
| 76 | + |
| 77 | + public function getTag() { |
| 78 | + return $this->tag; |
| 79 | + } |
| 80 | + |
| 81 | + protected static function normalize(StdClass &$data) { |
| 82 | + $data->id = (int) $data->id; |
| 83 | + $data->label = (string) $data->label; |
| 84 | + $data->tag = (string) $data->tag; |
| 85 | + |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + public function refresh() { |
| 90 | + $cache_key = "bnetdocs-packetapplicationlayer-" . $this->id; |
| 91 | + $cache_val = Common::$cache->get($cache_key); |
| 92 | + if ($cache_val !== false) { |
| 93 | + $cache_val = unserialize($cache_val); |
| 94 | + $this->label = $cache_val->label; |
| 95 | + $this->tag = $cache_val->tag; |
| 96 | + return true; |
| 97 | + } |
| 98 | + if (!isset(Common::$database)) { |
| 99 | + Common::$database = DatabaseDriver::getDatabaseObject(); |
| 100 | + } |
| 101 | + try { |
| 102 | + $stmt = Common::$database->prepare(" |
| 103 | + SELECT |
| 104 | + `id`, |
| 105 | + `label`, |
| 106 | + `tag` |
| 107 | + FROM `packet_application_layers` |
| 108 | + WHERE `id` = :id |
| 109 | + LIMIT 1; |
| 110 | + "); |
| 111 | + $stmt->bindParam(":id", $this->id, PDO::PARAM_INT); |
| 112 | + if (!$stmt->execute()) { |
| 113 | + throw new QueryException("Cannot refresh packet application layer"); |
| 114 | + } else if ($stmt->rowCount() == 0) { |
| 115 | + throw new PacketApplicationLayerNotFoundException($this->id); |
| 116 | + } |
| 117 | + $row = $stmt->fetch(PDO::FETCH_OBJ); |
| 118 | + $stmt->closeCursor(); |
| 119 | + self::normalize($row); |
| 120 | + $this->label = $row->label; |
| 121 | + $this->tag = $row->tag; |
| 122 | + Common::$cache->set($cache_key, serialize($row), 300); |
| 123 | + return true; |
| 124 | + } catch (PDOException $e) { |
| 125 | + throw new QueryException("Cannot refresh packet application layer", $e); |
| 126 | + } |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments