Completed
Push — master ( d942b0...9a3ace )
by Gareth
08:41
created

MailAPI   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 88.42%

Importance

Changes 17
Bugs 1 Features 4
Metric Value
wmc 22
c 17
b 1
f 4
lcom 2
cbo 3
dl 0
loc 202
ccs 84
cts 95
cp 0.8842
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getFolderId() 0 8 2
A setFolderId() 0 4 1
A pickMailFolder() 0 10 2
A formatRestrictions() 0 19 4
B getMailItems() 0 36 5
A getUnreadMailItems() 0 14 1
A updateMailItem() 0 20 2
A markMailAsRead() 0 10 2
A sendMail() 0 17 2
A getAttachment() 0 14 1
1
<?php
2
3
namespace jamesiarmes\PEWS\Mail;
4
5
use jamesiarmes\PEWS\API;
6
use jamesiarmes\PEWS\API\Type;
7
use \DateTime;
8
use jamesiarmes\PEWS\API\Type\MessageType;
9
10
class MailAPI extends API
11
{
12
    /**
13
     * @var Type\FolderIdType
14
     */
15
    protected $folderId;
16
17
    /**
18
     * @return Type\FolderIdType
19
     */
20 6
    public function getFolderId()
21
    {
22 6
        if (!$this->folderId) {
23 1
            $this->folderId = $this->getFolderByDistinguishedId('inbox')->getFolderId();
0 ignored issues
show
Documentation Bug introduced by
The method getFolderId does not exist on object<jamesiarmes\PEWS\API\Type>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
24 1
        }
25
26 6
        return $this->folderId;
27
    }
28
29
    /**
30
     * @param Type\FolderIdType $folderId
31
     */
32 1
    public function setFolderId($folderId)
33
    {
34 1
        $this->folderId = $folderId;
35 1
    }
36
37
    /**
38
     * @param string $displayName
39
     * @param string|Type\FolderIdType $parentFolder
40
     */
41 6
    public function pickMailFolder($displayName = null, $parentFolder = 'inbox')
42
    {
43 6
        if ($displayName === null) {
44 1
            $this->folderId = $this->getFolderByDistinguishedId('inbox')->getFolderId();
0 ignored issues
show
Documentation Bug introduced by
The method getFolderId does not exist on object<jamesiarmes\PEWS\API\Type>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
45 1
            return;
46
        }
47
48 6
        $folder = $this->getFolderByDisplayName($displayName, $parentFolder);
49 6
        $this->folderId = $folder->getFolderId();
50 6
    }
51
52 1
    protected function formatRestrictions($restrictions)
53
    {
54 1
        foreach ($restrictions as $restrictionType => $query) {
55 1
            $formattedRestrictionType = array();
56 1
            foreach ($query as $key => $value) {
57 1
                if ($value === false) {
58 1
                    $value = 'false';
59 1
                }
60 1
                $formattedRestrictionType[] = array(
61 1
                    'FieldURI' => array('FieldURI' => $this->getFieldUriByName($key, 'message')),
62 1
                    'FieldURIOrConstant' => array('Constant' => array('Value' => (string) $value))
63 1
                );
64 1
            }
65
66 1
            $restrictions[$restrictionType] = $formattedRestrictionType;
67 1
        }
68
69 1
        return $restrictions;
70
    }
71
72
    /**
73
     * Get all mail items in the inbox
74
     *
75
     * @param Type\FolderIdType
76
     * @param array $options
77
     * @return Type\MessageType[]
78
     */
79 6
    public function getMailItems($folderId = null, $options = array())
80
    {
81 6
        if (!$folderId) {
82 6
            $folderId = $this->getFolderId();
83 6
        }
84
85
        $request = array(
86 6
            'Traversal' => 'Shallow',
87
            'ItemShape' => array(
88
                'BaseShape' => 'AllProperties'
89 6
            ),
90
            'ParentFolderIds' => array(
91 6
                'FolderId' => $folderId->toXmlObject()
92 6
            )
93 6
        );
94
95 6
        if (!empty($options['Restriction'])) {
96 1
            $options['Restriction'] = $this->formatRestrictions($options['Restriction']);
97 1
        }
98
99 6
        $request = array_replace_recursive($request, $options);
100
101 6
        $request = Type::buildFromArray($request);
102 6
        $response = $this->getClient()->FindItem($request);
103 6
        $messages = $response->getItems()->getMessage();
0 ignored issues
show
Documentation Bug introduced by
The method getItems does not exist on object<jamesiarmes\PEWS\API\Type>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
104
105 6
        if ($messages === null) {
106 6
            $messages = array();
107 6
        }
108
109 6
        if (!is_array($messages)) {
110 3
            $messages = array($messages);
111 3
        }
112
113 6
        return $messages;
114
    }
115
116
    /**
117
     * @param Type\FolderIdType $folderId
118
     * @param array $options
119
     * @return Type\MessageType[]
120
     */
121 1
    public function getUnreadMailItems($folderId = null, $options = array())
122
    {
123
        $unReadOption = array(
124
            'Restriction' => array(
125
                'IsEqualTo' => array(
126
                    'IsRead' => false
127 1
                )
128 1
            )
129 1
        );
130
131 1
        $options = array_replace_recursive($unReadOption, $options);
132
133 1
        return $this->getMailItems($folderId, $options);
134
    }
135
136
    /**
137
     * Updates a calendar item with changes
138
     *
139
     * @param $itemId Type\ItemIdType|Type
140
     * @param $changes
141
     * @return Type\MessageType[]
142
     */
143 1
    public function updateMailItem($itemId, $changes)
144
    {
145
        //Create the request
146
        $request = array(
147
            'ItemChange' => array(
148 1
                'ItemId' => $itemId->toArray(),
149
                'Updates' => array(
150 1
                    'SetItemField' => $this->buildUpdateItemChanges('Message', 'message', $changes)
151 1
                )
152 1
            )
153 1
        );
154
155 1
        $items = $this->updateItems($request);
156
157 1
        if (!is_array($items)) {
158 1
            $items = array($items);
159 1
        }
160
161 1
        return $items;
162
    }
163
164
    /**
165
     * @param $mailItem Type\MessageType|Type\ItemIdType
166
     * @param $isRead boolean
167
     */
168 1
    public function markMailAsRead($mailItem, $isRead = true)
169
    {
170 1
        if ($mailItem instanceof Type\MessageType) {
0 ignored issues
show
Bug introduced by
The class jamesiarmes\PEWS\API\Type\MessageType does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
171 1
            $mailItem = $mailItem->getItemId();
172 1
        }
173
174 1
        $this->updateMailItem($mailItem, array(
175
            'IsRead' => $isRead
176 1
        ));
177 1
    }
178
179 3
    public function sendMail(MessageType $message, $options = array())
180
    {
181 3
        $items = array('Message' => $message->toXmlObject());
182
        $defaultOptions = array(
183 3
            'MessageDisposition' => 'SendAndSaveCopy',
184 3
        );
185
186 3
        if ($this->getPrimarySmtpMailbox() != null) {
187
            $sentItems = $this->getFolderByDistinguishedId('sentitems')->getFolderId();
0 ignored issues
show
Documentation Bug introduced by
The method getFolderId does not exist on object<jamesiarmes\PEWS\API\Type>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
188
            $defaultOptions['SavedItemFolderId'] =
189
                array('FolderId' => $sentItems->toXmlObject());
190
        }
191
192 3
        $options = array_replace_recursive($defaultOptions, $options);
193
194 3
        return $this->createItems($items, $options);
195
    }
196
197
    public function getAttachment(Type\AttachmentIdType $attachmentId)
198
    {
199
        $request = array (
200
            'AttachmentIds' => array(
201
                $attachmentId->toXmlObject()
202
            ),
203
            'AttachmentShape' => array(
204
                'IncludeMimeContent' => true
205
            )
206
        );
207
208
        $attachment = $this->getClient()->GetAttachment($request);
0 ignored issues
show
Documentation Bug introduced by
The method GetAttachment does not exist on object<jamesiarmes\PEWS\API\ExchangeWebServices>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
209
        return $attachment;
210
    }
211
}
212