Message::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
defined('BASEPATH') OR exit('No direct script access allowed');
3
4
class Message
5
{
6
  /**
7
   * [protected description]
8
   * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
9
   */
10
  protected $message;
11
12
  public function __construct($jsonString)
13
  {
14
    $this->message = json_decode($jsonString);
15
  }
16
  /**
17
   * [__get description]
18
   * @date   2019-11-23
19
   * @param  string     $key [description]
20
   * @return [type]          [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
21
   */
22
  public function __get(string $key):string
23
  {
24
    return $this->message->{$key};
25
  }
26
  /**
27
   * [header description]
28
   * @date   2019-11-22
29
   * @param  string      $key [description]
30
   * @return string|null      [description]
31
   */
32
  public function header(string $key):?string
33
  {
34
    foreach ($this->message->payload->headers as $header) {
35
      if ($header->name == $key) {
36
        return $header->value;
37
      }
38
    }
39
    return null;
40
  }
41
  /**
42
   * [isMultiPart description]
43
   * @date   2019-11-23
44
   * @return bool       [description]
45
   */
46
  public function isMultiPart():bool
47
  {
48
    return isset($this->message->payload->parts);
49
  }
50
  /**
51
   * [getSize description]
52
   * @date   2019-11-23
53
   * @return int        [description]
54
   */
55
  public function getSize():int
56
  {
57
    return $this->message->payload->body->size;
58
  }
59
  /**
60
   * [body description]
61
   * @date   2019-11-23
62
   * @param  integer    $partId [description]
63
   * @return [type]             [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
64
   */
65
  public function body(int $partId=0)
66
  {
67
    if ($this->isMultiPart()) {
68
      // Set Parent ID.
69
      $this->message->payload->parts[$partId]->id = $this->message->id;
70
      
71
      return new MessagePart($this->message->payload->parts[$partId]);
72
    }
73
74
    return base64url_decode($this->message->payload->body->data);
75
  }
76
}
77