Completed
Push — master ( bf6746...03a380 )
by Antonio
02:19
created

SqsQueueStoreAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
namespace Da\Mailer\Queue\Backend\Sqs;
3
4
use BadMethodCallException;
5
use Da\Mailer\Queue\Backend\MailJobInterface;
6
use Da\Mailer\Queue\Backend\QueueStoreAdapterInterface;
7
8
class SqsQueueStoreAdapter implements QueueStoreAdapterInterface
9
{
10
    /**
11
     * @var string the name of the queue to store the messages
12
     */
13
    private $queueName;
14
    /**
15
     * @var string the URL of the queue to store the messages
16
     */
17
    private $queueUrl;
18
    /**
19
     * @var SqsQueueStoreAdapter
20
     */
21
    protected $connection;
22
23
    /**
24
     * PdoQueueStoreAdapter constructor.
25
     *
26
     * @param SqsQueueStoreConnection $connection
27
     * @param string $queueName the name of the queue in the SQS where the mail jobs are stored
28
     */
29
    public function __construct(SqsQueueStoreConnection $connection, $queueName = 'mail_queue')
30
    {
31
        $this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connection of type object<Da\Mailer\Queue\B...qsQueueStoreConnection> is incompatible with the declared type object<Da\Mailer\Queue\B...s\SqsQueueStoreAdapter> of property $connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        $this->queueName = $queueName;
33
        $this->init();
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function init()
40
    {
41
        $this->getConnection()->connect();
0 ignored issues
show
Bug introduced by
The method connect() does not exist on Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreAdapter. Did you maybe mean getConnection()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
42
43
        // create new queue or get existing one
44
        $queue = $this->getConnection()->getInstance()->createQueue([
0 ignored issues
show
Bug introduced by
The method getInstance() does not seem to exist on object<Da\Mailer\Queue\B...s\SqsQueueStoreAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            'QueueName' => $this->queueName,
46
        ]);
47
        $this->queueUrl = $queue->get('QueueUrl');
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return SqsQueueStoreConnection
54
     */
55
    public function getConnection()
56
    {
57
        return $this->connection;
58
    }
59
60
    /**
61
     * @param MailJobInterface|SqsMailJob $mailJob
62
     *
63
     * @return bool whether it has been successfully queued or not
64
     */
65
    public function enqueue(MailJobInterface $mailJob)
66
    {
67
        $result = $this->getConnection()->getInstance()->sendMessage([
0 ignored issues
show
Bug introduced by
The method getInstance() does not seem to exist on object<Da\Mailer\Queue\B...s\SqsQueueStoreAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
            'QueueUrl' => $this->queueUrl,
69
            'MessageBody' => $mailJob->getMessage(),
70
            'DelaySeconds' => $mailJob->getDelaySeconds(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Da\Mailer\Queue\Backend\MailJobInterface as the method getDelaySeconds() does only exist in the following implementations of said interface: Da\Mailer\Queue\Backend\Sqs\SqsMailJob.

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...
71
        ]);
72
        $messageId = $result->get('MessageId');
73
        return $messageId !== null && is_string($messageId);
74
    }
75
76
    /**
77
     * Returns a MailJob fetched from Amazon SQS.
78
     *
79
     * @return MailJobInterface|SqsMailJob
80
     */
81
    public function dequeue()
82
    {
83
        $result = $this->getConnection()->getInstance()->receiveMessage([
0 ignored issues
show
Bug introduced by
The method getInstance() does not seem to exist on object<Da\Mailer\Queue\B...s\SqsQueueStoreAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
            'QueueUrl' => $this->queueUrl,
85
        ]);
86
87
        if (($result = $result->getPath('Messages/*')) === null) {
88
            return null;
89
        }
90
91
        return new SqsMailJob([
92
            'id' => $result['MessageId'],
93
            'receiptHandle' => $result['ReceiptHandle'],
94
            'message' => $result['Body'],
95
        ]);
96
    }
97
98
    /**
99
     * @param MailJobInterface|SqsMailJob $mailJob
100
     *
101
     * @return bool
102
     */
103
    public function ack(MailJobInterface $mailJob)
104
    {
105
        if ($mailJob->isNewRecord()) {
106
            throw new BadMethodCallException('SqsMailJob cannot be a new object to be acknowledged');
107
        }
108
109
        if ($mailJob->getDeleted()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Da\Mailer\Queue\Backend\MailJobInterface as the method getDeleted() does only exist in the following implementations of said interface: Da\Mailer\Queue\Backend\Sqs\SqsMailJob.

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...
110
            $this->getConnection()->getInstance()->deleteMessage([
0 ignored issues
show
Bug introduced by
The method getInstance() does not seem to exist on object<Da\Mailer\Queue\B...s\SqsQueueStoreAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
                'QueueUrl' => $this->queueUrl,
112
                'ReceiptHandle' => $mailJob->getReceiptHandle(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Da\Mailer\Queue\Backend\MailJobInterface as the method getReceiptHandle() does only exist in the following implementations of said interface: Da\Mailer\Queue\Backend\Sqs\SqsMailJob.

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...
113
            ]);
114
            return true;
115
        } elseif ($mailJob->getVisibilityTimeout() !== null) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Da\Mailer\Queue\Backend\MailJobInterface as the method getVisibilityTimeout() does only exist in the following implementations of said interface: Da\Mailer\Queue\Backend\Sqs\SqsMailJob.

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...
116
            $this->getConnection()->getInstance()->changeMessageVisibility([
0 ignored issues
show
Bug introduced by
The method getInstance() does not seem to exist on object<Da\Mailer\Queue\B...s\SqsQueueStoreAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
                'QueueUrl' => $this->queueUrl,
118
                'ReceiptHandle' => $mailJob->getReceiptHandle(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Da\Mailer\Queue\Backend\MailJobInterface as the method getReceiptHandle() does only exist in the following implementations of said interface: Da\Mailer\Queue\Backend\Sqs\SqsMailJob.

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...
119
                'VisibilityTimeout' => $mailJob->getVisibilityTimeout(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Da\Mailer\Queue\Backend\MailJobInterface as the method getVisibilityTimeout() does only exist in the following implementations of said interface: Da\Mailer\Queue\Backend\Sqs\SqsMailJob.

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...
120
            ]);
121
            return true;
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function isEmpty()
131
    {
132
        $attributes = $this->getConnection()->getInstance()->getQueueAttributes([
0 ignored issues
show
Bug introduced by
The method getInstance() does not seem to exist on object<Da\Mailer\Queue\B...s\SqsQueueStoreAdapter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
            'QueueUrl' => $this->queueUrl,
134
            'AttributeNames' => ['ApproximateNumberOfMessages'],
135
        ]);
136
        return $attributes->getPath('Attributes/ApproximateNumberOfMessages') == 0;
137
    }
138
}
139