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.
Completed
Push — master ( 894091...53b59c )
by Carsten
08:57 queued 05:38
created

EmailExceptionMiddleware::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Germania\Middleware;
4
5
use Germania\Middleware\Exceptions\FactoryException;
6
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Psr\Http\Server\MiddlewareInterface;
12
13
14
class EmailExceptionMiddleware implements MiddlewareInterface
15
{
16
    /**
17
     * @var callable
18
     */
19
    public $mailer_factory;
20
21
22
    /**
23
     * @var callable
24
     */
25
    public $message_factory;
26
27
28
    /**
29
     * @var string
30
     */
31
    public $app_name;
32
33
34
    /**
35
     * @var string
36
     */
37
    public $include_file;
38
39
40
    /**
41
     * @param string   $app_name        Name of application (used in email subject)
42
     * @param callable $mailer_factory  Factory that returns Swift_Mailer instance
43
     * @param callable $message_factory Factory that returns Swift_Message instance
44
     */
45 12
    public function __construct($app_name, callable $mailer_factory, callable $message_factory)
46
    {
47 12
        $this->app_name = $app_name;
48
49 12
        $this->mailer_factory = $mailer_factory;
50 12
        $this->message_factory = $message_factory;
51
52 12
        $include_path = realpath(__DIR__.'/../includes');
53 12
        $this->include_file = $include_path.'/exception.php';
54 12
    }
55
56
57
58
    
59
    /**
60
     * PSR-15 Single Pass
61
     * 
62
     * @param  ServerRequestInterface  $request Server reuest instance
63
     * @param  RequestHandlerInterface $handler Request handler
64
     * @return ResponseInterface
65
     */
66 4 View Code Duplication
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        try {
69 4
            $response = $handler->handle($request);            
70 4
            return $response;
71
        }
72
        // Executed only in PHP 7, will not match in PHP 5.x
73
        catch (\Throwable $e)
74
        {
75
            $this->handleThrowable( $e );
76
            throw $e;
77
        }
78
79
        // Executed only in PHP 5.x, will not be reached in PHP 7
80
        catch (\Exception $e)
81
        {
82
            $this->handleThrowable( $e );
83
            throw $e;
84
        }        
85
86
        
87
    }     
88
89
    
90
91
    /**
92
     * PSR-7 Double Pass
93
     * 
94
     * Wrap $next callable in a try-catch block.
95
     * When an exception is caught, an email will be sent, and the execption will be re-thrown.
96
     *
97
     * @param RequestInterface   $request
98
     * @param ResponseInterface  $response
99
     * @param callable           $next
100
     *
101
     * @return ResponseInterface
102
     */
103 4 View Code Duplication
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        try
106
        {
107
            // Try to do business as usual...
108 4
            return $next($request, $response);
109
        }
110
111
        // Executed only in PHP 7, will not match in PHP 5.x
112 4
        catch (\Throwable $e)
113
        {
114 4
            $this->handleThrowable( $e );
115
            throw $e;
116
        }
117
118
        // Executed only in PHP 5.x, will not be reached in PHP 7
119
        catch (\Exception $e)
120
        {
121
            $this->handleThrowable( $e );
122
            throw $e;
123
        }
124
125
        // Anything else NOT caught here will bubble up.
126
    }
127
128
129
    /**
130
     * @param  \Exception|\Throwable $e
131
     */
132 4
    public function handleThrowable( $e )
133
    {
134
        // Render email body, prepare some things
135 4
        $text    = $this->render($e);
0 ignored issues
show
Documentation introduced by
$e is of type object<Throwable>, but the function expects a object<Germania\Middleware\Exception>.

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...
136 4
        $format  = 'text/html';
137 4
        $subject = sprintf("[%s] Exception %s", $this->app_name, get_class($e));
138
139
        // Create email message instance
140 4
        $message = $this->getMessage();
141
        $message->setContentType( $format )
142
                ->setSubject( $subject )
143
                ->setBody( $text );
144
145
        // Create emailer instance + send
146
        $mailer = $this->getMailer();
147
        $mailer->send($message);
148
    }
149
150
    /**
151
     * Creates the email body.
152
     *
153
     * In this class, an include file creates a basic information table.
154
     * Override this method to use your own method.
155
     *
156
     * @param Exception $e
157
     *
158
     * @return string Exception explanation
159
     */
160 4
    public function render($e)
161
    {
162 4
        return require $this->include_file;
163
    }
164
165
166
    /**
167
     * @return Swift_Mailer
168
     *
169
     * @throws FactoryException
170
     */
171
    public function getMailer()
172
    {
173
        $mailer_factory = $this->mailer_factory;
174
        $mailer = $mailer_factory();
175
        if (!$mailer instanceof \Swift_Mailer) {
0 ignored issues
show
Bug introduced by
The class Swift_Mailer does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
176
            throw new FactoryException('Mailer factory must return Swift_Mailer instance.');
177
        }
178
179
        return $mailer;
180
    }
181
182
183
    /**
184
     * @return Swift_Message
185
     *
186
     * @throws FactoryException
187
     */
188 4
    public function getMessage()
189
    {
190 4
        $message_factory = $this->message_factory;
191 4
        $message = $message_factory();
192 4
        if (!$message instanceof \Swift_Message) {
0 ignored issues
show
Bug introduced by
The class Swift_Message does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
193 4
            throw new FactoryException('Message factory must return Swift_Message instance.');
194
        }
195
196
        return $message;
197
    }
198
}
199