BatchFileSubscriber   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 9
dl 0
loc 111
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A getEvents() 0 7 1
A onPrepared() 0 16 2
A onProcess() 0 13 2
A enqueue() 0 6 1
1
<?php
2
3
namespace SegmentIO\Subscriber;
4
5
use GuzzleHttp\Command\Event\PreparedEvent;
6
use GuzzleHttp\Command\Event\ProcessEvent;
7
use GuzzleHttp\Command\Guzzle\DescriptionInterface;
8
use GuzzleHttp\Event\SubscriberInterface;
9
use Monolog\Logger;
10
use Monolog\Formatter\LineFormatter;
11
use Monolog\Handler\StreamHandler;
12
13
/**
14
 * BatchFileSubscriber Class
15
 *
16
 * @author Keith Kirk <[email protected]>
17
 */
18
class BatchFileSubscriber implements SubscriberInterface
19
{
20
    /**
21
     * Monolog Logger
22
     *
23
     * @var Logger
24
     */
25
    private $logger;
26
27
    /**
28
     * Webservice Description.
29
     *
30
     * @var DescriptionInterface
31
     */
32
    private $description;
33
34
    /**
35
     * Constructor
36
     *
37
     * @param DescriptionInterface $description
38
     * @param array                $options
39
     */
40
    public function __construct(DescriptionInterface $description, array $options = [])
41
    {
42
        $this->description = $description;
43
44
        if (!isset($options['filename'])) {
45
            $filename = sys_get_temp_dir() . "/segment-io.log";
46
        } else {
47
            $filename = $options['filename'];
48
        }
49
50
        $stream = new StreamHandler($filename, Logger::INFO);
51
        $stream->setFormatter(new LineFormatter("%message%\n", null));
52
53
        $this->logger = new Logger('segment-io');
54
        $this->logger->pushHandler($stream);
55
    }
56
57
    /**
58
     * Returns the Subscribed Events
59
     *
60
     * @return array
61
     */
62
    public function getEvents()
63
    {
64
        return [
65
            'prepared' => ['onPrepared', 'last'],
66
            'process'  => ['onProcess', 'first']
67
        ];
68
    }
69
70
    /**
71
     * Event to add Segment.io Specific data to the Event Messages
72
     *
73
     * @param PreparedEvent $event The PreparedEvent
74
     *
75
     * @return bool
76
     */
77
    public function onPrepared(PreparedEvent $event)
78
    {
79
        $command   = $event->getCommand();
80
        $operation = $this->description->getOperation($command->getName());
81
82
        if (!$operation->getData('batching')) {
83
            return false;
84
        }
85
86
        $parameters = json_decode($event->getRequest()->getBody()->getContents(), true);
87
        $this->enqueue(array_merge($parameters, ['action' => $command->getName()]));
88
89
        $event->intercept(['success' => true, 'batched' => true]);
90
91
        return true;
92
    }
93
94
    /**
95
     * Stops propagation of ProcessEvents when using Batching
96
     *
97
     * @param  ProcessEvent $event The Process Event
98
     *
99
     * @return bool
100
     */
101
    public function onProcess(ProcessEvent $event)
102
    {
103
        $command   = $event->getCommand();
104
        $operation = $this->description->getOperation($command->getName());
105
106
        if (!$operation->getData('batching')) {
107
            return false;
108
        }
109
110
        $event->stopPropagation();
111
112
        return true;
113
    }
114
115
    /**
116
     * Adds User Actions to the Log
117
     *
118
     * @param  array   $operation Operation as an associative array
119
     *
120
     * @return boolean
121
     */
122
    public function enqueue(array $operation)
123
    {
124
        $this->logger->addInfo(json_encode($operation));
125
126
        return true;
127
    }
128
}
129