Completed
Pull Request — master (#17)
by Nicolas
04:11
created

TestWorker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require __DIR__ . '/../vendor/autoload.php';
4
5
use Puzzle\Configuration\Memory;
6
use Puzzle\AMQP\Clients\Pecl;
7
use Puzzle\AMQP\Workers\WorkerContext;
8
use Puzzle\AMQP\ReadableMessage;
9
use Puzzle\AMQP\Workers\Worker;
10
use Psr\Log\LoggerAwareTrait;
11
use Psr\Log\NullLogger;
12
use Puzzle\AMQP\Workers\ProcessorInterfaceAdapter;
13
use Puzzle\AMQP\Consumers\Insomniac;
14
15
$configuration = new Memory(array(
16
    'amqp/broker/host' => 'rabbitmq',
17
    'amqp/broker/login' => 'guest',
18
    'amqp/broker/password' => 'guest',
19
    'amqp/broker/vhost' => '/',
20
    'amqp/global/disallowSilentDropping' => true,
21
    'app/id' => 'puzzle-amqp-test',
22
));
23
24
$client = new Pecl($configuration);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
25
$consumer = new Insomniac();
26
27
class TestWorker implements Worker
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
28
{
29
    use LoggerAwareTrait;
30
31
    private
32
        $nbMessages;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $nbMessages.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
33
    
34
    public function __construct()
35
    {
36
        $this->logger = new NullLogger();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
37
        $this->nbMessages = 0;
38
    }
39
40
    public function process(ReadableMessage $message)
41
    {
42
        $decoded = [
43
            "content-type" => $message->getContentType(),
44
            "body" => $message->getBodyInOriginalFormat(),
45
            "headers" => $message->getHeaders()
46
        ];
47
        
48
        var_dump($decoded);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($decoded); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
49
     
50
        $this->nbMessages++;
51
    }
52
    
53
    public function nbMessagesProcessed()
54
    {
55
        return $this->nbMessages;
56
    }
57
}
58
59
$workerContext = new WorkerContext(function() {
60
        return new TestWorker();
61
    },
62
    $consumer,
63
    'test_1'
64
);
65
66
$processor = new ProcessorInterfaceAdapter($workerContext);
67
68
$workerContext
69
    ->getConsumer()
70
    ->consume($processor, $client, $workerContext);
71
72
$nbMsg = $workerContext->getWorker()->nbMessagesProcessed();
73
if($nbMsg > 0)
74
{
75
    echo "\033[32m[SUCCESS] WORKER HAS PROCESSED $nbMsg MESSAGE(S) \033[0m" . PHP_EOL;
76
    return;
77
}
78
79
echo "\033[31m[FAILD] WORKER HAS PROCESSED NONE MESSAGE \033[0m" . PHP_EOL;
80