PostmarkappParser   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 26
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 245
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 16 1
A address() 0 4 1
A foreachAddress() 0 10 2
A from() 0 4 1
A replyTo() 0 4 1
A to() 0 4 1
A cc() 0 4 1
A bcc() 0 4 1
A subject() 0 4 1
A body() 0 10 2
A bodyHtml() 0 4 1
A bodyText() 0 4 1
A bodyIsText() 0 4 1
A bodyIsHtml() 0 4 2
A headers() 0 10 2
A header() 0 6 1
A isSpam() 0 6 2
A hasAttachements() 0 4 1
A attachements() 0 17 3
1
<?php
2
3
namespace Inbounder\Parsers;
4
5
use Illuminate\Support\Collection;
6
use Inbounder\Parsers\Contracts\EmailParserInterface;
7
use Inbounder\Parsers\Contracts\ParserInterface;
8
use Inbounder\Parsers\Objects\Attachment;
9
use Inbounder\Parsers\Objects\Email\Address;
10
use Inbounder\Parsers\Objects\Email\Header;
11
12
class PostmarkappParser extends AbstractParser implements EmailParserInterface
13
{
14
    /**
15
     * Parse the request and return itself.
16
     *
17
     * @return ParserInterface
18
     */
19
    public function parse() : ParserInterface
20
    {
21
        $this->from = $this->from();
0 ignored issues
show
Documentation introduced by
The property from does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
22
        $this->replyTo = $this->replyTo();
0 ignored issues
show
Documentation introduced by
The property replyTo does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
23
24
        $this->to = $this->to();
0 ignored issues
show
Documentation introduced by
The property to does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
25
        $this->cc = $this->cc();
0 ignored issues
show
Documentation introduced by
The property cc does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
        $this->bcc = $this->bcc();
0 ignored issues
show
Documentation introduced by
The property bcc does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
28
        $this->subject = $this->subject();
0 ignored issues
show
Documentation introduced by
The property subject does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
29
        $this->body = $this->body();
0 ignored issues
show
Documentation introduced by
The property body does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
30
31
        $this->attachements = $this->attachements();
0 ignored issues
show
Documentation introduced by
The property attachements does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
32
33
        return $this;
34
    }
35
36
    /**
37
     * Return an Address object from an input.
38
     *
39
     * @param array $input
40
     *
41
     * @return Address
42
     */
43
    protected function address(array $input) : Address
44
    {
45
        return new Address($input['Email'], $input['Name'], $input['MailboxHash']);
46
    }
47
48
    /**
49
     * Parse an array of addresses.
50
     *
51
     * @param array $input
52
     *
53
     * @return Collection
54
     */
55
    protected function foreachAddress(array $input) : Collection
56
    {
57
        $collection = new Collection();
58
59
        foreach ($input as $address) {
60
            $collection->push($this->address($address));
61
        }
62
63
        return $collection;
64
    }
65
66
    /**
67
     * From property.
68
     *
69
     * @return Address
70
     */
71
    public function from() : Address
72
    {
73
        return $this->address($this->input('FromFull'));
0 ignored issues
show
Bug introduced by
It seems like $this->input('FromFull') targeting Inbounder\Parsers\AbstractParser::input() can also be of type string; however, Inbounder\Parsers\PostmarkappParser::address() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
    }
75
76
    /**
77
     * Reply-to property.
78
     *
79
     * @todo: edit this to handler the reply-to parameter
80
     *
81
     * @return Address
82
     */
83
    public function replyTo() : Address
84
    {
85
        return $this->address($this->input('FromFull'));
0 ignored issues
show
Bug introduced by
It seems like $this->input('FromFull') targeting Inbounder\Parsers\AbstractParser::input() can also be of type string; however, Inbounder\Parsers\PostmarkappParser::address() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86
    }
87
88
    /**
89
     * To property.
90
     *
91
     * @return Collection<Address>
0 ignored issues
show
Documentation introduced by
The doc-type Collection<Address> could not be parsed: Expected "|" or "end of type", but got "<" at position 10. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
92
     */
93
    public function to() : Collection
94
    {
95
        return $this->foreachAddress($this->input('ToFull'));
0 ignored issues
show
Bug introduced by
It seems like $this->input('ToFull') targeting Inbounder\Parsers\AbstractParser::input() can also be of type string; however, Inbounder\Parsers\Postma...arser::foreachAddress() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
96
    }
97
98
    /**
99
     * CC property.
100
     *
101
     * @return Collection<Address>
0 ignored issues
show
Documentation introduced by
The doc-type Collection<Address> could not be parsed: Expected "|" or "end of type", but got "<" at position 10. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
102
     */
103
    public function cc() : Collection
104
    {
105
        return $this->foreachAddress($this->input('CcFull'));
0 ignored issues
show
Bug introduced by
It seems like $this->input('CcFull') targeting Inbounder\Parsers\AbstractParser::input() can also be of type string; however, Inbounder\Parsers\Postma...arser::foreachAddress() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
106
    }
107
108
    /**
109
     * BCC property.
110
     *
111
     * @return Collection<Address>
0 ignored issues
show
Documentation introduced by
The doc-type Collection<Address> could not be parsed: Expected "|" or "end of type", but got "<" at position 10. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
112
     */
113
    public function bcc() : Collection
114
    {
115
        return $this->foreachAddress($this->input('BccFull'));
0 ignored issues
show
Bug introduced by
It seems like $this->input('BccFull') targeting Inbounder\Parsers\AbstractParser::input() can also be of type string; however, Inbounder\Parsers\Postma...arser::foreachAddress() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
116
    }
117
118
    /**
119
     * Subject property.
120
     *
121
     * @return string
122
     */
123
    public function subject()
124
    {
125
        return $this->input('Subject');
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->input('Subject'); of type string|array adds the type array to the return on line 125 which is incompatible with the return type declared by the interface Inbounder\Parsers\Contra...arserInterface::subject of type string.
Loading history...
126
    }
127
128
    /**
129
     * Body.
130
     *
131
     * @return string
132
     */
133
    public function body()
134
    {
135
        $body = $this->bodyHtml();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->bodyHtml(); of type string|array adds the type array to the return on line 141 which is incompatible with the return type declared by the interface Inbounder\Parsers\Contra...ilParserInterface::body of type string.
Loading history...
136
137
        if ($this->bodyIsText()) {
138
            $body = $this->bodyText();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->bodyText(); of type string|array adds the type array to the return on line 141 which is incompatible with the return type declared by the interface Inbounder\Parsers\Contra...ilParserInterface::body of type string.
Loading history...
139
        }
140
141
        return $body;
142
    }
143
144
    /**
145
     * Return the body html.
146
     *
147
     * @return string
148
     */
149
    public function bodyHtml()
150
    {
151
        return $this->input('HtmlBody');
152
    }
153
154
    /**
155
     * Return the body text.
156
     *
157
     * @return string
158
     */
159
    public function bodyText()
160
    {
161
        return $this->input('TextBody');
162
    }
163
164
    /**
165
     * IsText.
166
     *
167
     * @return bool
168
     */
169
    public function bodyIsText()
170
    {
171
        return !$this->bodyIsHtml();
172
    }
173
174
    /**
175
     * IsHtml.
176
     *
177
     * @return bool
178
     */
179
    public function bodyIsHtml()
180
    {
181
        return !is_null($this->input('HtmlBody')) || $this->input('HtmlBody') !== '';
182
    }
183
184
    /**
185
     * Get email headers.
186
     *
187
     * @return Collection
188
     */
189
    public function headers() : Collection
190
    {
191
        $collection = new Collection();
192
193
        foreach ($this->input('Headers') as $header) {
0 ignored issues
show
Bug introduced by
The expression $this->input('Headers') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
194
            $collection->push(new Header($header['Name'], $header['Value']));
195
        }
196
197
        return $collection;
198
    }
199
200
    /**
201
     * retrieve a email header.
202
     *
203
     * return Array
204
     */
205
    public function header($name)
206
    {
207
        return $this->headers()->filter(function ($header) use ($name) {
208
            return $header->name === $name;
209
        })->first();
210
    }
211
212
    /**
213
     * Determinate if the email is marked as spam.
214
     *
215
     * @return bool
216
     */
217
    public function isSpam()
218
    {
219
        $header = $this->header('X-Spam-Flag');
220
221
        return $header->value === 'NO' ? false : true;
222
    }
223
224
    /**
225
     * Has attachements.
226
     *
227
     * @return bool
228
     */
229
    public function hasAttachements()
230
    {
231
        return $this->attachements->isEmpty();
0 ignored issues
show
Documentation introduced by
The property attachements does not exist on object<Inbounder\Parsers\PostmarkappParser>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
232
    }
233
234
    /**
235
     * Attachments.
236
     *
237
     * @return Collection<File>
0 ignored issues
show
Documentation introduced by
The doc-type Collection<File> could not be parsed: Expected "|" or "end of type", but got "<" at position 10. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
238
     */
239
    public function attachements() : Collection
240
    {
241
        $collection = new Collection();
242
243
        foreach ($this->input('Attachments') as $attachment) {
0 ignored issues
show
Bug introduced by
The expression $this->input('Attachments') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
244
            $collection->push(
245
                new Attachment(
246
                    $attachment['Name'],
247
                    (isset($attachment['Content']) ? $attachment['Content'] : ''),
248
                    $attachment['ContentType'],
249
                    $attachment['ContentLength']
250
                )
251
            );
252
        }
253
254
        return $collection;
255
    }
256
}
257