StorableSender::getProviderClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace DoS\SMSBundle\SMS;
4
5
use DoS\SMSBundle\Provider\RecordProvider;
6
use SmsSender\DelayedSenderInterface;
7
use SmsSender\SmsSender;
8
use SmsSender\SmsSenderInterface;
9
10
class StorableSender implements DelayedSenderInterface
11
{
12
    /**
13
     * @var RecordProvider
14
     */
15
    protected $recordProvider;
16
17
    /**
18
     * @var SmsSenderInterface
19
     */
20
    protected $sender;
21
22
    /**
23
     * @var Logger
24
     */
25
    protected $logger;
26
27
    public function __construct(RecordProvider $recordProvider, SmsSender $sender, Logger $logger = null)
28
    {
29
        $this->recordProvider = $recordProvider;
30
        $this->sender = $sender;
31
        $this->logger = $logger;
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function flush()
38
    {
39
        if ($this->sender instanceof DelayedSenderInterface) {
40
            list($sentMessages, $errors) = $this->sender->flush();
0 ignored issues
show
Unused Code introduced by
The assignment to $sentMessages is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
41
42
            foreach ($errors as $error) {
43
                $this->logger->logError($error, $this->getProviderClass());
44
            }
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function send($recipient, $body, $originator = '')
52
    {
53
        $this->activateProvider();
54
55
        if ($this->logger) {
56
            $time = microtime(true);
57
            $result = $this->sender->send($recipient, $body, $originator);
58
            $duration = microtime(true) - $time;
59
60
            $this->logger->logMessage($result, $duration, $this->getProviderClass());
61
        } else {
62
            $result = $this->sender->send($recipient, $body, $originator);
63
        }
64
65
        $this->recordProvider->storeResult($result);
66
67
        return $result;
68
    }
69
70
    /**
71
     * Activate default provider.
72
     */
73
    public function activateProvider()
74
    {
75
        $provider = $this->recordProvider->getProvider()->getActivedProvider();
76
        $this->using($provider->getName());
77
    }
78
79
    /**
80
     * @param $name
81
     *
82
     * @return $this
83
     */
84
    public function using($name)
85
    {
86
        $this->sender->using($name);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SmsSender\SmsSenderInterface as the method using() does only exist in the following implementations of said interface: DoS\SMSBundle\SMS\StorableSender, SmsSender\SmsSender.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
87
88
        $pvd = $this->recordProvider->getProvider()->findByName($name);
89
        $provider = $this->sender->getProvider();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SmsSender\SmsSenderInterface as the method getProvider() does only exist in the following implementations of said interface: SmsSender\SmsSender.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
90
91
        if ($provider instanceof ProviderInterface && $pvd) {
92
            $provider->applyOptions($pvd->getParameters());
93
        }
94
95
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function acceptCallback($provider, array $response)
102
    {
103
        $provider = $this->using($provider)->sender->getProvider();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SmsSender\SmsSenderInterface as the method getProvider() does only exist in the following implementations of said interface: SmsSender\SmsSender.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
104
105
        if ($provider instanceof ProviderInterface) {
106
            $provider->processCallback($response);
107
            $provider->accept($this->recordProvider);
108
        }
109
    }
110
111
    /**
112
     * Allows to proxy method calls to the real SMS sender.
113
     *
114
     * @param $name
115
     * @param $arguments
116
     *
117
     * @return StorableSender|SmsSenderInterface
118
     */
119
    public function __call($name, $arguments)
120
    {
121
        if (is_callable(array($this->sender, $name))) {
122
            $result = call_user_func_array(array($this->sender, $name), $arguments);
123
124
            // don't break fluid interfaces
125
            return $result instanceof SmsSenderInterface ? $this : $result;
126
        }
127
    }
128
129
    /**
130
     * @return null|string
131
     */
132
    protected function getProviderClass()
133
    {
134
        return ($provider = $this->sender->getProvider()) ? get_class($provider) : null;
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SmsSender\SmsSenderInterface as the method getProvider() does only exist in the following implementations of said interface: SmsSender\SmsSender.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
135
    }
136
}
137