Psr3Logger   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 3
dl 0
loc 105
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLogger() 0 4 1
A start() 0 4 2
A tick() 0 15 3
A finish() 0 4 2
A abort() 0 4 2
A getSubscribedEvents() 0 9 1
1
<?php
2
3
/**
4
 * Tack Tracker - A library for tracking long-running task progress
5
 *
6
 * @license http://opensource.org/licenses/MIT
7
 * @link https://github.com/caseyamcl/tasktracker
8
 * @version 2.0
9
 * @package caseyamcl/tasktracker
10
 * @author Casey McLaughlin <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 * ------------------------------------------------------------------
16
 */
17
18
namespace TaskTracker\Subscriber;
19
20
use Psr\Log\LoggerInterface;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
use TaskTracker\Events;
23
use TaskTracker\Tick;
24
25
/**
26
 * PSR3-Compatible Logger Task Tracker Subscriber
27
 *
28
 * @author Casey McLaughlin <[email protected]>
29
 */
30
class Psr3Logger implements EventSubscriberInterface
31
{
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    // ---------------------------------------------------------------
38
39
    /**
40
     * Constructor
41
     *
42
     * @param LoggerInterface $logger
43
     */
44
    public function __construct(LoggerInterface $logger)
45
    {
46
        $this->logger = $logger;
47
    }
48
49
    // ---------------------------------------------------------------
50
51
    /**
52
     * Geth the logger
53
     *
54
     * @return LoggerInterface
55
     */
56
    public function getLogger()
57
    {
58
        return $this->logger;
59
    }
60
61
    // ---------------------------------------------------------------
62
63
    /**
64
     * Start callback
65
     *
66
     * @param Tick $tick
67
     */
68
    public function start(Tick $tick)
69
    {
70
        $this->logger->info($tick->getMessage() ?: 'Started', $tick->getReport()->toArray());
71
    }
72
73
    // ---------------------------------------------------------------
74
75
    /**
76
     * Tick callback
77
     *
78
     * @param Tick $tick
79
     */
80
    public function tick(Tick $tick)
81
    {
82
        $callback = ($tick->getStatus() == Tick::FAIL)
83
            ? [$this->logger, 'warning']
84
            : [$this->logger, 'info'];
85
86
        $msg = sprintf(
87
            "[%s/%s] %s",
88
            $tick->getReport()->getNumItemsProcessed(),
89
            $tick->getReport()->getTotalItemCount(),
90
            $tick->getMessage() ?: 'Tick'
91
        );
92
93
        call_user_func($callback, $msg, $tick->getReport()->toArray());
94
    }
95
96
    // ---------------------------------------------------------------
97
98
    /**
99
     * Finish callback
100
     *
101
     * @param Tick $tick
102
     */
103
    public function finish(Tick $tick)
104
    {
105
        $this->logger->info($tick->getMessage() ?: 'Finished', $tick->getReport()->toArray());
106
    }
107
108
    // ---------------------------------------------------------------
109
110
    /**
111
     * Abort callback
112
     *
113
     * @param Tick $tick
114
     */
115
    public function abort(Tick $tick)
116
    {
117
        $this->logger->warning($tick->getMessage() ?: 'Aborted', $tick->getReport()->toArray());
118
    }
119
120
    // ---------------------------------------------------------------
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public static function getSubscribedEvents()
126
    {
127
        return [
128
            Events::TRACKER_START  => 'start',
129
            Events::TRACKER_TICK   => 'tick',
130
            Events::TRACKER_FINISH => 'finish',
131
            Events::TRACKER_ABORT  => 'abort'
132
        ];
133
    }
134
}
135