ExchangeSoapClient   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 65
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseCode() 0 4 1
B __construct() 0 30 5
1
<?php
2
3
namespace PhpEws\Ntlm;
4
5
use PhpEws\Exception\EwsException;
6
7
/**
8
 * Handles Soap communication with the Exchnage server using NTLM
9
 * authentication
10
 * 
11
 * @author James I. Armes <[email protected]>
12
 */
13
class ExchangeSoapClient extends NtlmSoapClient
14
{
15
    /**
16
     * Username for authentication on the exchnage server
17
     *
18
     * @var string
19
     */
20
    protected $user;
21
22
    /**
23
     * Password for authentication on the exchnage server
24
     *
25
     * @var string
26
     */
27
    protected $password;
28
29
    /**
30
     * Constructor
31
     *
32
     * @param string $wsdl
33
     * @param array $options
34
     *
35
     * @throws EwsException If $options does not contain username and password
36
     */
37
    public function __construct($wsdl, $options)
38
    {
39
        // Verify that a user name and password were entered.
40
        if (empty($options['user']) || empty($options['password'])) {
41
            throw new EwsException('A username and password is required.');
42
        }
43
44
        // Set the username and password properties.
45
        $this->user = $options['user'];
46
        $this->password = $options['password'];
47
48
        // If a version was set then add it to the headers.
49
        if (!empty($options['version'])) {
50
            $this->__default_headers[] = new \SoapHeader(
0 ignored issues
show
Bug introduced by
The property __default_headers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
                'http://schemas.microsoft.com/exchange/services/2006/types',
52
                'RequestServerVersion Version="' . $options['version'] . '"'
53
            );
54
        }
55
56
        // If impersonation was set then add it to the headers.
57
        if (!empty($options['impersonation'])) {
58
            $this->__default_headers[] = new \SoapHeader(
59
                'http://schemas.microsoft.com/exchange/services/2006/types',
60
                'ExchangeImpersonation',
61
                $options['impersonation']
62
            );
63
        }
64
65
        parent::__construct($wsdl, $options);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpEws\Ntlm\NtlmSoapClient as the method __construct() does only exist in the following sub-classes of PhpEws\Ntlm\NtlmSoapClient: PhpEws\Ntlm\ExchangeSoapClient. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
66
    }
67
68
    /**
69
     * Returns the response code from the last request
70
     *
71
     * @return integer
72
     */
73
    public function getResponseCode()
74
    {
75
        return curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
76
    }
77
}
78