Completed
Push — master ( 46f3eb...7c230f )
by Mathieu
02:46 queued 40s
created

OpenAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 9 1
A getBlankPng() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Charcoal\Email\Api\V1;
6
7
use Charcoal\Email\EmailLog;
8
use Charcoal\Email\Services\Tracker;
9
use Charcoal\Model\Service\ModelLoader;
10
use Psr\Http\Message\ResponseInterface as Response;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
13
/**
14
 * Track link clicks.
15
 *
16
 * Open a link by ID, log the event into the database and redirect to the actual destination.
17
 */
18
class OpenAction
19
{
20
    /**
21
     * @var string
22
     */
23
    private $emailId;
24
25
    /**
26
     * @var Tracker
27
     */
28
    private $tracker;
29
30
    /**
31
     * @param string  $emailId Email log ID.
32
     * @param Tracker $tracker Tracker service.
33
     */
34
    public function __construct(string $emailId, Tracker $tracker)
35
    {
36
        $this->emailId = $emailId;
37
        $this->tracker = $tracker;
38
    }
39
40
    /**
41
     * @param Request  $request  PSR-7 Request.
42
     * @param Response $response PSR-7 Response.
43
     * @return Response
44
     */
45
    public function __invoke(Request $request, Response $response) : Response
46
    {
47
        $ip = $request->getAttribute('client-ip');
48
        $this->tracker->trackOpen($this->emailId, $ip);
49
50
        $response = $response->withHeader('Content-Type', 'image/png');
51
        $response->getBody()->write($this->getBlankPng());
0 ignored issues
show
Bug introduced by
It seems like $this->getBlankPng() targeting Charcoal\Email\Api\V1\OpenAction::getBlankPng() can also be of type boolean; however, Psr\Http\Message\StreamInterface::write() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
        return $response;
53
    }
54
55
    /**
56
     * @return boolean|false|string
57
     */
58
    private function getBlankPng()
59
    {
60
        return base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=');
61
    }
62
}
63