Admin
5 years ago
Commands
5 years ago
Helpers
5 years ago
Migrations
5 years ago
ValueObjects
5 years ago
Assets.php
5 years ago
Log.php
5 years ago
LogFactory.php
5 years ago
LogModel.php
5 years ago
LogRepository.php
5 years ago
LogServiceProvider.php
5 years ago
LogModel.php
242 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Log; |
| 4 | |
| 5 | use Give\Log\ValueObjects\LogType; |
| 6 | |
| 7 | /** |
| 8 | * Class LogModel |
| 9 | * @package Give\Log |
| 10 | * |
| 11 | * @method error( string $message, string $source, array $context = [] ) |
| 12 | * @method warning( string $message, string $source, array $context = [] ) |
| 13 | * @method notice( string $message, string $source, array $context = [] ) |
| 14 | * @method success( string $message, string $source, array $context = [] ) |
| 15 | * @method info( string $message, string $source, array $context = [] ) |
| 16 | * @method http( string $message, string $source, array $context = [] ) |
| 17 | * |
| 18 | * @since 2.10.0 |
| 19 | */ |
| 20 | class LogModel { |
| 21 | /** |
| 22 | * @var string |
| 23 | */ |
| 24 | private $type; |
| 25 | |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $message; |
| 30 | |
| 31 | /** |
| 32 | * @var string |
| 33 | */ |
| 34 | private $category; |
| 35 | |
| 36 | /** |
| 37 | * @var string |
| 38 | */ |
| 39 | private $source; |
| 40 | |
| 41 | /** |
| 42 | * @var array |
| 43 | */ |
| 44 | private $context; |
| 45 | |
| 46 | /** |
| 47 | * @var int|null |
| 48 | */ |
| 49 | private $id; |
| 50 | |
| 51 | /** |
| 52 | * @var string|null |
| 53 | */ |
| 54 | private $date; |
| 55 | |
| 56 | /** |
| 57 | * LogModel constructor. |
| 58 | * |
| 59 | * @param string $type |
| 60 | * @param string $message |
| 61 | * @param string $category |
| 62 | * @param string $source |
| 63 | * @param array $context |
| 64 | * @param int|null $logId |
| 65 | * @param string|null $date |
| 66 | */ |
| 67 | public function __construct( $type, $message, $category, $source, $context, $logId, $date ) { |
| 68 | $this->setType( $type ); |
| 69 | $this->category = $category; |
| 70 | $this->source = $source; |
| 71 | $this->context = $context; |
| 72 | $this->message = $message; |
| 73 | $this->id = $logId; |
| 74 | $this->date = $date; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Set log type |
| 79 | * If log type is not supported, it will fallback to NOTICE type |
| 80 | * |
| 81 | * @param string $type |
| 82 | */ |
| 83 | private function setType( $type ) { |
| 84 | $this->type = in_array( $type, LogType::getAll(), true ) |
| 85 | ? $type |
| 86 | : LogType::getDefault(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Set log message |
| 91 | * If not defined, fallback to default value |
| 92 | * |
| 93 | * @param string|null $message |
| 94 | */ |
| 95 | private function setMessage( $message ) { |
| 96 | $this->message = is_null( $message ) |
| 97 | ? esc_html__( 'Something went wrong', 'give' ) |
| 98 | : $message; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Set log source |
| 103 | * If not defined, fallback to default value |
| 104 | * |
| 105 | * @param string|null $source |
| 106 | */ |
| 107 | private function setSource( $source ) { |
| 108 | $this->source = is_null( $source ) |
| 109 | ? esc_html__( 'Give Core', 'give' ) |
| 110 | : $source; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @return int |
| 115 | */ |
| 116 | public function getId() { |
| 117 | return $this->id; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @return string |
| 122 | */ |
| 123 | public function getType() { |
| 124 | return $this->type; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return string |
| 129 | */ |
| 130 | public function getCategory() { |
| 131 | return $this->category; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return string |
| 136 | */ |
| 137 | public function getSource() { |
| 138 | return $this->source; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return string |
| 143 | */ |
| 144 | public function getMessage() { |
| 145 | return $this->message; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @return array |
| 150 | */ |
| 151 | public function getContext() { |
| 152 | return $this->context; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @return string |
| 157 | */ |
| 158 | public function getDate() { |
| 159 | return $this->date; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get context data |
| 164 | * |
| 165 | * @param bool $jsonEncode |
| 166 | * |
| 167 | * @return string|array |
| 168 | */ |
| 169 | public function getData( $jsonEncode = false ) { |
| 170 | $data = [ |
| 171 | 'message' => $this->getMessage(), |
| 172 | 'context' => $this->getContext(), |
| 173 | ]; |
| 174 | |
| 175 | if ( $jsonEncode ) { |
| 176 | return json_encode( $data ); |
| 177 | } |
| 178 | |
| 179 | return $data; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @param string $key |
| 184 | * @param mixed $value |
| 185 | */ |
| 186 | private function addContext( $key, $value ) { |
| 187 | if ( is_array( $value ) || is_object( $value ) ) { |
| 188 | $value = print_r( $value, true ); |
| 189 | } |
| 190 | $this->context[ $key ] = $value; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Add supplemental information to existing log. |
| 195 | * Supplemental data will be added as a context with a key prefixed with the current timestamp. |
| 196 | * |
| 197 | * @param string $key |
| 198 | * @param string $value |
| 199 | */ |
| 200 | public function addSupplemental( $key, $value ) { |
| 201 | $contextName = sprintf( '[%s] %s', date( 'Y-m-d H:i:s' ), $key ); |
| 202 | $this->addContext( $contextName, $value ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @param string $type |
| 207 | * @param array $args |
| 208 | */ |
| 209 | public function __call( $type, $args ) { |
| 210 | list ( $message, $source, $context ) = array_pad( $args, 3, null ); |
| 211 | |
| 212 | $this->setType( $type ); |
| 213 | $this->setMessage( $message ); |
| 214 | $this->setSource( $source ); |
| 215 | |
| 216 | // Set additional context |
| 217 | if ( is_array( $context ) ) { |
| 218 | foreach ( $context as $key => $value ) { |
| 219 | $this->addContext( $key, $value ); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | $this->save(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Save log record |
| 228 | */ |
| 229 | public function save() { |
| 230 | /** |
| 231 | * var LogRepository $repository |
| 232 | */ |
| 233 | $repository = give( LogRepository::class ); |
| 234 | |
| 235 | if ( $this->getId() ) { |
| 236 | $repository->updateLog( $this ); |
| 237 | } else { |
| 238 | $this->id = $repository->insertLog( $this ); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 |