GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SwiftMailerLoggerServiceProvider::register()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 20
cts 20
cp 1
rs 8.9163
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Germania\Logger;
3
4
use Pimple\Container;
5
use Pimple\ServiceProviderInterface;
6
7
use Monolog\Logger;
8
use Monolog\Formatter\HtmlFormatter;
9
use Monolog\Handler\FingersCrossedHandler;
10
use Monolog\Handler\BufferHandler;
11
use Monolog\Handler\SwiftMailerHandler;
12
13
class SwiftMailerLoggerServiceProvider implements ServiceProviderInterface
14
{
15
16
    /**
17
     * Loglevel for FingersCrossedHandler
18
     * @var int
19
     */
20
    public $outer_loglevel = Logger::WARNING;
21
22
    /**
23
     * Loglevel for SwiftMailerHandler
24
     * @var int
25
     */
26
    public $inner_loglevel = Logger::DEBUG;
27
28
29
30
    /**
31
     * @param int|null $outer_loglevel Monolog Loglevel constant for FingersCrossedHandler. Default: `Logger::WARNING`
32
     * @param int|null $inner_loglevel Monolog Loglevel constant for SwiftMailerHandler. Default: `Logger::DEBUG`
33
     */
34 8
    public function __construct(int $outer_loglevel = Logger::WARNING, int $inner_loglevel = Logger::DEBUG)
35
    {
36 8
        $this->outer_loglevel = $outer_loglevel;
37 8
        $this->inner_loglevel = $inner_loglevel;
38 8
    }
39
40
41
    /**
42
     * @param  Container $dic [description]
43
     * @return void
44
     */
45 6
    public function register(Container $dic)
46
    {
47 6
        if (!$dic->offsetExists("SwiftMailer")) :
48 2
            throw new \RuntimeException("This service provider requires a 'SwiftMailer' service.");
49
        endif;
50
51 4
        if (!$dic->offsetExists("SwiftMailer.HtmlMessage")) :
52 2
            throw new \RuntimeException("This service provider requires a 'SwiftMailer.HtmlMessage' service.");
53
        endif;
54
55
56
        // Make sure there's a 'Monolog.Handlers' service
57 2
        if (!$dic->offsetExists('Monolog.Handlers')) :
58 2
            $dic['Monolog.Handlers'] = function ($dic) {
0 ignored issues
show
Unused Code introduced by
The parameter $dic is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59 2
                return array();
60
            };
61
        endif;
62
63
        /**
64
         * @return array
65
         */
66 2
        $dic->extend('Monolog.Handlers', function (array $handlers, $dic) {
67 2
            $handlers[] = $dic['Monolog.Handlers.SwiftMailerHandler'];
68 2
            return $handlers;
69 2
        });
70
71
72
        /**
73
         * Send log messages per mail, combining three Monolog handlers:
74
         *
75
         * 1. FingersCrossedHandler:
76
         *    When a certain level has been reached ...
77
         *
78
         * 2. BufferHandler:
79
         *    Buffers all records until closing the handler [i.e. script end],
80
         *    then pass them as batch [to the wrapped handler].
81
         *
82
         * 3. SwiftMailerHandler:
83
         *    Send all [buffered] messages with one mail
84
         *
85
         * @return FingersCrossedHandler
86
         */
87 2
        $dic['Monolog.Handlers.SwiftMailerHandler'] = function ($dic) {
88 2
            $mailer   = $dic['SwiftMailer'];
89
90
            // The mail subject will be used for LineFormatter
91
            // https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
92 2
            $message  = $dic['SwiftMailer.HtmlMessage'];
93 2
            $message->setSubject("[%channel%.%level_name%] %message%\n");
94
95
            // Set loglevel for this handler to $inner_loglevel (usually DEBUG) to gather ALL information
96 2
            $mailerHandler = new SwiftMailerHandler($mailer, $message, $this->inner_loglevel);
97 2
            $mailerHandler->setFormatter(new HtmlFormatter);
98
99
            // Build handler "sandwich"
100 2
            return new FingersCrossedHandler(new BufferHandler($mailerHandler), $this->outer_loglevel);
0 ignored issues
show
Documentation introduced by
new \Monolog\Handler\BufferHandler($mailerHandler) is of type object<Monolog\Handler\BufferHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
        };
102 2
    }
103
}
104