Completed
Push — master ( 5ad124...178107 )
by Malte
02:00
created

Structure   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
eloc 39
c 3
b 1
f 1
dl 0
loc 122
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findContentType() 0 5 2
A parse() 0 3 1
A getBoundary() 0 2 1
A __construct() 0 5 1
B find_parts() 0 36 9
1
<?php
2
/*
3
* File: Structure.php
4
* Category: -
5
* Author: M.Goldenbaum
6
* Created: 17.09.20 20:38
7
* Updated: -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\PHPIMAP;
14
15
16
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
17
use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException;
18
19
/**
20
 * Class Structure
21
 *
22
 * @package Webklex\PHPIMAP
23
 */
24
class Structure {
25
26
    /**
27
     * Raw structure
28
     *
29
     * @var string $raw
30
     */
31
    public $raw = "";
32
33
    /**
34
     * @var Header $header
35
     */
36
    private $header = null;
37
38
    /**
39
     * Message type (if multipart or not)
40
     *
41
     * @var int $type
42
     */
43
    public $type = IMAP::MESSAGE_TYPE_TEXT;
44
45
    /**
46
     * All available parts
47
     *
48
     * @var Part[] $parts
49
     */
50
    public $parts = [];
51
52
    /**
53
     * Config holder
54
     *
55
     * @var array $config
56
     */
57
    protected $config = [];
58
59
    /**
60
     * Structure constructor.
61
     * @param $raw_structure
62
     * @param Header $header
63
     *
64
     * @throws MessageContentFetchingException
65
     * @throws InvalidMessageDateException
66
     */
67
    public function __construct($raw_structure, Header $header) {
68
        $this->raw = $raw_structure;
69
        $this->header = $header;
70
        $this->config = ClientManager::get('options');
71
        $this->parse();
72
    }
73
74
    /**
75
     * Parse the given raw structure
76
     *
77
     * @throws MessageContentFetchingException
78
     * @throws InvalidMessageDateException
79
     */
80
    protected function parse(){
81
        $this->findContentType();
82
        $this->parts = $this->find_parts();
83
    }
84
85
    /**
86
     * Determine the message content type
87
     */
88
    public function findContentType(){
89
        if(stripos($this->header->get("content-type"), 'multipart') === 0) {
90
            $this->type = IMAP::MESSAGE_TYPE_MULTIPART;
91
        }else{
92
            $this->type = IMAP::MESSAGE_TYPE_TEXT;
93
        }
94
    }
95
96
    /**
97
     * Determine the message content type
98
     */
99
    public function getBoundary(){
100
        return $this->header->find("/boundary\=\"(.*)\"/");
101
    }
102
103
    /**
104
     * Find all available parts
105
     *
106
     * @return array
107
     * @throws MessageContentFetchingException
108
     * @throws InvalidMessageDateException
109
     */
110
    public function find_parts(){
111
        if($this->type === IMAP::MESSAGE_TYPE_MULTIPART) {
112
            if (($boundary = $this->getBoundary()) === null)  {
113
                throw new MessageContentFetchingException("no content found", 0);
114
            }
115
116
            $boundaries = [
117
                $boundary
118
            ];
119
120
            if (preg_match("/boundary\=\"(.*)\"/", $this->raw, $match) == 1) {
121
                if(is_array($match[1])){
122
                    foreach($match[1] as $matched){
123
                        $boundaries[] = $matched;
124
                    }
125
                }else{
126
                    if(!empty($match[1])) {
127
                        $boundaries[] = $match[1];
128
                    }
129
                }
130
            }
131
132
            $raw_parts = explode( $boundaries[0], str_replace($boundaries, $boundaries[0], $this->raw) );
133
            $parts = [];
134
            $part_number = 0;
135
            foreach($raw_parts as $part) {
136
                $part = trim(rtrim($part));
137
                if ($part !== "--") {
138
                    $parts[] = new Part($part, null, $part_number);
139
                    $part_number++;
140
                }
141
            }
142
            return $parts;
143
        }
144
145
        return [new Part($this->raw, $this->header)];
146
    }
147
}