ResponseItems::__construct()   B
last analyzed

Complexity

Conditions 10
Paths 16

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 16
nop 1
dl 0
loc 27
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * @copyright   2018 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\EmailBundle\Swiftmailer\Momentum\Callback;
13
14
use Mautic\EmailBundle\Swiftmailer\SendGrid\Exception\ResponseItemException;
15
use Symfony\Component\HttpFoundation\Request;
16
17
class ResponseItems implements \Iterator
18
{
19
    /**
20
     * @var int
21
     */
22
    private $position = 0;
23
24
    /**
25
     * @var ResponseItem[]
26
     */
27
    private $items = [];
28
29
    public function __construct(Request $request)
30
    {
31
        $payload = $request->request->all();
32
        foreach ($payload as $item) {
33
            $msys = $item['msys'];
34
            if (isset($msys['message_event'])) {
35
                $event = $msys['message_event'];
36
            } elseif (isset($msys['unsubscribe_event'])) {
37
                $event = $msys['unsubscribe_event'];
38
            } else {
39
                continue;
40
            }
41
42
            if (isset($event['rcpt_type']) && 'to' !== $event['rcpt_type']) {
43
                // Ignore cc/bcc
44
45
                continue;
46
            }
47
48
            $bounceClass = isset($event['bounce_class']) ? (int) $event['bounce_class'] : null;
49
            if (empty($event['type']) || !CallbackEnum::shouldBeEventProcessed($event['type'], $bounceClass)) {
0 ignored issues
show
Bug introduced by
It seems like $bounceClass can also be of type integer; however, parameter $bounceClass of Mautic\EmailBundle\Swift...houldBeEventProcessed() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            if (empty($event['type']) || !CallbackEnum::shouldBeEventProcessed($event['type'], /** @scrutinizer ignore-type */ $bounceClass)) {
Loading history...
50
                continue;
51
            }
52
53
            try {
54
                $this->items[] = new ResponseItem($event);
55
            } catch (ResponseItemException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
56
            }
57
        }
58
    }
59
60
    /**
61
     * Return the current element.
62
     *
63
     * @see  http://php.net/manual/en/iterator.current.php
64
     *
65
     * @return ResponseItem
66
     */
67
    public function current()
68
    {
69
        return $this->items[$this->position];
70
    }
71
72
    /**
73
     * Move forward to next element.
74
     *
75
     * @see  http://php.net/manual/en/iterator.next.php
76
     */
77
    public function next()
78
    {
79
        ++$this->position;
80
    }
81
82
    /**
83
     * Return the key of the current element.
84
     *
85
     * @see  http://php.net/manual/en/iterator.key.php
86
     *
87
     * @return int
88
     */
89
    public function key()
90
    {
91
        return $this->position;
92
    }
93
94
    /**
95
     * Checks if current position is valid.
96
     *
97
     * @see  http://php.net/manual/en/iterator.valid.php
98
     *
99
     * @return bool
100
     */
101
    public function valid()
102
    {
103
        return isset($this->items[$this->position]);
104
    }
105
106
    /**
107
     * Rewind the Iterator to the first element.
108
     *
109
     * @see  http://php.net/manual/en/iterator.rewind.php
110
     */
111
    public function rewind()
112
    {
113
        $this->position = 0;
114
    }
115
}
116