Completed
Push — master ( 07244a...ed11ea )
by Gareth
05:28
created

MailAPI::getMailItems()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 8.8571
cc 3
eloc 14
nc 4
nop 2
crap 3
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
        return $this->getClient()->FindItem($request);
103
    }
104
105
    /**
106
     * @param Type\FolderIdType $folderId
107
     * @param array $options
108
     * @return Type\MessageType[]
109
     */
110 6
    public function getUnreadMailItems($folderId = null, $options = array())
111
    {
112
        $unReadOption = array(
113
            'Restriction' => array(
114
                'IsEqualTo' => array(
115
                    'IsRead' => false
116 1
                )
117 6
            )
118 1
        );
119
120 1
        $options = array_replace_recursive($unReadOption, $options);
121
122 1
        return $this->getMailItems($folderId, $options);
123
    }
124
125
    /**
126
     * Updates a calendar item with changes
127
     *
128
     * @param $itemId Type\ItemIdType|Type
129
     * @param $changes
130
     * @return Type\MessageType[]
131
     */
132 1
    public function updateMailItem($itemId, $changes)
133
    {
134
        //Create the request
135
        $request = array(
136
            'ItemChange' => array(
137 1
                'ItemId' => $itemId->toArray(),
138
                'Updates' => array(
139 1
                    'SetItemField' => $this->buildUpdateItemChanges('Message', 'message', $changes)
140 1
                )
141 1
            )
142 1
        );
143
144 1
        $items = $this->updateItems($request);
145
146 1
        if (!is_array($items)) {
147 1
            $items = array($items);
148 1
        }
149
150 1
        return $items;
151
    }
152
153
    /**
154
     * @param $mailItem Type\MessageType|Type\ItemIdType
155
     * @param $isRead boolean
156
     */
157 1
    public function markMailAsRead($mailItem, $isRead = true)
158
    {
159 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...
160 1
            $mailItem = $mailItem->getItemId();
161 1
        }
162
163 1
        $this->updateMailItem($mailItem, array(
164
            'IsRead' => $isRead
165 1
        ));
166 1
    }
167
168 3
    public function sendMail(MessageType $message, $options = array())
169
    {
170 3
        $items = array('Message' => $message->toXmlObject());
171
        $defaultOptions = array(
172 3
            'MessageDisposition' => 'SendAndSaveCopy',
173 3
        );
174
175 3
        if ($this->getPrimarySmtpMailbox() != null) {
176
            $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...
177
            $defaultOptions['SavedItemFolderId'] =
178
                array('FolderId' => $sentItems->toXmlObject());
179
        }
180
181 3
        $options = array_replace_recursive($defaultOptions, $options);
182
183 3
        return $this->createItems($items, $options);
184
    }
185
186
    public function getAttachment(Type\AttachmentIdType $attachmentId)
187
    {
188
        $request = array (
189
            'AttachmentIds' => array(
190
                $attachmentId->toXmlObject()
191
            ),
192
            'AttachmentShape' => array(
193
                'IncludeMimeContent' => true
194
            )
195
        );
196
197
        $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...
198
        return $attachment;
199
    }
200
}
201