Completed
Push — master ( 4c9412...ee119a )
by Hamoud
03:20 queued 01:51
created

MobilyWsConfig::getAuthenticationMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NotificationChannels\MobilyWs;
4
5
use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification;
6
7
class MobilyWsConfig
8
{
9
    /**
10
     * @var array Supported authentication methods
11
     */
12
    protected $authenticationMethods = [
13
        'apiKey', 'password', 'auto',
14
    ];
15
16
    /**
17
     * @var string The authentication method
18
     */
19
    private $authMethod;
20
21
    /**
22
     * @var array
23
     */
24
    private $config;
25
26
    /**
27
     * MobilyWsConfig constructor.
28
     *
29
     * @param $config
30
     */
31 19
    public function __construct($config)
32
    {
33 19
        $this->config = $config;
34 19
        $this->setAuthenticationMethod($config);
35 17
        $this->validateCredentials();
36 15
    }
37
38 9
    public function getCredentials()
39
    {
40 9
        switch ($this->authMethod) {
41 9
            case 'password':
42
                return [
43 2
                  'mobile' => $this->mobile,
0 ignored issues
show
Documentation introduced by
The property mobile does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. 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...
44 2
                  'password' => $this->password,
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. 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...
45 2
                ];
46 7
            case 'apiKey':
47
                return [
48 2
                  'apiKey' => $this->apiKey,
0 ignored issues
show
Documentation introduced by
The property apiKey does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. 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...
49 2
                ];
50 5
            case 'auto':
51 5
                return $this->getAutoCredentials();
52
        }
53
    }
54
55 1
    public function getAuthenticationMethod()
56
    {
57 1
        return $this->authMethod;
58
    }
59
60 11
    public function __get($name)
61
    {
62 11
        if ($name == 'request') {
63 7
            return $this->config['guzzle']['request'];
64
        }
65
66 10
        if (isset($this->config[$name])) {
67 10
            return $this->config[$name];
68
        }
69 3
    }
70
71 19
    protected function setAuthenticationMethod($config)
72
    {
73 19
        if (isset($config['authentication'])) {
74 18
            if (in_array($config['authentication'], $this->authenticationMethods)) {
75 17
                return $this->authMethod = $config['authentication'];
76
            }
77
78 1
            throw CouldNotSendNotification::withErrorMessage(
79 1
                sprintf('Method %s is not supported. Please choose from: (apiKey, password, auto)',
80 1
                    $config['authentication']
81 1
                )
82 1
            );
83
        }
84
85 1
        throw CouldNotSendNotification::withErrorMessage('Please set the authentication method in the mobilyws config file');
86
    }
87
88 5
    protected function getAutoCredentials()
89
    {
90 5
        if ($this->apiKey) {
0 ignored issues
show
Documentation introduced by
The property apiKey does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. 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...
91
            return [
92 4
              'apiKey' => $this->apiKey,
0 ignored issues
show
Documentation introduced by
The property apiKey does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. 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...
93 4
            ];
94
        }
95
96
        return [
97 2
            'mobile' => $this->mobile,
0 ignored issues
show
Documentation introduced by
The property mobile does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. 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...
98 2
            'password' => $this->password,
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<NotificationChann...obilyWs\MobilyWsConfig>. 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...
99 2
        ];
100
    }
101
102 17
    protected function validateCredentials()
103
    {
104 17
        if (!isset($this->config['apiKey']) && !isset($this->config['mobile'], $this->config['password'])) {
105 2
            throw CouldNotSendNotification::withErrorMessage('No credentials were provided. Please set your (mobile/password) or apiKey in the config file');
106
        }
107 15
    }
108
}
109