Completed
Push — master ( 3f58aa...01cbeb )
by Mathijs
04:40
created

JobExchangePublishMapper::mapConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace mcorten87\rabbitmq_api\mappers;
4
5
use mcorten87\rabbitmq_api\jobs\JobBase;
6
use mcorten87\rabbitmq_api\jobs\JobExchangeDelete;
7
use mcorten87\rabbitmq_api\jobs\JobExchangeList;
8
use mcorten87\rabbitmq_api\jobs\JobExchangePublish;
9
use mcorten87\rabbitmq_api\jobs\JobUserList;
10
use mcorten87\rabbitmq_api\objects\Method;
11
use mcorten87\rabbitmq_api\objects\Url;
12
use mcorten87\rabbitmq_api\services\MqManagementConfig;
13
14
class JobExchangePublishMapper  extends BaseMapper
0 ignored issues
show
Coding Style introduced by
Expected 1 space after class name; 2 found
Loading history...
Coding Style introduced by
Expected 1 space before extends keyword; 2 found
Loading history...
15
{
16
17
    protected function mapMethod() : Method {
18
        return new Method(Method::METHOD_POST);
19
    }
20
21
    /**
22
     * @param JobExchangeDelete $job
23
     * @return Url
24
     */
25
    protected function mapUrl(JobBase $job) : Url {
26
        if (!$job instanceof JobExchangePublish) {
27
            throw new WrongArgumentException($job, JobExchangePublish::class);
28
        }
29
30
        $url = 'exchanges';
31
        $url .= '/'.urlencode($job->getVirtualHost());
32
        $url .= '/'.urlencode($job->getExchangeName());
33
        $url .= '/publish';
34
        return new Url($url);
35
    }
36
37
    /**
38
     * @param JobExchangePublish $job
39
     * @return array
40
     */
41
    protected function mapConfig(JobBase $job) : array {
42
        $body = [
43
            'payload' => (string)$job->getMessage(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class mcorten87\rabbitmq_api\jobs\JobBase as the method getMessage() does only exist in the following sub-classes of mcorten87\rabbitmq_api\jobs\JobBase: mcorten87\rabbitmq_api\jobs\JobExchangePublish. 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...
44
            'payload_encoding' => 'string', // TODO
45
            'routing_key' => (string)$job->getRoutingKey(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class mcorten87\rabbitmq_api\jobs\JobBase as the method getRoutingKey() does only exist in the following sub-classes of mcorten87\rabbitmq_api\jobs\JobBase: mcorten87\rabbitmq_api\j...BindingToExchangeCreate, mcorten87\rabbitmq_api\j...BindingToExchangeDelete, mcorten87\rabbitmq_api\j...JobBindingToQueueCreate, mcorten87\rabbitmq_api\j...JobBindingToQueueDelete, mcorten87\rabbitmq_api\jobs\JobExchangePublish. 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...
46
            'properties' => [
47
                'delivery_mode' =>  (int)$job->getDeliveryMode(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class mcorten87\rabbitmq_api\jobs\JobBase as the method getDeliveryMode() does only exist in the following sub-classes of mcorten87\rabbitmq_api\jobs\JobBase: mcorten87\rabbitmq_api\jobs\JobExchangePublish. 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...
48
                'headers' => []         // TODO
49
            ]
50
        ];
51
52
        return array_merge(parent::mapConfig($job), [
53
            'json'      =>  $body,
54
        ]);
55
    }
56
}
57